0

I want to add two list in one list view.when i click item app is crash and show Index Out Of Bounds Exception . how to solve it ?

Combine two model class in one model class and then use

My sqlite method is..

      public List<Combine>getListbyId(String id) {

            db = getReadableDatabase();
            List<Combine> combineList = new ArrayList<Combine>();
            String SELECT_QUERY_Category = " SELECT * FROM " + TABLE_CATEGORY + " WHERE " + CATEGORY_PARENT_ID + " = " + id;

            Log.e("fetch","fetchitem " +SELECT_QUERY_Category);
            Cursor cursor = db.rawQuery(SELECT_QUERY_Category, null);


        if (cursor.moveToFirst()) {
            do {
                Combine combine = new Combine();
                combine.setId(cursor.getInt(0));
                combine.setName(cursor.getString(1));
                combine.setType(0);

                combineList.add(combine);
            } while (cursor.moveToNext());

            cursor.close();

    }
            String SELECT_QUERY_ITEM = " SELECT * FROM " + TABLE_ITEM + " WHERE " + ITEM_CATEGORY_ID + " = " + id;
            Log.e("fetch","fetchitem " +SELECT_QUERY_ITEM);
            Cursor cursor1 = db.rawQuery(SELECT_QUERY_ITEM, null);

                if (cursor1.moveToFirst()) {
                    do {
                        Combine combine = new Combine();
                        combine.setId(cursor1.getInt(0));
                        combine.setName(cursor1.getString(1));
                        combine.setType(1);

                        combineList.add(combine);
                    } while (cursor1.moveToNext());

                    cursor1.close();
                }
                    db.close();



            return combineList;
        }

Then i call this method when list view category is click and get category id .. get cat id from list..

     int cat_id = UnsobergamesApplication.combine.get(position).getId();
    call method..
     List<Combine>combineList = handler.getListbyId(String.valueOf(cat_id));

    check type "0" for catgory and "1" for Item
    get type..
     int type = UnsobergamesApplication.combine.get(position).getType();
        if (type == 0){
     if(!combineList.isEmpty())
    {

set adapter in list..
     adapter = new ArrayAdapter(SubCategoryActivity.this,android.R.layout.simple_list_item_1,combineList);                                                                            

listView_Category.setAdapter(adapter);
check type 1 for item ..
    }else(type==1){
 some code here for item.. when type one is click go next screen and show details of item..
    }

but Type 1 is not work and when i click item give Index out of bound Exception please solve my problem ,how to check type please and how to give condition please help me.. thanks

Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
user5829709
  • 23
  • 1
  • 7

1 Answers1

0

Change your code at

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        // do what you need with the cursor here
        Combine combine = new Combine();
        combine.setId(cursor.getInt(0));
        combine.setName(cursor.getString(1));
        combine.setType(0);

        combineList.add(combine);
    }
J.D.
  • 1,401
  • 1
  • 12
  • 21