简体   繁体   中英

How do I get the value of the item selected in ListView?

I thought I could use the position int , but when I click on the item in the list view, nothing happens. Please help!

ListView d = (ListView) findViewById(R.id.apo); 
ArrayAdapter adapt = ArrayAdapter.createFromResource(this,
  R.array.algebra, android.R.layout.simple_list_item_1); 
d.setAdapter(adapt); 
d.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
        if (position == '0') {
            Intent intent = new Intent(Algebra.this, Alqv.class);
            startActivity(intent);
        }
        if (position == '2') {
            Intent intent1 = new Intent(Algebra.this, qfs.class);
            startActivity(intent1);
        }
    });
}

The position here is an integer, so instead of comparing with chars ('0', '1'...), it should be compared with integers.

 if (position == 0) {
     Intent intent = new Intent(Algebra.this, Alqv.class);
        startActivity(intent);
 }
 if (position == 2) {
     Intent intent1 = new Intent(Algebra.this, qfs.class);
        startActivity(intent1);
 }

The position here is an integer. Can't compare with chars('0','0').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM