I have a listview
of all install app
. I am using custom adapter I save the state of my checkboxes
(checked or unchecked)with sharedprefrences
when user exits the application so that I can reload this state when the application restarts . i have a button in mainactivity
which show the selected value of checkbox
which is checked its working fine but now problem is this when app restart checkbox
state is reloaded but when i click on button its show null value but checkbox
is selected and it show null value what is the issue please help me.here is my code
public class Listadapter extends BaseAdapter {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
CheckBox checkBox;
boolean index[];
boolean[] itemChecked;
ApplicationInfo entry;
String[] itempkg;
public Listadapter(Context applicationContext, List<ApplicationInfo> installedApplication, PackageManager packageManager) {
//super(applicationContext,textViewResourceId,installedApplication);
super();
this.mContext = applicationContext;
this.mListAppInfo = installedApplication;
index = new boolean[installedApplication.size()];
this.mPackManager = packageManager;
for (int i = 0; i < installedApplication.size(); i++) {
checkList.add(false);
itemChecked = new boolean[installedApplication.size()];
itempkg = new String[installedApplication.size()];
}
}
public void setItemChecked(String[] items) {
itempkg= items;
}
public void setItemCheckedd(boolean[] itemss) {
itemChecked = itemss;
}
private class ViewHolder {
ImageView ivAppIcon;
TextView tvAppName;
TextView tvPkgName;
CheckBox checkBox;
}
@Override
public int getCount() {
return mListAppInfo.size();
//return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
}
@Override
public Object getItem(int position) {
// index = new boolean[mListAppInfo.size()];
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// get the selected entry
final ViewHolder holder;
// LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
// reference to convertView
holder.tvAppName = (TextView) convertView
.findViewById(R.id.textView1);
holder.tvPkgName = (TextView) convertView
.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.ivAppIcon = (ImageView) convertView
.findViewById(R.id.imageView);
convertView.setTag(holder);
// holder.ck1.setTag(packageList.get(position));
}
else {
holder = (ViewHolder) convertView.getTag();
}
entry = mListAppInfo.get(position);
holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
holder.tvAppName.setText(entry.loadLabel(mPackManager));
holder.tvPkgName.setText(entry.packageName);
holder.checkBox.setChecked(false);
if (itemChecked[position])
holder.checkBox.setChecked(true);
else
holder.checkBox.setChecked(false);
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkBox.isChecked())
{
itemChecked[position] = true;
}else {
itemChecked[position] = false;
} }
});
return convertView;
}
}
here is mainactivity
public class MainActivity extends Activity implements OnItemClickListener {
ListView apps;
PackageManager packageManager;
ArrayList<String> checkedValue;
Button bt1;
private ShareActionProvider mShareAction;
ApplicationInfo pi = new ApplicationInfo();
public static final String PREF_PROFILE3 = "pref_profile3";
public static final String PROFILE3 = "profile3";
Context context = this;
SharedPreferences pkg;
String currentApp = "NULL";
CheckBox cb;
Listadapter Adapter = null;
boolean[] itemCheckedd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemCheckedd = new boolean[Utilities.getInstalledApplication(this).size()];
bt1 = (Button) findViewById(R.id.button1);
apps = (ListView) findViewById(R.id.listView1);
apps.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
packageManager = getPackageManager();
checkedValue = new ArrayList<String>();
final String[] items = new String[Utilities.getInstalledApplication(this).size()];
final boolean[] itemss = new boolean[Utilities.getInstalledApplication(this).size()];
SharedPreferences preferences = context.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
for (int i = 0; i < Utilities.getInstalledApplication(this).size(); ++i) {
itemss[i] = preferences.getBoolean("checkbox_" + i, false);
}
for (int i = 0; i < Utilities.getInstalledApplication(this).size(); ++i) {
items[i] = preferences.getString("pkgname" + i, "");
Toast.makeText(MainActivity.this, "all" + items[i], Toast.LENGTH_LONG).show();
}
//
// Listadapter Adapter = new Listadapter(this, Utilities.getInstalledApplication(this), packageManager);
Adapter = new Listadapter(this, Utilities.getInstalledApplication(this), packageManager);
// Adapter.setItem(pkg);
Adapter.setItemChecked(items);
Adapter.setItemCheckedd(itemss);
apps.setAdapter(Adapter);
apps.setOnItemClickListener(this);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "all"+":" + checkedValue, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
cb = (CheckBox) v.findViewById(R.id.checkBox1);
TextView tv = (TextView) v.findViewById(R.id.textView);
pi = (ApplicationInfo) arg0.getItemAtPosition(position);
cb.performClick();
if (cb.isChecked()) {
checkedValue.add(tv.getText().toString());
itemCheckedd[position] = true;
// Toast.makeText(MainActivity.this, "all" + position, Toast.LENGTH_LONG).show();
SharedPreferences preferences = context.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean("checkbox_" + position, true);
edit.putString("pkgname" + position, (tv.getText().toString()));
edit.commit();
} else if (!cb.isChecked()) {
checkedValue.remove(tv.getText().toString());
SharedPreferences preferences = context.getSharedPreferences("YOUR_APP_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean("checkbox_" + position, false);
edit.putString("pkgname" + position, "");
edit.commit();
}
}
}
Aucun commentaire:
Enregistrer un commentaire