我们可以为自定义ArrayAdapter类添加Listener吗?

时间:2013-05-09 13:45:05

标签: android

我为ArrayAdapter提供了自定义listview课程。

我正在使用该自定义适配器获得更多listviews

所有listview项都必须在点击时启动相同的活动,如何在不向每个listview项添加单个itemlistener的情况下执行此操作?

我做过这样的事情:

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)(context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        row                     = inflater.inflate(layoutResourceId, parent, false);
        ImageView albumArt      = (ImageView)(row.findViewById(R.id.album_art));
        TextView  albumName     = (TextView)(row.findViewById(R.id.album_name));

        final OnClickListener Listener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), PlayerActivity.class);
                    intent.putExtra("albumart", album.albumArt);
                    v.getContext().startActivity(intent);
                }
            };
            row.setOnClickListener(Listener);
       }

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

请勿在每行上设置OnClickListener,而是在OnItemClickListener上设置ListView

ActivityFragment

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(view.getContext(), PlayerActivity.class);
        // Add code here to get album data from the row...
        intent.putExtra("albumart", album.albumArt);
        startActivity(intent);
    }
});