AdapterView.OnItemClickListener无法正常工作

时间:2016-02-24 19:26:31

标签: java android

AdapterView.OnItemClickListener itemClickListener=new
    AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> listView, int position, View v, long id) {
            if (position == 0) {
                Intent intent=new Integer(TopLevelActivity.this, DrinkCategoryActivity.class);
                startActivity(intent);
            }
        }
    };

2 个答案:

答案 0 :(得分:1)

我们假设您的AdapterViewListView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
            startActivity(intent);

答案 1 :(得分:0)

您需要将听众设置为ListView。这就是它无法正常工作的原因。

这样做:

AdapterView.OnItemClickListener itemClickListener = 
      new AdapterView.OnItemClickListener() {  ... }; // create listener (you did that already)

listView.setOnItemClickListener(itemClickListener); // set listener