Android: listview: get the cursor value of the item

I create a database and table (category) (_id, title,...) with columns. I want to read the category from DB and display the category list as a list view

This is the Y code:

   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) I can get the location of onItemClick. But I want to get the category_ ID onItemClick. Please help

2) I'm new. Is there anything wrong with my code?

Thank you.

resolvent:

replace

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

Create a category class to contain these values and use the parameterized ArrayList. Like

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

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

and

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

With this, you can set onitemclicklistener as follows:

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

Your ArrayList is the data source of the arrayadapter, and the position corresponds to the index of the category clicked in it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>