2

使用“onItemLongClickListiner”时,有什么方法可以捕获在列表视图中单击的项目的名称?我知道我可以捕捉位置等,但我需要一个名字

4

4 回答 4

11

我想你ListView已经被 String 对象填满了:

public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id) {
 String name = (String) parent.getItemAtPosition(position);
}

AdapterView.getItemAtPosition(position)获取列表中与指定位置关联的数据。

于 2013-06-30T10:08:53.673 回答
1

您可以使用lv.getItemAtPosition(position)根据索引位置获取文本

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
        long arg3) 
{
    String name = arg0.getItemAtPosition(position).toString()
}
});

欲了解更多信息

获取项目位置

于 2013-06-30T10:16:37.300 回答
1

试试这个..

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //inflated row layout textview
            TextView tagText = (TextView) view.findViewById(R.id.txt_text);
            String tag = tagText.getText().toString();
            Toast.makeText(getApplicationContext(),
            "Element Name " + tag + " Clicked", Toast.LENGTH_SHORT).show();
        }
    });
于 2014-06-26T05:20:33.457 回答
1

它对我有用。

listtview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
       String selected = (String) adapterView.getItemAtPosition(i);
       Toast.makeText(getApplicationContext(),"This is"+selected,Toast.LENGTH_LONG).show();
  }
});
于 2018-03-23T17:43:21.360 回答