0

i have a spinner and a listview in a view. data is coming in both from sqlite . spinner data is coming from category table(Cid, Cname) Cname is to show in spinner and listview data is from Items table(Iid, Iname, Cid) Iname is to show in listview. cid is foreign key in items table .when spinner selection change i want to change listview data.this is what i have done so far.. below is my spinner with Cname populated from sqlite

      spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {                                                    dbRepo = new DBRepo(getApplicationContext());                    
            String pos = (String) parent.getSelectedItemPosition();              
            String cid =   dbRepo.getId(pos);
            Toast.makeText(getApplicationContext(), cid, Toast.LENGTH_LONG).show();
            final ArrayList<Support> list = dbRepo.getItems(cid);

                adapter = new Custom(Category.this, R.layout.view_entry, list);

                lv.setAdapter(adapter);                  
               adapter.notifyDataSetChanged();

this is the code to get cid of selected item in spinner

public  String getId(String selected) {
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT Cid from Catogeries where Cname=\"" + selected+ "\"";
    Cursor c = db.rawQuery(query, null);
    String s = null;

    if (c.moveToFirst()) {
        while (c.moveToNext()) {
            s = c.getInt(c.getColumnIndex("Cid"));

        }
    }
    c.close();
    db.close();
    Log.e("CID", " " + s);

    return s;
}

this is the method to get data in listview from db

 public ArrayList<Support> getItems(String cid){
   ArrayList<Support> list = new ArrayList<Support>();
    SQLiteDatabase db = this.getReadableDatabase();
  // Cursor c = db.query(TABLE_ITEM, new String[]{KEY_IID, KEY_INAME, KEY_CID}, KEY_CID + "=?", new String[]{cid}, null, null, null,null);
    String select = "SELECT * from items_table where Cid = "+cid;
    Cursor c = db.rawQuery(select, null);
    String res;
    int j = c.getColumnIndex(Support.KEY_INAME);
    if(c.moveToFirst()){
        do {
            Support item = new Support();
                    item.setItemID(c.getString(0));
                    item.setItemName(c.getString(1));
                    item.setCategoryID(c.getString(2));
                    list.add(item);
        }while (c.moveToNext());
    }
    c.close();
    db.close();
    return list;
}

now cid is showing in toast but after passing cid to getitems() to get items nothing showing in listview.cid is showing in getItems() in log

   public ArrayList<Support> getItems(String cid) {
    ArrayList<Support> itemlist = new ArrayList<Support>();
    SQLiteDatabase db = this.getReadableDatabase();
    Log.e("CID is", cid);
    String selectQuery = "SELECT * FROM " + TABLE_ITEM;
    Cursor c = db.rawQuery(selectQuery, null);
    if (c != null) {
        while (c.moveToNext()) {
            Support item = new Support();
            item.setItemID(c.getString(0));
            item.setItemName(c.getString(1));
            item.setCategoryID(c.getString(2));
            itemlist.add(item);
        }
    }
    c.close();
    db.close();
    return itemlist;
}

the above code is showing items without passing cid but when i pass cid listview is showing empty and their is no error showing in logcat

finally resolved my issue that was because of a space after cid using trim function while storing data fetched from webservice to sqlite has done for me

murtaza ali
  • 53
  • 2
  • 11

1 Answers1

0
public ArrayList<Support> getItems(String cid){
   ArrayList<Support> list = new ArrayList<Support>();
   SQLiteDatabase db = this.getReadableDatabase();

  Cursor cursor = database.rawQuery(SELECT * FROM items_table WHERE Cid =? ",  new String[]  {cid}, null);

   Support item = new Support( ); 
if(cursor!=null)
{
  while(cursor.moveToNext())
{
   String ID= cursor.getString(cursor.getColumnIndexOrThrow(YOUR_DatabaseHelper.id_column));
   // same with the others
   item.add(ID);
   list.add(item);
}
}
    cursor.close();
    db.close();
    return list;
}        
John Joe
  • 12,412
  • 16
  • 70
  • 135