数据库仅返回最后一个db项

时间:2011-12-03 13:49:26

标签: java android database

这是我第一次尝试在android.i中使用数据库。希望我的应用程序获取所有数据库数据并将它们显示在列表中。我的问题是我只是在列表中显示数据库的最后一项。这个是获取数据库数据的代码:

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};
}

为了查看整个数据库,我必须返回什么?谢谢

这是我的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};
}

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);

             }

       }

3 个答案:

答案 0 :(得分:1)

在你的getData方法中,你循环,在循环的每次迭代中,你设置sa,sb和sc。但是,在循环退出之前,您不会将它们添加到任何集合中,因此数组只保留最后一组值。如果您仍想返回字符串数组,可以执行以下操作:

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;

然后在你的onCreate方法中你可以简单地这样做:

  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);

答案 1 :(得分:1)

您的方法返回一个包含3个值的数组:sa,sb和sc(您应该使用更多描述性名称)。 你应该让它返回一个数组列表。列表中的每个数组都是表格的一行:

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;
}

由于Java是一种OO语言,你甚至应该为每一行创建一个对象:

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;
}

答案 2 :(得分:1)

这是因为你要覆盖字符串数组中的值,因为你不可能在3个元素的String数组中返回整个数据库。

更好的方法是返回光标并从您需要它的光标中检索数据。否则,您可以使用String数组的StringList来存储和返回方法中的值。看看JB Nizet对代码的回答。

编辑:查看你的onCreate我建议你直接使用Cursor创建一个CursorAdapter并将其设置为ListAdapter。您不必经历Map的创建。它会让你的生活变得更加简单。

以下是good tutorial,用于创建SimpleCursorAdapter s