0

i have a class:

public class custumkiemtra extends ArrayAdapter<Cauhoi>{
    Context context;
    int resource;
    List<Cauhoi> ch;
    public custumkiemtra(Context context, int resource, List<Cauhoi> ch) {
        super(context, resource, ch);
        this.context = context;
        this.resource = resource;
        this.ch = ch;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final RadioButton SDA,SDB,SDC,SDD;

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View viewrow = inflater.inflate(R.layout.custumkiemtra,parent,false);

        TextView ndch = (TextView)viewrow.findViewById(R.id.cauhoi);
        final Button ndA = (Button)viewrow.findViewById(R.id.DA);
        final Button ndB = (Button)viewrow.findViewById(R.id.DB);
        final Button ndC = (Button)viewrow.findViewById(R.id.DC);
        final Button ndD = (Button)viewrow.findViewById(R.id.DD);

        SDA = (RadioButton) viewrow.findViewById(R.id.SUADA);
        SDB = (RadioButton) viewrow.findViewById(R.id.SUADB);
        SDC = (RadioButton) viewrow.findViewById(R.id.SUADC);
        SDD = (RadioButton) viewrow.findViewById(R.id.SUADD);
        SDA.setEnabled(false);
        SDB.setEnabled(false);
        SDC.setEnabled(false);
        SDD.setEnabled(false);
        final Cauhoi getch = ch.get(position);
        ndch.setText("Cau "+ String.valueOf(getch.get_id()) +": " + getch.getNoidung_cauhoi());
        ndA.setText(getch.getNoidung_traloi1());
        ndB.setText(getch.getNoidung_traloi2());
        ndC.setText(getch.getNoidung_traloi3());
        ndD.setText(getch.getNoidung_traloi4());
        ndA.setTag(getch.get_id());
        ndB.setTag(getch.get_id());
        ndC.setTag(getch.get_id());
        ndD.setTag(getch.get_id());
        ndA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ndA.setBackgroundResource(R.drawable.nutdapanchon);
                ndB.setBackgroundResource(R.drawable.nutdapan);
                ndC.setBackgroundResource(R.drawable.nutdapan);
                ndD.setBackgroundResource(R.drawable.nutdapan);
                SDA.setEnabled(true);
                SDB.setEnabled(false);
                SDC.setEnabled(false);
                SDD.setEnabled(false);

            }
        });
return viewrow;
}

and then i use it for create a adapter for a Listview. When I click on Button NdA it'Background turn to "nutdapanchon" and SDA is "true" but then i scroll ListView and back my ndA background is back to "nutdapan" and SDA is "flase". Why? How can fix this? Help me pls?

GoldenICE
  • 1
  • 1

1 Answers1

-1

Listview element recycle automatically when out of screen so try below way to solve your problem:-

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

         View vi = convertView;
         ViewHolder holder;

         if(convertView==null){

             /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
             vi = inflater.inflate(R.layout.tabitem, null);

             /****** View Holder Object to contain tabitem.xml file elements ******/

             holder = new ViewHolder();
             holder.text = (TextView) vi.findViewById(R.id.text);
             holder.text1=(TextView)vi.findViewById(R.id.text1);
             holder.image=(ImageView)vi.findViewById(R.id.image);

            /************  Set holder with LayoutInflater ************/
             vi.setTag( holder );
         }
         else 
             holder=(ViewHolder)vi.getTag();

See below link :-

http://www.vogella.com/tutorials/AndroidListView/article.html

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=9

duggu
  • 37,851
  • 12
  • 116
  • 113
  • I add holder like you it's change when i scroll list view but i found a problem like this: when i click on one of 4 buton (ex: ndA) in list[0] it's change backgorund but in list[6] ndA too. why? – GoldenICE Jan 05 '15 at 23:55