修复code自定义的ListView [英] Fix Code custom ListView

查看:113
本文介绍了修复code自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:

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;
}

然后我用它为一个ListView创建一个适配器。当我点击按钮NDA it'Background转向nutdapanchon和SDA是真,但后来我滚动ListView和回
我的NDA背景是回到nutdapan和SDA是FLASE。为什么?如何解决这个问题?帮我请?

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?

推荐答案

列表视图元素回收时会自动出画面这样试试下面的方法来解决问题: -

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

<一个href=\"http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=9\" rel=\"nofollow\">http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=9

这篇关于修复code自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