0

As i am newbie in android i want to show my saved spinner value at the time of view of saved form how can i show database saved value at the time of view for spinner

here is my code Java activity file

        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

}

at tried setSelection but it maches index value rather than id value so i get abstract value to b selected please show me correct way to implement ...Thnks in advance

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Aditi K
  • 1,534
  • 5
  • 22
  • 43
  • search on google... spnAECust.setSelection(custArrayAdapter..getItem(0)); http://stackoverflow.com/a/8116756/1168654 – Dhaval Parmar Dec 20 '13 at 04:42
  • spnAEToPort.setSelection(); is used in which i get index value using for loop 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; } } – Aditi K Dec 24 '13 at 05:21
  • its not proper solution yet ...finding for more perfect solution – Aditi K Dec 24 '13 at 05:22

1 Answers1

0

Here i got answer by my own

/* 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);

used for loop to get index for saved value

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;
    }
}

and add index to

spnAEComodity.setSelection(commIndex);
Aditi K
  • 1,534
  • 5
  • 22
  • 43