vendredi 10 mars 2017

Saving State Of Dynamatically Created Checkbox

I am currently trying to make a simple task killer for learning purpose, I created a list view that show all user installed apps with checkbox, so user could select with apps should be killed when screen off.

But I am stuck at how to save the checkbox state and get the checked app's package name

I have search stack overflow a long time but still don't know how to do it, please help me, thanks

below is the code

AppAdapter.java

public class AppAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private List<AppList> listStorage;

public AppAdapter(Context context, List<AppList> customizedListView) {
    layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listStorage = customizedListView;
}

@Override
public int getCount() {
    return listStorage.size();
}

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

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

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

    final ViewHolder listViewHolder;
    if(convertView == null){
        listViewHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);

        listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
        listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
        listViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
        convertView.setTag(listViewHolder);

        listViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listViewHolder.checkBox.setSelected(listStorage.get(position).isSelected());
            }
        });
    }else{
        listViewHolder = (ViewHolder)convertView.getTag();
    }
    listViewHolder.textInListView.setText(listStorage.get(position).getName());
    listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
    listViewHolder.checkBox.setChecked(listStorage.get(position).isSelected());

    return convertView;
}

static class ViewHolder{

    TextView textInListView;
    ImageView imageInListView;
    CheckBox checkBox;
}

}

Applist.java

public class AppList {

private String name;
Drawable icon;
private static final String PREFERENCES_NAMESPACE = "checkboxes_states";
boolean selected = false;
private SharedPreferences mSettings;
private SharedPreferences.Editor mEditor;

public AppList(Context context, String name, Drawable icon) {
    this.name = name;
    this.icon = icon;
    mSettings = context.getSharedPreferences(PREFERENCES_NAMESPACE, 0);
    mEditor = mSettings.edit();
    setSelected(mSettings.getBoolean(name, selected));
}

public String getName() {
    return name;
}

public Drawable getIcon() {
    return icon;
}
public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    if(this.selected != selected) { // update if changed
        mEditor.putBoolean(getName(), selected);
        mEditor.apply();
        this.selected = selected;
    }
}

}

MainActivity.java

public class MainActivity extends Activity {

Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = getApplicationContext();

    ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);

    List<AppList> installedApps = getInstalledApps();
    AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
    userInstalledApps.setAdapter(installedAppAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private List<AppList> getInstalledApps() {
    List<AppList> res = new ArrayList<AppList>();
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        if ((isSystemPackage(p) == false)) {
            String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
            Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
            res.add(new AppList(context, appName, icon));
        }
    }
    return res;
}

private boolean isSystemPackage(PackageInfo pkgInfo) {
    return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}

}

Thanks in advanced.




Aucun commentaire:

Enregistrer un commentaire