繁体   English   中英

如何从SQLite数据库表填充Android Spinner

[英]How to populate Android Spinner from SQLite Database Table

我有一个SQLite数据库,其中有2个表(很快就会有更多表),我试图从这些表中的1个中填充微调器,并且找不到用于指定特定表的资源。

所以我目前的情况是我必须在“菜单项”和“菜单类别”表中...我在“菜单项”中有一个微调框,想要在其中填充“菜单类别”中的项。

这是我的MenuItem活动

public class SettingsMenuItem extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_menu_item);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    countRecords();
    readRecords();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Button buttonCreateLocation = (Button) findViewById(R.id.btnAdd);
    buttonCreateLocation.setOnClickListener(new OnClickListenerCreateMenuItem());
}

public void countRecords() {

    int recordCount = new TableControllerMenuItem(this).count();
    TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount);
    textViewRecordCount.setText(recordCount + " records found.");

}

public void readRecords() {

    LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
    linearLayoutRecords.removeAllViews();

    List<ObjectMenuItem> menuitem = new TableControllerMenuItem(this).read();

    if (menuitem.size() > 0) {

        for (ObjectMenuItem obj : menuitem) {

            int id = obj.id;
            String menuitemName = obj.name;
            String menuitemDescription = obj.description;
            Float menuitemPrice = obj.price;
            Float menuitemCost = obj.cost;

            String textViewContents = menuitemName;

            TextView textViewMenuItem= new TextView(this);
            textViewMenuItem.setPadding(0, 10, 0, 10);
            textViewMenuItem.setText(textViewContents);
            textViewMenuItem.setTag(Integer.toString(id));
            textViewMenuItem.setTextSize(20.0f);

            textViewMenuItem.setOnLongClickListener(new OnLongClickListenerMenuItem());


            linearLayoutRecords.addView(textViewMenuItem);
        }

    }

    else {

        TextView locationItem = new TextView(this);
        locationItem.setPadding(8, 8, 8, 8);
        locationItem.setText("No records yet.");

        linearLayoutRecords.addView(locationItem);
    }

}

这是我的MenuItem xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editTextItemName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:hint="Item Name"
    android:singleLine="true" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editTextItemDesc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editTextItemName"
    android:hint="Item Description"
    android:singleLine="true" />

<EditText
    android:id="@+id/editTextItemPrice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editTextItemDesc"
    android:hint="Item Price"
    android:singleLine="true"
    android:inputType="numberDecimal"/>

<EditText
    android:id="@+id/editTextItemCost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editTextItemPrice"
    android:hint="Item Cost"
    android:singleLine="true"
    android:inputType="numberDecimal"/>

<Spinner
    android:id="@+id/spinnerCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editTextItemCost"></Spinner>

从数据库中读取数据,并将相关数据保存到数组中。 然后使用该数组填充ArrayAdapter,最后将其设置为微调器的适配器。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM