我目前正在嘗試做一個簡單的任務殺手爲學習目的,我創建了一個列表視圖,顯示所有用戶安裝的應用程序與複選框,所以用戶可以選擇與應用程序應該在屏幕關閉時被殺死。保存狀態動態創建複選框
但我停留在如何保存複選框狀態,並得到檢查應用的軟件包名稱
我有搜索棧溢出了很長時間,但還是不知道該怎麼做,請幫幫我,謝謝
這是我的代碼
AppAdapter.java
public class AppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
listViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(listViewHolder);
listViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
listViewHolder.checkBox.setSelected(listStorage.get(position).isSelected());
}
}
});
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
listViewHolder.checkBox.setChecked(listStorage.get(position).isSelected());
return convertView;
}
static class ViewHolder{
TextView textInListView;
ImageView imageInListView;
CheckBox checkBox;
}
}
Applist.java
public class AppList {
private String name;
Drawable icon;
private static final String PREFERENCES_NAMESPACE = "checkboxes_states";
boolean selected = false;
private SharedPreferences mSettings;
private SharedPreferences.Editor mEditor;
public AppList(Context context, String name, Drawable icon) {
this.name = name;
this.icon = icon;
mSettings = context.getSharedPreferences(PREFERENCES_NAMESPACE, 0);
mEditor = mSettings.edit();
setSelected(mSettings.getBoolean(name, selected));
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
if(this.selected != selected) { // update if changed
mEditor.putBoolean(getName(), selected);
mEditor.apply();
this.selected = selected;
}
}
}
MainActivity.java
public class MainActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);
List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(new AppList(context, appName, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
}
在此先感謝
我試過你的代碼但沒有工作,該複選框仍然沒有保存狀態 – Dragon957
編輯,雖然添加測試項目 –
我嘗試導入您的項目和測試應用程序,但它仍然無法正常工作,滾動listview後,複選框變得不再檢查 – Dragon957