1

I have a ListView and i populated that ListView with TextView and Button using custom adapter.I had generated a click event for that Button in custom adapter.In that click event i am trying to change the button text and color,up to here its working fine but when i scroll the ListView up and down the text color of other Button changing.I had stop here from past couple of days...

Here is some code which i had tried so far

@Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View v=convertView;
        final ViewHolder holder;
        if(convertView ==null)
        {
            convertView = mInlfater.inflate(R.layout.attendancelistview,null);
            holder = new ViewHolder();
            holder.b1 = (Button)convertView.findViewById(R.id.row3);
            holder.tv1 = (TextView)convertView.findViewById(R.id.row1);
            holder.tv2 = (TextView)convertView.findViewById(R.id.row2);
            convertView.setTag(holder);
        }
        else
        {
            holder =(ViewHolder) convertView.getTag();
        }
        //List<StudentData> data = list.get(position).Rno;
        holder.tv1.setText(String.valueOf(list.get(position).Rno));
        holder.tv2.setText(list.get(position).StudentName);
        holder.b1.setText(list.get(position).Attendance);
        holder.b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if(holder.b1.getText().equals("Present"))
                {
                    holder.b1.setText("Absent");
                    //list.get(position).put("Attendance", "Absent");

                    holder.b1.setTextColor(Color.RED);
                }
                else if(holder.b1.getText().equals("Absent"))
                {
                    holder.b1.setText("Present");
                    //list.get(position).put("Attendance", "Present");
                    holder.b1.setTextColor(Color.GREEN);
                }
                String rno1=  String.valueOf(holder.tv1.getText());
                name=String.valueOf(holder.tv2.getText());
            }

        });
        return convertView;
    }
    static class ViewHolder
    {
        Button b1;
        TextView tv1,tv2,tv3;
    }
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69

3 Answers3

1

If holder.b1 button text is changing then the reason is you are not handling your getView properly.

I guess this returns some Integer id

list.get(position).Rno

So you need to store that id in some arraylist for example

//Declare this outside of getView().   
 ArrayList<Integer> your_number = new ArrayList();

And in your button click

holder.b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if(holder.b1.getText().equals("Present"))
                {
                    holder.b1.setText("Absent");
                    //list.get(position).put("Attendance", "Absent");

                    holder.b1.setTextColor(Color.RED);
your_number.add(list.get(position).Rno);
                }
                else if(holder.b1.getText().equals("Absent"))
                {
                    holder.b1.setText("Present");
                    //list.get(position).put("Attendance", "Present");
                    holder.b1.setTextColor(Color.GREEN);
your_number.remove(list.get(position).Rno);
                }
                String rno1=  String.valueOf(holder.tv1.getText());
                name=String.valueOf(holder.tv2.getText());
            }

        });

And finally in your getView()

if (your_number.contains(list.get(position).Rno)){
//set your text
}else{
//Set your text
}
Amsheer
  • 7,046
  • 8
  • 47
  • 81
0

You need to keep track of the buttons you have clicked, and set the colours accordingly to that.

Remember that your view is being reused so if you have a red button next time your view is reused (by scrolling up/down) it will still be red.

Use a List or some kind of container to store the clicked items and in your getView set the right values.

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
0

As the same views are used for multiple times if you change the color for one item it will be applied to the another item with same reference. To avoid this when you click on a button get the position of the item and in getView() method based on the position change the text color.

EDIT

Add these lines to your getView() method before holder.b1.setOnClickListener() line

if(holder.b1.getText().equals("Present"))
 {
      holder.b1.setTextColor(Color.GREEN);
 }
 else if(holder.b1.getText().equals("Absent"))
 {
      holder.b1.setTextColor(Color.RED);
 }

I didn't executed your code...but it may works and also remove comment for line list.get(position).put("Attendance", "Absent"); and list.get(position).put("Attendance", "Present"); in onClick() method.

Chandrakanth
  • 3,711
  • 2
  • 18
  • 31