Pages

Friday 29 July 2011

Spinner with SimpleCursorAdapter Android

If you are using android sqlite database and you want to set query data base to set in spinner on list view than you can use the SimpleCursorAdapter.

Advantage of using the SimpleCursorAdapter that when you implement on click listener on onItemSelectedListner than you don't have to save query database locally in array or something.

Cursor cursor = dbAdapter.getAllData();

  if(cursor.getCount()>0){
   String[] from = new String[]{"columm_name"};
   // create an array of the display item we want to bind our data to
   int[] to = new int[]{android.R.id.text1};
   SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
     cursor, from, to);
   mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinnerName.setAdapter(mAdapter);
  }
 

Now implementing all click listner and query the selected item id or other columm data from database.

spinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {

   public void onItemSelected(AdapterView parent, View view,
     int pos, long log) {
    
    Cursor c = (Cursor)parent.getItemAtPosition(pos);
          int id = c.getInt(c.getColumnIndexOrThrow("columm_name")); 
   }

   public void onNothingSelected(AdapterView arg0) {
    
   }
  });

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks for your great tutorial, what i want to know now is the listview with checkbox, if i checked the checkbox the datarow will be saved in another table and when i unchecked the checkbox it will delete the datarow, here is my code for list view.

    private void populateListViewFromDB() {
    Cursor cursor = myDb.getAllRowsIngredient_Filter(SetIdFood);
    startManagingCursor(cursor);
    String[] fromFieldNames = new String[]{DBAdapter.KEY_INGREDIENT};
    int[] toViewIDs = new int[]{android.R.id.text1};
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_multiple_choice, cursor, fromFieldNames, toViewIDs);
    ListView myList = (ListView) findViewById(R.id.listViewPart);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(myCursorAdapter);

    myList.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View view,
    int position, long id) {
    // When clicked, show a toast with the TextView text
    Cursor c = (Cursor) parent.getItemAtPosition(position);
    int idInDB = c.getInt(c.getColumnIndexOrThrow(DBAdapter.KEY_ROWID));

    displayToastForId(idInDB);
    }
    });
    }

    private void displayToastForId(long idInDB) {
    Cursor cursor = myDb.getRowIngredient(idInDB);
    if (cursor.moveToFirst()) {
    long idDB = cursor.getLong(DBAdapter.COL_ROWID);
    long idgroup = cursor.getLong(DBAdapter.COL_IDGROUP);
    String ingredient = cursor.getString(DBAdapter.COL_INGREDIENT);

    Getidingredient = idDB;
    GetidGroup = idgroup;
    Parts.setText(ingredient);
    String message = "ID: " + idDB + "\n"
    + "Group ID: " + idgroup + "\n"
    + "Ingredient: " + ingredient;
    Toast.makeText(Compose.this, message, Toast.LENGTH_SHORT).show();
    }
    cursor.close();
    }

    ReplyDelete