简体   繁体   中英

Confusion with getview android imagebuttons

I have a listview which has two image buttons in the row. Tapping image button change the background color. When I tap on the first list item, image button background change and the view is saved but scrolling bottom of the listview, background color of image button of another list item changes as well. Below is the getView of Custom Adapter. How can I avoid this problem?

public View getView(final int i, View view, ViewGroup viewGroup) {
    ViewHolder holder = new ViewHolder();
    holder = null;

    //view=null;
    if (inflater == null) {
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    if (view == null) {
        view = inflater.inflate(R.layout.student_list, null);

        holder = new ViewHolder();

        holder.presentButton = (ImageButton) view.findViewById(R.id.imageView);
        holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2);
        holder.presentButton.setBackgroundColor(0);
        holder.absentButton.setBackgroundColor(0);


        view.setTag(holder);

    }
    else {
        holder = (ViewHolder) view.getTag();

    }

    final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext());
    final TextView tvName = (TextView) view.findViewById(R.id.tv_name);
    final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll);
    final studentInfo s = students.get(i);
    tvRoll.setText(s.getRoll() + ".");
    tvName.setText(s.getName());
    final Integer roll = Integer.parseInt(s.getRoll());
    //ivpresent.setBackgroundColor(0);
    final ViewHolder finalHolder1 = holder;
    //final ViewHolder finalHolder = holder;
    holder.presentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db1.updateUser(roll,"present");


            finalHolder1.presentButton.setBackgroundColor(GREEN);
            finalHolder1.absentButton.setBackgroundColor(0);
            //v1.setTag(v.getTag());
            //Log.d("present","Roll No: "+String.valueOf(roll));
            finalHolder1.presentButton.setTag(Integer.toString(i));
            notifyDataSetChanged();

        }
    });
    holder.absentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            db1.updateUser(roll,"absent");
            finalHolder1.presentButton.setBackgroundColor(0);
            finalHolder1.absentButton.setBackgroundColor(RED);
            //view=null;
            //Log.d("absent","Roll No: "+String.valueOf(roll));
            finalHolder1.presentButton.setTag(Integer.toString(i));
            notifyDataSetChanged();
        }
    });


    return view;

}
public static class ViewHolder {
    public ImageButton presentButton;
    public ImageButton absentButton;
}
  public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<studentInfo> students;
    private boolean presentButton = false;
    private boolean absentButton = false;

You need to add an else accordingly in your code, see below:

public View getView(final int i, View view, ViewGroup viewGroup) {
    ViewHolder holder = new ViewHolder();
    holder = null;

    //view=null;
    if (inflater == null) {
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    if (view == null) {
        view = inflater.inflate(R.layout.student_list, null);

        holder = new ViewHolder();

        holder.presentButton = (ImageButton) view.findViewById(R.id.imageView);
        holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2);
        holder.presentButton.setBackgroundColor(0);
        holder.absentButton.setBackgroundColor(0);


        view.setTag(holder);

    }
    else {
        holder = (ViewHolder) view.getTag();

    }

    final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext());
    final TextView tvName = (TextView) view.findViewById(R.id.tv_name);
    final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll);
    final studentInfo s = students.get(i);
    tvRoll.setText(s.getRoll() + ".");
    tvName.setText(s.getName());
    final Integer roll = Integer.parseInt(s.getRoll());
    //ivpresent.setBackgroundColor(0);
    final ViewHolder finalHolder1 = holder;
    //final ViewHolder finalHolder = holder;
    holder.presentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db1.updateUser(roll,"present");
            presentButton = true;
        }
    });
    holder.absentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            db1.updateUser(roll,"absent");
            absentButton = true;

        }
    });

   if(presentButton){
    finalHolder1.presentButton.setBackgroundColor(GREEN);
            finalHolder1.absentButton.setBackgroundColor(0);
            //v1.setTag(v.getTag());
            //Log.d("present","Roll No: "+String.valueOf(roll));
            finalHolder1.presentButton.setTag(Integer.toString(i));
            notifyDataSetChanged();
   } else {
    //set button to some default button like black
   }

   if(absentButton){
    finalHolder1.presentButton.setBackgroundColor(0);
            finalHolder1.absentButton.setBackgroundColor(RED);
            //view=null;
            //Log.d("absent","Roll No: "+String.valueOf(roll));
            finalHolder1.presentButton.setTag(Integer.toString(i));
            notifyDataSetChanged();
   } else {
    //set button to some default button like black
   }


    return view;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM