Android ListAdapter无法使用onItemClickListener函数

时间:2013-07-02 14:41:12

标签: android listview onitemclicklistener

我有以下代码,但onItemClickListener不起作用, 任何人都可以帮我解决问题所在吗? 我还添加了Override和setClickable,但问题仍然存在。

public class Show extends Activity implements AdapterView.OnItemClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.show);

...//Some codes in here

adapter = new MyAdapter(this, hadithList);

    list = (ListView) findViewById(R.id.list);

    list.setAdapter(adapter);

    list.setClickable(true);    

    list.setOnItemClickListener(new OnItemClickListener() {

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


            Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();

    });

}

1 个答案:

答案 0 :(得分:1)

Activity,您首先实施AdapterView.OnItemClickListener,而不是将AdapterView.OnItemClickListener设置为ListView。您应该选择其中一种方法,而不是两种方法。

如果您选择坚持实施OnItemClickListener,则应将其添加到Activity

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}

并将其添加到ListView:list.setOnItemClickListener(this);

如果您选择不执行OnItemClickListener,请执行以下操作:

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();

        }
    });

这应该可以正常工作。