Android : save listview item even after exit

on Thursday, August 7, 2014


hello friends i have listview that contain all install app and a checkbox.my problem is that when i check item using checkbox it is not remain permanently ie if i check some item using checkbox and exit app and then run app it should show all previous checked item.


here my package ApkListActivity.java



import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.ibc.android.demo.appslist.adapter.ApkAdapter;
import com.ibc.android.demo.appslist.app.AppData;

public class ApkListActivity extends Activity {

PackageManager packageManager;
ListView apkList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);

List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

/* To filter out System apps */
for (PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if (!b) {
packageList1.add(pi);
}
}

Collections.sort(packageList1, new Comparator<PackageInfo>() {
public int compare(PackageInfo o1, PackageInfo o2) {
return o1.applicationInfo
.loadLabel(getPackageManager())
.toString()
.compareTo(
o2.applicationInfo.loadLabel(
getPackageManager()).toString());
}
});
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));
apkList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long row) {
PackageInfo packageInfo = (PackageInfo) parent
.getItemAtPosition(position);
AppData appData = (AppData) getApplicationContext();
appData.setPackageInfo(packageInfo);

// Intent appInfo = new Intent(getApplicationContext(),
// ApkInfo.class);
// startActivity(appInfo);
PackageInfo country = (PackageInfo) parent
.getItemAtPosition(position);
Toast.makeText(
getApplicationContext(),
"Clicked on Row: "
+ country.applicationInfo.loadLabel(
getPackageManager()).toString(),
Toast.LENGTH_LONG).show();

}
});

}

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

}


and this is my ApkAdapter.java



import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ibc.android.demo.appslist.activity.R;

public class ApkAdapter extends BaseAdapter {

List<PackageInfo> packageList = null;
Activity context;
Context context2;
PackageManager packageManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();

boolean selected = false;

// List<ApplicationInfo> packageList = new ArrayList<ApplicationInfo>();

public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;

for (int i = 0; i < packageList.size(); i++) {

checkList.add(selected);

}

}

public int getCount() {
return ((null != packageList) ? packageList.size() : 0);
// return packageList.size();
}

public PackageInfo getItem(int position) {
return ((null != packageList) ? packageList.get(position) : null);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;

if (null == view) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.apklist_item, null);
// holder = new ViewHolder();

}
PackageInfo data = packageList.get(position);
if (null != data) {

TextView apkName = (TextView) view.findViewById(R.id.appname);

ImageView apkicon = (ImageView) view.findViewById(R.id.icon_image);
TextView packageName = (TextView) view
.findViewById(R.id.packagename);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.setTag(Integer.valueOf(position)); // set the tag so we can
// identify the correct
// row in the listener
checkBox.setChecked(checkList.get(position)); // set the status
// as
// we stored it
checkBox.setOnCheckedChangeListener(mListener); // set the listener
PackageInfo packageInfo = (PackageInfo) getItem(position);

Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
;
apkicon.setImageDrawable(appIcon);
apkName.setText(appName);
packageName.setText(packageInfo.packageName);

}

return view;

}

OnCheckedChangeListener mListener = new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkList.set((Integer) buttonView.getTag(), isChecked);
}
};

}

0 comments:

Post a Comment