Question

I am trying to do a listview with 2 database collumn. I succeded but my problem is that only the first information of the database appears in the listview. If i have x data in the database it will show the x rows in the listview with only the first entry.

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
data = new ArrayList();
    Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);

    if (cursor.moveToFirst()) {
        Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
        data.clear();
        do {
            data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
            data.add(cursor.getString(cursor.getColumnIndex("nome")));
            map.put("id", data.get(0).toString());
            map.put("nome", data.get(1).toString());
            map.put("hora", "17:00");
            mylist.add(map);
        } while (cursor.moveToNext());


        SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
        lista.setAdapter(resul);
Was it helpful?

Solution

I see two problems in your code:

  • You don't clear data (Like said in a previous answer) so you always get the first two values
  • You always use the same map. Since map is an object you will always modify the same and your list will be fill will one map.

To fix this two points try this:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);

if (cursor.moveToFirst()) {
    Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
    do {
        HashMap<String, String> map = new HashMap<String, String>();
        data.clear();
        data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
        data.add(cursor.getString(cursor.getColumnIndex("nome")));
        map.put("id", data.get(0).toString());
        map.put("nome", data.get(1).toString());
        map.put("hora", "17:00");
        mylist.add(map);
    } while (cursor.moveToNext());


    SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
    lista.setAdapter(resul);

OTHER TIPS

look at this,

map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");

You always put the same key in the map, always "id, nome and hora" you need put diferent key.

ps: Why you create a List of Map? You don't need to this.

You need to clear data each time through the loop. You need to move your call to data.clear() to just after the do { loop begins. What you're doing at the moment will mean that after the first item is retrieved from the cursor, data will contain 2 items. After the second time it will contain 4, then 6, etc. You then always read the same two items. If you clear data it should fix your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top