在我的活动中我有一个列表视图包含许多项目和列表具有多重模式。我想保存列表视图的所选项目的状态,因此当用户在列表视图中选择项目并关闭应用程序并重新启动应用程序时,应该已经选择了先前选择的项目。谢谢
这是我的活动课。
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;
public class ApkListActivity extends Activity {
PackageManager packageManager;
ListView apkList;
Editor editor;
List<PackageInfo> packageList;
String pname = "";
SharedPreferences sharedPrefs;
int po;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
packageManager = getPackageManager();
final 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.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
}
答案 0 :(得分:0)
我通过使用布尔ArrayList来解决该问题,以使用item的大小保存检查状态。
你还需要这样做
if(list.get(i))
item.setChecked(true);
else
item.setChecked(false);