如何保存复选框的状态,而在ListView中滚动? [英] How to save state of CheckBox while scrolling in ListView?

查看:96
本文介绍了如何保存复选框的状态,而在ListView中滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的ListView自定义适配器,所以我的ListView包括复选框,但问题是,当我向下滚动,选中的复选框取消选中,我如何与这个code呢?

更新

我的自定义适配器:

 公共类AppsArrayAdapter扩展ArrayAdapter<串GT; {
    私人最终上下文的背景下;    私人列表<整数GT; checkedItems;    私人最终字符串DefaultApps =def_apps;    公共AppsArrayAdapter(上下文的背景下,的String []值列表与LT;整数GT; checkedItems){// XXX
        超(背景下,R.layout.apps,价值观);
        this.context =背景;
        this.checkedItems = checkedItems; // XXX
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        共享preferences我的prefsApps = context.getShared preferences(我的prefsApps,Context.MODE_PRIVATE);
        字符串prefNameDefaultApps =我的prefsApps.getString(DefaultApps,);
        字符串prefNameDefaultAppsVer =我的prefsApps.getString(DefaultAppsVer,);
        LayoutInflater吹气=(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        查看rowView = inflater.inflate(R.layout.apps,父母,假);
        TextView的text_apps =(TextView中)rowView.findViewById(R.id.text_apps);
        复选框检查=(复选框)rowView.findViewById(R.id.check_apps);
        text_apps.setText(prefNameDefaultApps.split(\\ n)[位置]);        check.setChecked(checkedItems.contains((整数)位置));
        返回rowView;
    }
}

和我的活动:

 公共类AppsActivity扩展ListActivity {
    私人最终字符串DefaultApps =def_apps;    私人列表<整数GT; checkedItems =新的ArrayList<整数GT;();
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        AppsArrayAdapter适配器=新AppsArrayAdapter(这一点,prefNameDefaultApps.split(\\ n),checkedItems);
        setListAdapter(适配器);
}
}
@覆盖
    公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
       如果(checkedItems.contains((整数)位置))checkedItems.remove((整数)位置);
       其他checkedItems.add((整数)位置);
    }


解决方案

你应该使用
MyAdapter newad =(MyAdapter)adapter.getAdapter();
        newad.notifyDataSetChanged();
这code在onitem点击方法
和MyAdapter是类适配器,使新对象像这样的
在适配器类,你应该有写一行

在LayoutInflater;
在=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
适配器构造简单

I have used ListView with custom adapter, so my ListView includes CheckBox, but problem is that when I scroll down, checked CheckBoxes uncheck, how can I do it with this code?

UPDATE

My custom adapter:

    public class AppsArrayAdapter extends ArrayAdapter<String> {
    private final Context context;

    private List<Integer> checkedItems;

    private final String DefaultApps = "def_apps";

    public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
        super(context, R.layout.apps, values);
        this.context = context;
        this.checkedItems = checkedItems;//XXX
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
        String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
        String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");


        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.apps, parent, false);
        TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
        CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
        text_apps.setText(prefNameDefaultApps.split("\n")[position]);

        check.setChecked(checkedItems.contains((Integer) position));
        return rowView;
    }   
}

And my Activity:

    public class AppsActivity extends ListActivity {
    private final String DefaultApps = "def_apps";

    private List<Integer> checkedItems = new ArrayList<Integer>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems);
        setListAdapter(adapter);
}
}
@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
       if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position);
       else checkedItems.add((Integer) position);
    }

解决方案

you should use MyAdapter newad=(MyAdapter)adapter.getAdapter(); newad.notifyDataSetChanged(); this code in onitem click method and MyAdapter is class of Adapter and make new object like this one and in adapter class you should have to write a line

LayoutInflater in; in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); in adapter constructor simple

这篇关于如何保存复选框的状态,而在ListView中滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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