如何获取ListView中所选项目的值?

时间:2010-06-03 01:44:10

标签: android listview android-activity android-intent selecteditem

我以为我可以使用position int,但是当我点击列表视图中的项目时,没有任何反应。请帮忙!

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

2 个答案:

答案 0 :(得分:3)

此处的位置是整数,因此不应与字符('0','1'...)进行比较,而应将其与整数进行比较。

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

答案 1 :(得分:0)

这里的位置是整数。无法与字符('0','0')进行比较。