i have a custom listview with textview and checkbox . i want to save the checkbox state in sharedprefrences and get and set it again adaptor when activity open . i have also done this but issue is this i save the checkbox checked position in sharedprefrences and get it . when listview item postion change then my checked checkbox postion got wroung checked how can i handle this.here is my adaptor code.
public class Listadapter extends BaseAdapter {
CheckBox checkBox;
boolean index[];
boolean[] itemChecked;
ResolveInfo entry;
String[] itempkg;
private Context mContext;
private List<ResolveInfo> mListAppInfo;
private PackageManager mPackManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
public Listadapter(Context applicationContext, List<ResolveInfo> 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()];
}
}
@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)
{
final ViewHolder holder;
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);
} 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.activityInfo.packageName);
holder.checkBox.setChecked(false);
if (itemChecked[position])
holder.checkBox.setChecked(true);
else
holder.checkBox.setChecked(false);
final View finalConvertView = convertView;
holder.checkBox.setOnClickListener(new View.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;
}
public void setItemChecked5(boolean[] items5) {
itemChecked = items5;
}
private class ViewHolder
{
private ImageView ivAppIcon;
private TextView tvAppName;
private TextView tvPkgName;
private CheckBox checkBox;
}
}
here is my activity code.
public class Profile5Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView apps5;
PackageManager packageManager5;
static ArrayList<String> checkedValue5;
Button bt5;
ResolveInfo pi5 = new ResolveInfo();
Context context = this;
static String currentApp5 = "NULL";
CheckBox cb5;
Listadapter Adapter5 = null;
boolean[] itemChecked5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile5);
itemChecked5 = new boolean[AllApps.getInstalledApplication(this).size()];
//needPermissionForBlocking(MainActivity.mainContext);
apps5 = (ListView) findViewById(R.id.list5);
packageManager5 = getPackageManager();
final boolean[] items5 = new boolean[AllApps.getInstalledApplication(this).size()];
SharedPreferences preferences5 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
for (int i = 0; i < AllApps.getInstalledApplication(this).size(); ++i) {
items5[i] = preferences5.getBoolean("checkbox_5" + i, false);
}
Adapter5 = new Listadapter(this, AllApps.getInstalledApplication(this), packageManager5);
Adapter5.setItemChecked5(items5);
apps5.setAdapter(Adapter5);
apps5.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, final View v, final int position, long arg3)
{
// TODO Auto-generated method stub
CheckBox cb5 = (CheckBox) v.findViewById(R.id.checkBox1);
final TextView tv5 = (TextView) v.findViewById(R.id.textView);
final TextView tvv5 = (TextView) v.findViewById(R.id.textView1);
cb5.performClick();
if (cb5.isChecked()) {
checkedValue5.add(tv5.getText().toString());
itemChecked5[position] = true;
String pname = (tvv5.getText().toString());
SharedPreferences preferences1 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences1.edit();
edit.putBoolean("checkbox_5" + position, true);
edit.commit();
} else if (!cb5.isChecked()) {
checkedValue5.remove(tv5.getText().toString());
String pname = (tvv5.getText().toString());
SharedPreferences preferences1 = context.getSharedPreferences("YOUR_APP_NAME5", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences1.edit();
edit.putBoolean("checkbox_5" + position, false);
edit.commit();
}
}
}
all things working fine in this code until the list item position change when list item postion changed then i got wroung checked value please help me with some code or edit my code where i made mistake i really thankful to you.
Aucun commentaire:
Enregistrer un commentaire