0

我有下面的代码,但是 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();

    });

}
4

1 回答 1

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

        }
    });

这应该可以正常工作。

于 2013-07-02T14:51:04.440 回答