在Android中为动态创建的复选框保存复选框状态

时间:2014-11-13 19:18:22

标签: java android listview checkbox android-listview

我创建了listview,其中包含该设备上安装的所有应用程序。 当向用户显示已安装的应用程序时,每个应用程序都有其相应的复选框。 用户单击多个复选框并保存相应的应用程序名称。

我想要的是,当用户再次启动应用程序时,应该检查用户保存的应用程序的复选框。

我该怎么做?谁能帮我? 提前谢谢。

这是代码

AppList.java

public class AppList extends Activity {
AppInfoAdapter adapter ;
AppInfo app_info[] ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);
    final ListView listApplication = (ListView)findViewById(R.id.listApplication);
    final Button b= (Button) findViewById(R.id.button001);
    final Context context = null;
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            StringBuilder result = new StringBuilder();
            for(int i=0;i<app_info.length;i++)
            {
                if(adapter.mCheckStates.get(i)==true)
                {

                                   result.append(app_info[i].applicationName).append(",");
                   // result.append(",");
                }

            }

            String playlists = result.toString();
            StoringClas.writeApps(AppList.this, playlists);
            //String[] po = playlists.split(",");
            //b.setText(po[2]+"o");

        }

    });

    ApplicationInfo applicationInfo = getApplicationInfo();
    PackageManager pm = getPackageManager();
    List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
    pInfo.addAll(pm.getInstalledPackages(0));
    app_info = new AppInfo[pInfo.size()];

    int counter = 0;
    for(PackageInfo item: pInfo){
        try{

            applicationInfo = pm.getApplicationInfo(item.packageName, 1);

                     app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), 
                             String.valueOf(pm.getApplicationLabel(applicationInfo)));


            System.out.println(counter);

        }
        catch(Exception e){
             System.out.println(e.getMessage());
        }

        counter++;
    }

   adapter = new AppInfoAdapter(this, R.layout.item_row, app_info);
    listApplication.setAdapter(adapter);

}}

AppInfoAdapter.java

public class AppInfoAdapter extends ArrayAdapter<AppInfo> implements CompoundButton.OnCheckedChangeListener
{  SparseBooleanArray mCheckStates; 

Context context;
int layoutResourceId;
AppInfo  data[] = null;

public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){
    super(context, layoutResourceId,data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
    mCheckStates = new SparseBooleanArray(data.length);
}

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

    View row = convertView;
    AppInfoHolder holder= null;

    if (row == null){
        LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new AppInfoHolder();

        holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
        holder.txtTitle = (TextView) row.findViewById(R.id.textView001);
        holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1);

        row.setTag(holder);

    }
    else{
        holder = (AppInfoHolder)row.getTag();
    }


    AppInfo appinfo = data[position];
    holder.txtTitle.setText(appinfo.applicationName);
    holder.imgIcon.setImageDrawable(appinfo.icon);
   // holder.chkSelect.setChecked(true);
   // data[2].applicationName="kj";
  /*   String mm=StoringClas.readApps(context);
   String[] po= mm.split(",");
    for(int k=0;k<po.length;k++){
        for(int j=0;j<data.length;j++){
        if(po[k].contains(data[j].applicationName))
        {
            holder.chkSelect.setChecked(mCheckStates.get(position, true));
        }

    }}*/



    holder.chkSelect.setTag(position);
    holder.chkSelect.setChecked(mCheckStates.get(position, false));
    holder.chkSelect.setOnCheckedChangeListener(this);
    return row;

}
public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));

}
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {

     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}
static class AppInfoHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    CheckBox chkSelect;}}

AppInfo.java

public class AppInfo {
public Drawable icon;
public String applicationName;

public AppInfo(){
    super();
}

public AppInfo(Drawable icon, String applicationName){
    super();
    this.icon = icon;
    this.applicationName = applicationName;
}}

1 个答案:

答案 0 :(得分:0)

Android中有很多方法可以在应用启动之间保存信息。 Android网站提供了不同内置方式的精彩概述:Saving Data

选择保存方式(例如SharedPreference或数据库)后,当您的应用启动时,您可以检查保存的信息并将其显示给用户。