自定义listView中的按钮无法正常工作?

时间:2013-08-08 10:59:35

标签: android android-listview

在我的应用程序中,我使用带有文本视图按钮的自定义listView来更改按钮图像。单击第一个位置的按钮后,按钮的图像发生了变化,但当我向下滚动listView时,图像集又变为其他位置。

 public class CustomListViewAdapter_ringtone extends BaseAdapter {

ArrayList<HashMap<String, String>> list;
Activity activity;

public CustomListViewAdapter_ringtone(Activity activity,ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = activity;
    this.list = list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

class ViewHolder {
    Button play;
    TextView tarck_name,singer;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;

    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.test, null);

        holder = new ViewHolder();

        holder.tarck_name = (TextView)convertView.findViewById(R.id.track_name);
        holder.singer = (TextView)convertView.findViewById(R.id.singer);
        holder.play = (Button)convertView.findViewById(R.id.play_pause_btn);

        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    HashMap<String, String> map = list.get(position);
    holder.tarck_name.setText(map.get("track_name"));
    holder.singer.setText(map.get("sing"));

    holder.play.setTag(position);
    holder.play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            holder.play.setBackgroundResource(R.drawable.pausebtn);

            //here am set the background for the button but when i scroll down the image changed to some other positon
        }
    });

    return convertView;
}   
}

1 个答案:

答案 0 :(得分:0)

试试这会有所帮助:

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

    final ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();

    convertView = inflater.inflate(R.layout.test, null);

    holder = new ViewHolder();

    holder.tarck_name = (TextView)convertView.findViewById(R.id.track_name);
    holder.singer = (TextView)convertView.findViewById(R.id.singer);
    holder.play = (Button)convertView.findViewById(R.id.play_pause_btn);

    convertView.setTag(holder);
相关问题