0

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

请问有什么建议吗?

4

1 回答 1

3

不要OnClickListener在每一行上设置一个,而是OnItemClickListenerListView.

在您的Activity或中Fragment

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);
    }
});
于 2013-05-09T13:48:26.010 回答