2016-05-31 4 views
0
klicken

Ich möchte zwei Listen in einer Listenansicht hinzufügen.Wenn ich auf Element app klicken, ist Absturz und Index außerhalb der Grenzen Exception anzeigen. Wie man es löst ?So entfernen Sie IndexOutOfBoundsException: Ungültiger Index 2, Größe ist 2 ..Wenn ich auf das Element

Kombinieren Sie zwei Modellklasse in einer Modellklasse und verwenden dann

Meine SQLite Methode ist ..

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

Dann rufe ich diese Methode, wenn der Listenansicht Kategorie ist klicken und die Kategorie-ID erhalten .. cat-ID aus der Liste erhalten ..

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

aber Typ 1 ist nicht funktioniert und wenn ich Artikel Indexaus gebundener Ausnahme geben klicken 10 bitte mein Problem zu lösen, wie Art überprüfen Sie bitte, und wie Bedingung geben mir bitte helfen .. Dank

Antwort

0

ändern Sie den Code in

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); 
    } 
+0

soory, das ist nicht richtig am .. cursor.hasnext() gib Fehler .. – user5829709

Verwandte Themen