2014-09-06 111 views
0

我試圖從數據庫中檢索所有數據,並將其放入arraylist,但我只獲得一個表的記錄。這裏是我的代碼:arraylist返回只有一個記錄

where Birthdates is arraylist. 

public ArrayList<String> read() 
{ 
Birthdates.clear(); 
String selectQuery = "SELECT * FROM Birthday_Reminder"; 
Cursor crs=database.rawQuery(selectQuery,null); 

System.out.println("In read"); 
if(crs.moveToFirst()) 
{ 
    do { 
     Toast.makeText(con,"adding to arraylist", Toast.LENGTH_LONG).show(); 
     Birthdates.add(crs.getString(crs.getColumnIndex("B_Date"))); 
     } 
    while (crs.moveToNext()); 

} 
return Birthdates; 

} 
+0

什麼吐司消息的數量算?它是一個嗎? – 2014-09-06 12:23:37

回答

0

我這樣做的方式,通過使用getter和setter

package com.sunil.smilee; 

public class DataOfUser { 

    int _id; 
    String _data; 
    String _date; 

    // Empty constructor 
    public DataOfUser(){ 

    } 


    // constructor 

    // getting ID 
    public int getID(){ 
     return this._id; 
    } 

    // setting id 
    public void setID(int id){ 
     this._id = id; 
    } 

    // getting name 
    public String getD(){ 
     return this._data; 
    } 

    // setting name 
    public void setD(String s){ 
     this._data = s; 
    } 
    // 
    public void setDate(String string) { 
     // TODO Auto-generated method stub 
     this._date = string; 
    } 

    public String getDate(){ 
      return this._date ; 
     } 

} 

    // Add it in database class or change accordingly 
// Getting All Contacts 
     public List<DataOfUser> getAllContacts() { 
      List<DataOfUser> dist = new ArrayList<DataOfUser>(); 
      // Select All Query 
      String selectQuery = "SELECT * FROM " + ITEM.TABLE_DATA; 

      SQLiteDatabase db = this.getWritableDatabase(); 
      Cursor cursor = db.rawQuery(selectQuery, null); 

      // looping through all rows and adding to list 
      if (cursor.moveToFirst()) { 
       do { 
        DataOfUser contact = new DataOfUser(); 
        contact.setID(Integer.parseInt(cursor.getString(0))); 
        contact.setD(cursor.getString(1)); 
        contact.setDate(cursor.getString(2)); 


        dist.add(contact);//add data in list 
       } while (cursor.moveToNext()); 
        // 
      } 

      // return contact list 
      return dist; 
     }