【发布时间】:2014-08-15 20:02:41
【问题描述】:
在过去的两周里,我一直在努力寻找一种解决方案,以便在我退出并打开应用程序时将我的复选框值存储在我的每个列表视图项旁边。本质上,我有一个自定义 BaseAdapter 类,它使用用户手机上安装的应用程序填充列表视图,并在每个安装的应用程序旁边有一个复选框。当我向下滚动复选框时,状态保持不变;但是,当我单击返回或退出并返回时,它不会保持不变。
我几乎以所有可能的方式使用了 SharedPreferences。我的最后一种方法是使用 for 循环为每个循环创建一个 SharedPreference 并在 for 循环中恢复布尔值,并在 if 和 else 语句中存储布尔值 true 和 false(分别用于检查和未检查)。
这是我使用 SharedPreferences 的 BaseAdapter 类的代码:
package com.ibc.android.demo.appslist.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.spicycurryman.getdisciplined10.app.R;
import java.util.List;
//
public class ApkAdapter extends BaseAdapter {
//Pastebin link: http://pastebin.com/LGRicg4U , http://pastebin.com/c4WfmhMK , http://pastebin.com/gFuuM4dY, http://pastebin.com/4Q7EP9G4
// http://pastebin.com/Te2g072w, http://pastebin.com/NLT5iUiA ,
SharedPreferences sharedPrefs;
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
String PACKAGE_NAME;
public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
itemChecked = new boolean[packageList.size()];
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
TextView packageName;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.installed_apps, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.appname);
holder.ck1= (CheckBox)convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
//holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
// ViewHolder holder = (ViewHolder) convertView.getTag();
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
// Make sure to define it again!
PACKAGE_NAME = packageInfo.packageName;
final String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 80, 80);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
Log.d("just loaded??", PACKAGE_NAME);
Log.d("just loaded 2?", appName+position);
// CHANGE UP EVERYTHING! MAKE THIS SHIT WORK, TIGGA!
for(int i= 0; i<packageList.size(); i++){
sharedPrefs = context.getSharedPreferences(String.valueOf(i), Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean(String.valueOf(i),false));
}
holder.ck1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = context.getSharedPreferences(String.valueOf(position), Context.MODE_PRIVATE).edit();
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
holder.ck1.setChecked(true);
Log.i("This is", " checked: " + position);
editor.putBoolean(String.valueOf(position), true);
Log.d("put true", appName+position);
editor.apply();
} else {
itemChecked[position] = false;
holder.ck1.setChecked(false);
Log.i("This is", " not checked: " + position);
editor.putBoolean(String.valueOf(position), false);
Log.d("put false", appName+position);
editor.apply();
}
}
});
return convertView;
}
}
我还创建了一个 DatabaseHandler 类和 Object 类,用于使用 SQLite 数据库来存储我的复选框值,而不是在使用 SharedPreferences 存储它失败后。
这里是 pastebin 链接,所以我不会阻塞帖子:
DatabaseHandler 类:http://pastebin.com/NzKhBiZ3
对象类:http://pastebin.com/Jp3BLXba
我知道有很多关于使用自定义适配器/列表视图以及使用 SQLite 和 SharedPreferences 保存复选框和按钮状态等的帖子、博客和链接,但我向你保证,我已经完成了数周的研究,但我无法找到适合我的具体情况的东西。
一些例子:
Saving State of the checkbox in a custom list view with Checkboxes
http://blog.csdn.net/qu213/article/details/9289349
在我的特定情况下,最佳选项是什么?在我的情况下,我将如何生成解决方案以成功保存每个已安装的应用程序列表视图项目旁边的复选框状态,这些状态将在我退出应用程序后保存,并且重新输入?一些代码真的很有帮助。
感谢您的宝贵时间。
如果您需要我包含任何 xml 文件,请告诉我。
【问题讨论】:
标签: java android sqlite android-listview sharedpreferences