0
public class ABC extends ListActivity {
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abc_list);

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.abc_list_row,
            new String[] {"AAA","BBB","CCC"},
            new int[] {R.id.aa,R.id.bb, R.id.cc,}
            );

    add();
    setListAdapter(adapter);  
}

   private void add() {        

    DatabaseA databaseHelper = new DatabaseA(this);
    SQLiteDatabase db = databaseHelper.getReadableDatabase();

    Cursor c= db.rawQuery("select * from data", null);
    HashMap<String,String> temp = new HashMap<String,String>();  

    while(c.moveToNext()){
        Log.d("Debug", c.getString(2));
        temp.put("AAA", c.getString(1));
        temp.put("BBB", c.getString(2));
        temp.put("CCC", c.getString(3));
        list.add(temp);
    } 

    c.close();
    db.close();

    for (Map.Entry<String, String> entry : temp.entrySet()) {
        String xx= entry.getKey();
        String yy = entry.getValue();
        Log.d("data", xx+" : "+yy);
    }
}

My Logcat:

 D/Debug(657): New york
 D/Debug(657): New moon
 D/data(657): AAA : Aug 20, 2012 3:57:02 PM
 D/data(657): BBB : New moon
 D/data(657): CCC : Title

My problem is it always left out the first data from sqlite in this list. Besides, everytime i press a button to view this list, it will add those data into this list although they are already exist. I just want to view those data in sqlite in list form. Any way to solve this. Thanks.

4

2 回答 2

2

You need to create your Map inside your while loop

while(c.moveToNext()){
    Map<String,String> temp = new HashMap<String,String>();  
    Log.d("Debug", c.getString(2));
    temp.put("AAA", c.getString(1));
    temp.put("BBB", c.getString(2));
    temp.put("CCC", c.getString(3));
    list.add(temp);
} 
于 2012-08-20T09:50:46.573 回答
0

Add cursor moveToFirst() like this:

Cursor c= db.rawQuery("select * from data", null);
HashMap<String,String> temp = new HashMap<String,String>();  
c.moveToFirst();
while(c.moveToNext()){
    Log.d("Debug", c.getString(2));
    temp.put("AAA", c.getString(1));
    temp.put("BBB", c.getString(2));
    temp.put("CCC", c.getString(3));
    list.add(temp);
} 

And please post the source code when the button is clicked!

于 2012-08-20T09:47:20.357 回答