如何使用数组适配器在Spinner中显示选定的值?

时间:2013-12-19 12:59:52

标签: android sqlite android-spinner

因为我是android的新手,我想在保存表单的视图时显示我保存的微调器值 如何在查看微调器

时显示数据库保存的值

这是我的代码 Java活动文件

        Spinner spnAECust;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ae_view_edit_sales);

            spnAECust = (Spinner) findViewById(R.id.spnAECust);

        /* Get Customer List and Add Select Default */
        cust = con.getAllCustomers();// from database getting list          
        List<Customer> custList = new ArrayList<Customer>();
        Customer c = new Customer();
        c.setId(Constants.Common.ZERO);
        c.setNm("Select Customer");
        custList.add(c);
        custList.addAll(cust);

        // Create and fill an ArrayAdapter with a bunch of "Customer" objects
        ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
                custList.toArray(new Customer[custList.size()]));

        // Tell the spinner about our adapter
        spnAECust.setAdapter(custArrayAdapter);

        sa = con.getAirSalesActivityDetails(Integer.parseInt(saId));// get details from Sqlite

        Customer cust = new Customer();
        cust.setId(sa.getCustomerId());
        spnAECust.setSelection(custArrayAdapter.getPosition(cust));// to set value saved in db

}

在尝试的setSelection但它匹配索引值而不是id值所以我得到抽象值到b选择请告诉我正确的方法来实现...提前提前

1 个答案:

答案 0 :(得分:0)

我在这里得到了自己的回答

/* Get Customer List and Add Select Default */
    cust = con.getAllCustomers();
    List<Customer> custList = new ArrayList<Customer>();
    Customer cst = new Customer();
    cst.setId(Constants.Common.ZERO);
    cst.setNm(Constants.Common.CUSTOMER_HINT);
    custList.add(cst);
    custList.addAll(cust);

    /* Get Commodity List and Add Select Default */
    comm = con.getAllCommodities();
    List<Commodity> commList = new ArrayList<Commodity>();
    Commodity cm = new Commodity();
    cm.setId(Constants.Common.ZERO);
    cm.setNm(Constants.Common.COMMODITY_HINT);
    commList.add(cm);
    commList.addAll(comm);

// Create and fill an ArrayAdapter with a bunch of "Customer" objects
    ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
            custList.toArray(new Customer[custList.size()]));
    int custIndex = 0;
    // to set selected item
    for (int r = 0; r < custArrayAdapter.getCount(); r++) {
        if (sa.getCustomerId() == custArrayAdapter.getItem(r).getId()) {
            custIndex = r;
            break;
        }
    }
    // Tell the spinner about our adapter
    spnAECust.setAdapter(custArrayAdapter);
    spnAECust.setSelection(custIndex);

    // Create and fill an ArrayAdapter with a bunch of "Commodities" objects
    ArrayAdapter<Commodity> commArrayAdapter = new ArrayAdapter<Commodity>(this, android.R.layout.simple_spinner_item,
            commList.toArray(new Commodity[commList.size()]));
    int commIndex = 0;
    // to set selected item
    for (int r = 0; r < commArrayAdapter.getCount(); r++) {
        if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
            commIndex = r;
            break;
        }
    }
    // Tell the spinner about our adapter
    spnAEComodity.setAdapter(commArrayAdapter);
    spnAEComodity.setSelection(commIndex);

用于循环以获取已保存值的索引

int commIndex = 0;
// to set selected item
for (int r = 0; r < commArrayAdapter.getCount(); r++) {
    if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
        commIndex = r;
        break;
    }
}

并将索引添加到

spnAEComodity.setSelection(commIndex);