0

我已经搜索并无法弄清楚为什么我的代码无法正常工作。我想从我的数据库中显示一行并在列表视图中显示该行的所有记录。该代码有效,但是它只返回一个列表视图值,而不是数据库内行中的所有记录。这是我更新的代码:

db = openOrCreateDatabase ("Names", MODE_PRIVATE, null); 

    String query = "SELECT * from players";
    Cursor c = db.rawQuery(query, null);
    int count = c.getCount();
    c.moveToFirst();

    ListView layout=(ListView)findViewById(R.id.listView1);  

    for (Integer j = 0; j < count; j++){ 


        String lister = c.getString(c.getColumnIndex("names_of"));

        String[] items = {lister};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        layout.setAdapter(adapter);
    c.moveToNext();

    }

    db.close();


}

数组工作不正常,我如何获得比第一个值更多的值?谢谢大家
在此处输入图像描述

4

2 回答 2

0

使用这个,您应该将所有值存储在字符串数组中并将适配器线移出循环

for (Integer j = 0; j < count; j++){ 
String[j] items = lister;
}

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
  layout.setAdapter(adapter);
于 2013-08-23T07:09:08.587 回答
0

试试这个代码。

try to use this:

db = openOrCreateDatabase ("Names", MODE_PRIVATE, null); 

String query = "SELECT * from players";
Cursor c = db.rawQuery(query, null);
int count = 0;
String[] items=new String[c.getCount()]; 

if (c.moveToFirst()) {

  do {
            TextView txt = new TextView(getApplicationContext());
            txt.setText(c.getString(c.getColumnIndex("names_of")));
            txt.setPadding(20, 20, 20, 20);
            txt.setTextSize(20.0f);
            txt.setTextColor(Color.MAGENTA);
            String lister = c.getString(c.getColumnIndex("names_of"));
            items[count] = lister;
            count++;


     } while (c.moveToNext());

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

在那之后设置适配器如果items不为空

  if(items.length>0){
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
      layout.setAdapter(adapter);
  }
于 2013-08-23T07:25:43.577 回答