Android - 重新启动应用程序后无法恢复复选框状态?

发布于 2024-11-01 19:22:01 字数 5324 浏览 3 评论 0原文

我正在尝试恢复复选框之前的状态。我的 Activity 中有近 20 个复选框,我想要的是当我重新启动应用程序时,如果在退出应用程序之前检查了所有复选框,则应检查所有复选框。由于我使用的是自定义适配器,所以我发现它很难实现。

这是我的适配器的代码 -

public class IconAdapter extends BaseAdapter
{
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
public ArrayList<Boolean> itemChecked = null;
public ArrayList<String> itemSelected = new ArrayList<String>();
public ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();
//TextView textView;
CheckBox cb;
//ImageView imageView;
public CompleteTaskManager ctm = new CompleteTaskManager();

public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
    activity = a;
    listItems = items;
    data = items.toArray();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = a.getPackageManager();
    if(itemChecked==null){
        itemChecked = new ArrayList<Boolean>();
    for(int i = 0; i < items.size(); i++)
    {
        itemChecked.add(i,false);
    }}
    for(int i = 0; i < items.size(); i++)
    {
        itemSelected.add(i," ");
    }
     for(int i = 0; i < items.size(); i++)
    {
        cb  = new CheckBox(a);
        ctv.add(i,cb);
    }
}

public int getCount() {
    return listItems.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView textView;
    public ImageView imageView;
    public CheckBox checkBox;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    final ViewHolder holder;
    if(convertView==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check); holder.checkBox.setChecked(itemChecked.get(position));
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();

    }

    String s = data[position].toString();
    String[] tokens = s.split(",");
    String[] mToken = tokens[0].split("=");
    String taskName = mToken[1];
    holder.textView.setText(taskName);
    String[] mTokens = tokens[1].split("=");
    final String pkgName =  mTokens[1].substring(0, (mTokens[1].length() - 1));

    holder.checkBox.setTag(position);
//this is how i am trying to restore the checked checkboxes.
    **for(int i = 0; i < itemSelected.size(); i++)
    {
        if(itemSelected.contains(pkgName))
        {
            holder.checkBox.setChecked(true);
        }
    }**
    ctv.set(position,holder.checkBox);
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton button, boolean b) {
            Integer posClicked = (Integer)button.getTag();
            if(b)
            {
                itemChecked.set(posClicked, true);
                itemSelected.set(posClicked, pkgName);
            }
            else
            {
                itemChecked.set(posClicked,false);
                itemSelected.set(posClicked, " ");
            }
        }
    });

   holder.checkBox.setChecked(itemChecked.get(position));

    try{
        Drawable icon =   pm.getApplicationIcon(pkgName);
        holder.imageView.setImageDrawable(icon);
    }
    catch (PackageManager.NameNotFoundException ne)
    {

    }
     row.setId(position);
     return row;
}

public boolean isChecked(int position)
{
    return itemChecked.get(position);
}
public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void removeItem(int position)
{
    listItems.remove(position);
}
}

这是我尝试恢复我的 pkgNames 的代码。我选择 pkgName 是因为我注意到位置可能会被打乱,因为我的列表是动态的,但与每个项目相关的 pkgName 将保持不变。

@Override
public void onPause()
{
    super.onPause();
    save(notes.itemSelected);
}
@Override
public void onResume()
{
    super.onResume();
    ArrayList<String> checkOld = load();

    for (int i = 0 ; i < checkOld.size(); i++)
    {
        notes.itemSelected = checkOld;
    }
}
@Override
public void onRestart()
{
    super.onRestart();
    ArrayList<String> checkOld = load();

    for (int i = 0 ; i < checkOld.size(); i++)
    {
        notes.itemSelected = checkOld;
    }
}

private void save(final ArrayList<String> isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
 for(Integer i = 0; i < isChecked.size(); i++)
 {
     editor.putString(i.toString(), isChecked.get(i));
 }
editor.commit();
}

private ArrayList<String> load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    ArrayList<String> reChecked = new ArrayList<String>();
    for(Integer i = 0; i < notes.getCount(); i++)
    {
        reChecked.add(i, sharedPreferences.getString(i.toString(), " "));
    }
    return reChecked;
}
}

