自定义列表视图中的android按钮

时间:2012-09-02 12:47:09

标签: android listview button

我使用自定义ListView开发了一个Android应用程序。我的自定义ListViewTextviewImageView和2 ImageButton组成。我想点击它时更改ImageButton的资源。 (例如:从游戏到停止)。更清楚的是,我在下面附上我的设计。

enter image description here

如何从ImageButton控制我的Activity

public class MyCustomBaseAdapterSurat extends BaseAdapter {
    private static ArrayList<SearchResults> searchArrayList;

    private LayoutInflater mInflater;
    ViewHolder holder;
    private StreamingMediaPlayer audioStreamer;

    public MyCustomBaseAdapterSurat(Context context,
            ArrayList<SearchResults> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return searchArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.customlistsurat, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.name);
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
            holder.play = (ImageButton) convertView
                    .findViewById(R.id.imageButton1);
            holder.rep = (ImageButton) convertView
                    .findViewById(R.id.imageButton2);
             holder.play.setOnClickListener(klikplay);
        //   holder.rep.setOnClickListener(klikrep);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.rep.setId(searchArrayList.get(position).getIdRep());
        holder.play.setId(searchArrayList.get(position).getIdPlay());
        holder.txtName.setText(searchArrayList.get(position).getName());
        holder.iv.setBackgroundResource(searchArrayList.get(position)
                .getDrawable());
        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        ImageView iv;
        ImageButton play;
        ImageButton rep;
    }

    private OnClickListener klikplay = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            holder.play.setBackgroundResource(searchArrayList.get(v.getId())
                    .getDrawable());
            //startStreamingAudio();
        }
    };

}

0 个答案:

没有答案