2011-04-18 66 views
0

我想恢復我的複選框的以前的狀態。我在我的活動中有近20個複選框,我想要的是當我重新啓動我的應用程序時,如果在退出應用程序之前檢查它,則應檢查所有複選框。由於我使用自定義適配器,所以我發現它很難實現。Android - 重新啓動應用後無法恢復複選框狀態?

這裏是我的適配器的代碼 -

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

請請幫助!!!!

+0

添加一些調試日誌記錄到你的load()方法來縮小問題。確保您獲得了您對sharedPreferences的期望。 – levis501 2011-04-18 19:10:05

回答