请帮忙!!!!

I am trying to restore the previous state of my Checkboxes. I have nearly 20 checkbox in my Activity and what i want is when i restart my app, all the checkbox should be checked if it was checked before exiting the app. As i am using a custom adapter so i found it very difficult to achieve.

Here is my adapter's code -

public class IconAdapter extends BaseAdapter
{
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
public ArrayList<Boolean> itemChecked = null;
public ArrayList<String> itemSelected = new ArrayList<String>();
public ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();
//TextView textView;
CheckBox cb;
//ImageView imageView;
public CompleteTaskManager ctm = new CompleteTaskManager();

public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
    activity = a;
    listItems = items;
    data = items.toArray();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = a.getPackageManager();
    if(itemChecked==null){
        itemChecked = new ArrayList<Boolean>();
    for(int i = 0; i < items.size(); i++)
    {
        itemChecked.add(i,false);
    }}
    for(int i = 0; i < items.size(); i++)
    {
        itemSelected.add(i," ");
    }
     for(int i = 0; i < items.size(); i++)
    {
        cb  = new CheckBox(a);
        ctv.add(i,cb);
    }
}

public int getCount() {
    return listItems.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView textView;
    public ImageView imageView;
    public CheckBox checkBox;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    final ViewHolder holder;
    if(convertView==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check); holder.checkBox.setChecked(itemChecked.get(position));
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();

    }

    String s = data[position].toString();
    String[] tokens = s.split(",");
    String[] mToken = tokens[0].split("=");
    String taskName = mToken[1];
    holder.textView.setText(taskName);
    String[] mTokens = tokens[1].split("=");
    final String pkgName =  mTokens[1].substring(0, (mTokens[1].length() - 1));

    holder.checkBox.setTag(position);
//this is how i am trying to restore the checked checkboxes.
    **for(int i = 0; i < itemSelected.size(); i++)
    {
        if(itemSelected.contains(pkgName))
        {
            holder.checkBox.setChecked(true);
        }
    }**
    ctv.set(position,holder.checkBox);
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton button, boolean b) {
            Integer posClicked = (Integer)button.getTag();
            if(b)
            {
                itemChecked.set(posClicked, true);
                itemSelected.set(posClicked, pkgName);
            }
            else
            {
                itemChecked.set(posClicked,false);
                itemSelected.set(posClicked, " ");
            }
        }
    });

   holder.checkBox.setChecked(itemChecked.get(position));

    try{
        Drawable icon =   pm.getApplicationIcon(pkgName);
        holder.imageView.setImageDrawable(icon);
    }
    catch (PackageManager.NameNotFoundException ne)
    {

    }
     row.setId(position);
     return row;
}

public boolean isChecked(int position)
{
    return itemChecked.get(position);
}
public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void removeItem(int position)
{
    listItems.remove(position);
}
}

and here is code in which i am trying to restore my pkgNames. I picked pkgName because i have noticed position could be shuffled as my list is dynamic but pkgName related to each item will stay same.

@Override
public void onPause()
{
    super.onPause();
    save(notes.itemSelected);
}
@Override
public void onResume()
{
    super.onResume();
    ArrayList<String> checkOld = load();

    for (int i = 0 ; i < checkOld.size(); i++)
    {
        notes.itemSelected = checkOld;
    }
}
@Override
public void onRestart()
{
    super.onRestart();
    ArrayList<String> checkOld = load();

    for (int i = 0 ; i < checkOld.size(); i++)
    {
        notes.itemSelected = checkOld;
    }
}

private void save(final ArrayList<String> isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
 for(Integer i = 0; i < isChecked.size(); i++)
 {
     editor.putString(i.toString(), isChecked.get(i));
 }
editor.commit();
}

private ArrayList<String> load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    ArrayList<String> reChecked = new ArrayList<String>();
    for(Integer i = 0; i < notes.getCount(); i++)
    {
        reChecked.add(i, sharedPreferences.getString(i.toString(), " "));
    }
    return reChecked;
}
}

Please Please HELP!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文