OnItemClickListener麻烦 - 不检测ListView项的名称

时间:2014-02-12 12:26:26

标签: android listview

getListAdapter()上的Eclipse错误:对于类型new AdapterView.OnItemClickListener(){}

,未定义方法getListAdapter(ArrayAdapter)
listView1.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
        Object o = this.getListAdapter(lvAdapter).getItem(pos);
        String keyword = o.toString();
        makeToast("List Selected: " + pos,"#ff1111","#cfcfcf","testImage");
        Log.d("ListView", String.valueOf(pos));
    }});

所有其他 - 正常工作

4 个答案:

答案 0 :(得分:1)

this您所在的OnItemClickListener。如果您删除this它应该有效。或者,您可以使用MyActivity.this.getListAdapter(lvAdapter)MyActivity替换为您的活动/片段名称

答案 1 :(得分:0)

更改此类代码,使用您的活动名称

更改YouaActivity
 listView1.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
    Object o = YouaActivity.this.getListAdapter(lvAdapter).getItem(pos);
    String keyword = o.toString();
    makeToast("List Selected: " + pos,"#ff1111","#cfcfcf","testImage");
    Log.d("ListView", String.valueOf(pos));
}});

答案 2 :(得分:0)

listView1.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
        Object o = MyActivity.getListAdapter(lvAdapter).getItem(pos);
        String keyword = o.toString();
        makeToast("List Selected: " + pos,"#ff1111","#cfcfcf","testImage");
        Log.d("ListView", String.valueOf(pos));
    }});

答案 3 :(得分:0)

谢谢朋友们: - )))

正常使用此变体:

listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
String itemName = (String) adapter.getItemAtPosition(pos);
makeToast("List Selected: " + pos + " / " + itemName,"#ff1111","#cfcfcf","testImage");
Log.d("ListView", String.valueOf(pos));
}});