2016-12-28 180 views
0

我只是得到了arraylist的大小而不是數據庫類返回的arraylist項,這裏我到目前爲止所做的,問題是我只從數據庫中獲取一個項目。沒有得到數據庫的所有項目

MainActivity

//inside oncreate 
List<Integer> id=new ArrayList<>(); 
List<Integer> retrieve_id=new ArrayList<>(); 

for(int i=0;i<300;i++){ 
    id.add(i); 
} 
database.addAll(id); 

//method to get all data 
public void getData(){ 

    List<Integer> id=new ArrayList<>(); 
    id=database.getAllData(); 
    Log.e("Returned size:",""+id.size()); 
} 

在數據庫類

 public static final String TABLE_NAME ="IdHelper"; 
     public static final String ID="id"; 
     public static final String CREATE_TABLE_ID = "CREATE TABLE " 
       + TABLE_NAME 
       + " (" 
       + ID 
       + " INTEGER) "; 
     public static final String DROP_TABLE_ID = "DROP TABLE IF EXISTS " + TABLE_NAME; 
     public static final String SELECT_ALL_ID = "select * from " + TABLE_NAME_ID + ";"; 


    public boolean addAll(List<Integer> id){ 

     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL(DROP_TABLE_ID); 
     db.execSQL(CREATE_TABLE_ID); 
     ContentValues values = new ContentValues(); 

     for(int i=0;i<id.size();i++){ 
      values.put(ID,id.get(i)); 
      Log.e("DBValues",id.get(i).toString()); 
     } 

     db.insert(TABLE_NAME_ID, null, values); 
     db.close(); 

     if (isInserted == -1) 
      return false; 
     else 
      return true; 

     return true; 
    } 

    public List<Integer> getAllData(){ 
     List<Integer> idList = new LinkedList<>(); 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(SELECT_ALL_ID, null); 
     if (cursor.moveToFirst()) { 
      do { 
       idList.add(Integer.valueOf(cursor.getString(cursor.getColumnIndex(ID)))); 
       Log.e("Ranjan","Return/" + cursor.getString(cursor.getColumnIndex(ID))); 
      } while (cursor.moveToNext()); 
     } 
     return idList; 
    } 
} 

回答

0

編輯這改變: -

公共靜態最後絃樂SELECT_ALL_ID = 「SELECT * FROM」 + TABLE_NAME + 「;」 ;

將這個db.insert(TABLE_NAME_ID, null, values);線內for循環: -

for(int i=0;i<id.size();i++){ 
      values.put(ID,id.get(i)); 
      Log.e("DBValues",id.get(i).toString()); 
      db.insert(TABLE_NAME_ID, null, values); 
     } 
+0

燁這個工作對我來說 –

相關問題