4

我创建了一个数据库,其中包含带有列(_id、标题等)的表(类别)。我想从我的数据库中读取这些类别的数据并将它们列在ListView.

这是我的代码:

   public class MainActivity extends listActivity{

    private ArrayAdapter arrayAdapter;
    ArrayList results = new ArrayList();
    ListView catlist;
    Cursor cur;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        catlist = getListView();

        int parentid = getIntent().getIntExtra("catid", 0);
        openAndQueryDatabase(parentid);

        displayCatListView();

    }

    private void displayCatListView() {

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));


        catlist.setOnItemClickListener(new OnItemClickListener() {

               @Override
               public void onItemClick(AdapterView<?> parent, View v,
                 final int position, long id) {


                Toast.makeText(MainActivity.this,
                  "List View Clicked:" + position, Toast.LENGTH_LONG)
                  .show();
               }
              });     

    }

    private void openAndQueryDatabase(int parentid) {
        DataBaseHelper db = new DataBaseHelper(this);

        SQLiteDatabase dbr = db.getReadableDatabase();
        cur = dbr.rawQuery(
                "SELECT _id, title, has_sub FROM categories where parent_id=?",
                new String[] { String.valueOf(parentid) });


        if (cur != null) {
            while (cur.moveToNext()) {

                int cat_id = cur.getInt(cur.getColumnIndex("_id"));
                String cattitle = cur.getString(cur.getColumnIndex("title"));
                int has_sub = cur.getInt(cur.getColumnIndex("has_sub"));
                results.add(cat_id + cattitle + has_sub);
            }
            cur.close();
        }

        db.close();

    }
}
  1. 我可以在ItemClick 上获取项目的位置,但我想在ItemClick 上获取他们行的_id。我该怎么做呢?
  2. 我是新手。我的代码有错误吗?

非常感谢。

4

2 回答 2

12

改用 CursorAdapter( http://developer.android.com/reference/android/widget/CursorAdapter.html ),当您使用 ArrayAdapter 时,它会超出内存分配,并且如果 db 更改,您也不会收到通知。

此外,在使用 CursorAdapter 的情况下,您将拥有

public void onItemClick(AdapterView<?> parent, View v, final int position, long id)

“id”参数将与表的 _id 字段一致

得到整行就做

adapter.getItem(int position)
于 2013-07-13T19:12:37.880 回答
1

代替

int cat_id = cur.getInt(cur.getColumnIndex("_id"));
String cattitle = cur.getString(cur.getColumnIndex("title"));
int has_sub = cur.getInt(cur.getColumnIndex("has_sub"));
results.add(cat_id + cattitle + has_sub);

创建一个 Category 类来包含这些值,并使用参数化的 ArrayList。就像是

class Category {
    public int cat_id;
    public String cattitle;
    public int has_sub;

    public Category(int cat_id, ...) {
        // constructor logic here
    }
}

results.add(new Category(cat_id, cattitle, has_sub));

有了这个,您可以这样设置 onItemClickListener:

        catlist.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View v,
             final int position, long id) {
                 Category clickedCategory = results.get(position);
                 int id = clickedCategory.cat_id;

                 // do something with id
           }
          });     

你的 ArrayList 是你的 ArrayAdapter 的数据源,position对应于其中被点击的 Category 的索引。

于 2013-07-13T18:14:26.820 回答