简体   繁体   中英

Database returns only the last db item

This is my first try to work with databases in android.i want my app to get all the database data and present them in a list.My problem is that im just presenting the last item of the database in my list.This is the code im getting database data:

public String[] getData()
{

    String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE ,  DBHelper.AUTHOR, DBHelper.ISBN };
    Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
    String sa = null;
    String sb = null;
    String sc = null;


    int iRow=c.getColumnIndex(DBHelper.ROWID);
    int is1=c.getColumnIndex(DBHelper.TITLE);
        int is2=c.getColumnIndex(DBHelper.AUTHOR);
            int is3=c.getColumnIndex(DBHelper.ISBN);

            for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                 sa=c.getString(is1);
                 sb=c.getString(is2);
                 sc=c.getString(is3);
            }
return new String[] {sa,sb,sc};
}

What i have to return in order to see the whole database?Thanks

This is my onCreate:

  HotOrNot entry2=new HotOrNot(this);
       entry2.open();  
          String[] data2=entry2.getData();
          entry2.close();

          Toast.makeText(SQLView.this,data2[0].toString()+"  "+data2[1].toString()+"  "+data2[2].toString(), Toast.LENGTH_SHORT).show();




       ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


       HashMap<String, String> map = new HashMap<String, String>();

       for(int i=0;i<data2.length;i+=3){
       map = new HashMap<String, String>();
       map.put("name",data2[i].toString());
       map.put("address", data2[i+1].toString());
       map.put("address2", data2[i+2].toString());

       mylist.add(map);
       }


       // ...
       ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
               new String[] { "name", "address", "address2"},
               new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
       lv.setAdapter(mSchedule);

@Aki

public String[] getData()
{

    String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE ,  DBHelper.AUTHOR, DBHelper.ISBN };
    Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
    String result="";
    String sa = null;
    String sb = null;
    String sc = null;


    int iRow=c.getColumnIndex(DBHelper.ROWID);
    int is1=c.getColumnIndex(DBHelper.TITLE);
        int is2=c.getColumnIndex(DBHelper.AUTHOR);
            int is3=c.getColumnIndex(DBHelper.ISBN);

            for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                //result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
                 sa=c.getString(is1);
                 sb=c.getString(is2);
                 sc=c.getString(is3);
            }
return new String[] {sa,sb,sc};
}

and

public class SQLView extends ListActivity {
    /** Called when the activity is first created. */    

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.list_layout); 
       HotOrNot entry2=new HotOrNot(this);
       entry2.open();  
       Cursor cursor = getContentResolver().query(DBHelper.DATABASE_TABLE, new String[] {DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN}, null, null, null);

                   startManagingCursor(cursor);



                   // THE DESIRED COLUMNS TO BE BOUND

                   String[] columns = new String[] { DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN };

                   // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
       int[] to = new int[] { R.id.rtextView1,R.id.rtextView2,R.id.rtextView3 };


                   // CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION

                   SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);


                   // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER

                   this.setListAdapter(mAdapter);

             }

       }

Inside your getData method, you're looping and on each iteration of the loop, you set sa, sb and sc. You don't, however, add those to any collection until the loop exits so the array only holds the last set of values. If you still want to return a string array, you can do this:

List<Map<String,String>> data = new ArrayList<Map<String,String>>();
 int iRow=c.getColumnIndex(DBHelper.ROWID);
    int is1=c.getColumnIndex(DBHelper.TITLE);
        int is2=c.getColumnIndex(DBHelper.AUTHOR);
            int is3=c.getColumnIndex(DBHelper.ISBN);

            for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                 sa=c.getString(is1);
                 sb=c.getString(is2);
                 sc=c.getString(is3);
                 Map<String,String> map = new HashMap<String,String>();
                 map.put("name",sa);
                 map.put("address",sb);
                 map.put("address2",sc);
                 data.add(map);
            }

return data;

Then in your onCreate method you can simply do this:

  HotOrNot entry2=new HotOrNot(this);
  entry2.open();  
  List<Map<String,String>> data2=entry2.getData();
  entry2.close();
  ListAdapter mSchedule = new SimpleAdapter(this, data2, R.layout.row,
               new String[] { "name", "address", "address2"},
               new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
  lv.setAdapter(mSchedule);

Your method returns an array containing 3 values : sa, sb and sc (you should use more descriptive names). You should make it return a list of arrays. Each array in the list would be a row of your table:

public List<String[]> getData() {
    // ...
    List<String[]> result = new ArrayList<String[]>()
    for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
        String sa = c.getString(is1);
        String sb = c.getString(is2);
        String sc = c.getString(is3);
        result.add(new String[] {sa, sb, sc});
    }
    return result;
}

And since Java is an OO language, you should even create an object for each row:

public List<SomeObject> getData() {
    // ...
    List<String[]> result = new ArrayList<String[]>()
    for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
        String sa = c.getString(is1);
        String sb = c.getString(is2);
        String sc = c.getString(is3);
        result.add(new SomeObject(sa, sb, sc));
    }
    return result;
}

That is because you are overwriting the values in your string array and because you cannot possibly return the entire database in a String array of 3 elements.

A better approach would be to return the cursor and retrieve the data from the cursor where you require it. Or else, you could use an ArrayList of String arrays to store and return the values from your method. Look at JB Nizet's answer for the code.

Edit : Looking at your onCreate I would recommend using the Cursor directly to create a CursorAdapter and setting that as your ListAdapter. You would not have to go through the creation of Map. It would make your life a lot simpler.

Here is a good tutorial for creating SimpleCursorAdapter s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM