2017-11-11 145 views
-1

喜使用SQLite我,我需要顯示所有coulms的名字,但列表視圖只顯示最後colums命名支票影像enter image description here One is last addedsqlite的列表視圖只顯示最後科拉姆或一個

private void ListShow() { 
      list = findViewById(R.id.list); 
      myDb.getReadableDatabase(); 
      myDb.getWritableDatabase(); 
      Cursor res = myDb.getAllData(); 
      while (res.moveToNext()) { 
       String[] strNAME = new String[]{res.getString(0) 
       }; 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, android.R.id.text1, strNAME); 
       list.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
      } 
     } 
public Cursor getAllData() { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor res = db.rawQuery("select * from "+TABLE_NAME,null); 
     return res; 
    } 
+0

因爲您正在重新創建並在每次循環迭代時重新分配ArrayAdaper。另外請注意,你濫用'notifyDatabaseChanged()'方法 –

回答

0

您函數需要返回ArrayList的對象,參見示例:

public ArrayList<FavoriteObj> selectAllRow() { 
      String query = "SELECT * FROM " + tableName; 

      ArrayList<FavoriteObj> favoriteObjs= new ArrayList<>(); 
      try { 
       Cursor cursor = this.getReadableDatabase().rawQuery(query, null); 
       if (cursor.moveToFirst()) { 
        do { 
         favoriteObjs.add(new FavoriteObj(
           String.valueOf(cursor.getString(1)) 
           , cursor.getString(2) 
           , null 
           , cursor.getInt(3))); 
        } while (cursor.moveToNext()); 
       } 
       cursor.close(); 
      } catch (Exception e) { 
       Log.i("error", e.toString()); 
      } 
      this.getReadableDatabase().close(); 
      return favoriteObjs; 
} 
+0

謝謝,但我不知道我必須把我把什麼字符串錯誤我把類錯誤我把anythink錯誤我pust模型錯誤請再次幫助 –

+0

FavoriteObj –

+0

Rasoul Miri <波斯語Languge> Salam Mitoni Gmaileto Bedi Chan Ta Soal Dashtam Mamnon –

相關問題