如何获取在android中单击的Listview项的值?

时间:2010-06-07 09:34:29

标签: android listview

我在下面的代码中将ListView项值访问为字符串并在alert中显示它?

ListView shot = getListView();
shot.setOnItemClickListener(this);

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

    String S = arg1.getContext().toString();
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

    // set the message to display
    alertbox.setMessage(S).show();    
}

4 个答案:

答案 0 :(得分:17)

也许这个例子可以帮到你

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

https://developer.android.com/reference/android/widget/ListView.html

答案 1 :(得分:11)

这将为您提供所点击项目的准确值。检查日志

ListView shot = getListView();
shot.setOnItemClickListener(this);

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

    String val =(String) parent.getItemAtPosition(position);
    System.out.println("Value is "+val); 
}

答案 2 :(得分:0)

也许你可以试试这个

String data = (String)shot.getItemAtPosition(arg2);
AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());              
adb.setMessage(data).show();

答案 3 :(得分:0)

获取模型的价值

adaptor.getItem(位置).getCardName();