Android ListView setOnItemClickListener不起作用

时间:2016-07-09 18:49:50

标签: android listview

这个简单的代码有一个listView,工作正常,移动复制/粘贴后在其他项目中,setOnItemClickListener不起作用,所有类和listview id,活动布局都是唯一的,为什么这个监听器在项目中不起作用?

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView               simple_list;
    private ArrayList<SimpleModel> data;
    private MyAdapter              adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data = new ArrayList<>();
        simple_list = (ListView) findViewById(R.id.simple_list);

        for (int i = 0; i <= 5; i++) {
            data.add(new SimpleModel("Test " + i, "title " + i, "", ""));
        }

        adapter = new MyAdapter(this, data);
        simple_list.setAdapter(adapter);
        simple_list.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(MainActivity.this, "ddddd " + i, Toast.LENGTH_SHORT).show();
    }

    public class MyAdapter extends ArrayAdapter<SimpleModel> {
        public MyAdapter(Context context, ArrayList<SimpleModel> users) {
            super(context, 0, users);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SimpleModel data = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.ewallet_items, parent, false);
            }
            TextView e_account_number = (TextView) convertView.findViewById(R.id.e_account_number);
            e_account_number.setText(data.getAccount_number());
            return convertView;
        }
    }

    public class SimpleModel {
        private String account_number;
        private String title;
        private String symbol;
        private String amount;

        public SimpleModel(String account_number, String title, String symbol, String amount) {
            this.account_number = account_number;
            this.title = title;
            this.symbol = symbol;
            this.amount = amount;
        }
    }
}

活动xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/simple_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:divider="@null"/>
</RelativeLayout>

enter image description here

问题已解决

父列表视图项目是clickable =“true”,删除该行后,我的问题已解决

2 个答案:

答案 0 :(得分:1)

如果 ListView的行布局/项目布局包含一些可点击元素,那么父级(行布局)失去焦点,而OnItemClickListener将不再工作

如果您的行布局/项目布局包含任何可点击项目,则必须将其添加到父布局:

android:descendantFocusability="blocksDescendants"

所以父母会重新关注它。

答案 1 :(得分:0)

您不必创建新的适配器来显示您的项目,您可以通过调用(String [])data.toArray()

轻松地将ArrayList转换为字符串。