I have created the listview which consists of all the application installed on that device. When the installed applications are displayed to the user, each application have its corresponding checkbox. User clicks on the multiple checkboxes and the corresponding application names are saved.
What I want is, when user again starts the application, the checkboxes of the applications which are saved by user should be checked.
How can I do this ? Can anyone help me? Thanks in advance.
Here is the code
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;
}}