有没有一种简单的方法来使用Cursor填充ListView?

时间:2014-10-13 19:13:04

标签: java android android-listview android-sqlite android-cursor

我想知道是否有一种简单的方法可以用Cursor填充ListView,就像使用EditText一样:

final EditText visnavn=(EditText)findViewById(R.id.visnavn);

DBAdapter db;

Cursor cur = db.findall();

            if(cur.moveToFirst()){
                do{
                    visnavn.append(cur.getString(0));
                    visnavn.append(" ");
                    visnavn.append(cur.getString(1));
                    visnavn.append("\n");
                }while(cur.moveToNext());
            }
            cur.close();

我尝试过这样的事,没有运气:

    Cursor c = db.findAll();
    ListView list = (ListView)findViewById(R.id.list);

    if(c.moveToFirst()){
        do{
            list.add(c.getString(0));
            list.add(" ");
            list.add(c.getString(1));
        }while(c.moveToNext());
    }
    c.close();

1 个答案:

答案 0 :(得分:-1)

Oke,正如我在评论中已经说过的那样,有一种简单的方法可以将Cursor数据加载到listview中,这是我使用的代码。也许它对你有用! :)

final Cursor locations = <get your data here>
locations.moveToFirst();
LayoutInflater inflater = this.getLayoutInflater();
while ( locations.isAfterLast() == false ) {
    View tablerow = inflater.inflate(R.layout.tablerow, null);
    TextView txtName = (TextView)tablerow.findViewById(R.id.txtName);

    txtName.setText(locations.getString(1));
    sView.addView(tablerow);
    locations.moveToNext();
}