更正android上的视图展览

时间:2012-04-12 23:05:56

标签: android listview

我正在开发一个简单的列表视图,但我收到的信息不正确,而不是正确的数据格式db。

List<Category> values = cdao.listAllItems();
ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(this,
   R.layout.row, values);
setListAdapter(adapter);


public List<Category> listAllItems() {
    List<Category> list = new ArrayList<Category>();

    Cursor cursor = db.rawQuery("SELECT _id, category " +
            " FROM category", null);

    while (cursor.moveToNext()) {
        Category c = new Category();
        c.setId(cursor.getInt(cursor.getColumnIndex("_id")));
        c.setCategory(cursor.getString(cursor.getColumnIndex("category")));
        list.add(c);
        Log.d(wallet.utils.Constants.TAG, c.getCategory());
    }
    return list;
}


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

结果是:wallet.entity.Category@44dbe038,正确的值必须是“一般”一词。

有什么问题?

提前感谢。

2 个答案:

答案 0 :(得分:0)

试试这个

if(cursor.getCount > 0) {
   cursor.moveToFirst();
   for(int i =0;i<cursor.getCount();i++) {
       Category c = new Category();
       c.setId(cursor.getInt(cursor.getColumnIndex("_id")));
       c.setCategory(cursor.getString(cursor.getColumnIndex("category")));
       list.add(c);
       Log.d(wallet.utils.Constants.TAG, c.getCategory());
   }
} 

答案 1 :(得分:0)

我发现了什么是错的。我忘了在班上覆盖toString。

@Override
public String toString() {
    return category;
}

我认为它可以帮助其他人。