gpt4 book ai didi

java - 保存动态创建的复选框的状态

转载 作者:行者123 更新时间:2023-12-02 02:58:31 26 4
gpt4 key购买 nike

我目前正在尝试制作一个简单的任务 killer 以用于学习目的,我创建了一个 ListView ,显示所有用户安装的带有复选框的应用程序,以便用户可以选择在屏幕关闭时应杀死的应用程序。

但我不知道如何保存复选框状态并获取选中的应用程序的包名称

我已经搜索堆栈溢出很长时间了,但仍然不知道该怎么办,请帮助我,谢谢

这是我的代码

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;
}
}

提前致谢

最佳答案

创建接口(interface):

    public interface AppSelector {
void selectApp(String appName);
void removeApp(String appName);
boolean isSelected(String appName);
}

将其添加到您的 AppAdapter:

    public class AppAdapter extends BaseAdapter {

private AppSelector appSelector;
private List<AppList> listStorage;

public AppAdapter(AppSelector appSelector, List<AppList> customizedListView) {
this.appSelector = appSelector;
listStorage = customizedListView;
}

....

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).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) {
appSelector.selectApp(listStorage.get(position).getName());
} else {
appSelector.removeApp(listStorage.get(position).getName());
}
}
});
} else {
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
listViewHolder.checkBox.setChecked(appSelector.isSelected(listStorage.get(position).getName()));

return convertView;
}

....
}

通过您的 MainActivity 实现:

    class MainActivity extends AppCompatActivity implements AppSelector {

private static final String PREFERENCES_NAMESPACE = "checkboxes_states";
private SharedPreferences checkedAppsPreferences;

....

@Override
protected void onCreate(Bundle savedInstanceState) {
....
checkedAppsPreferences = getSharedPreferences(PREFERENCES_NAMESPACE, 0);
....
AppAdapter installedAppAdapter = new AppAdapter(this, installedApps);
....
}

@Override
public void selectApp(String appName) {
checkedAppsPreferences.edit()
.putBoolean(appName, true)
.apply();
}

@Override
public void removeApp(String appName) {
checkedAppsPreferences.edit()
.remove(appName)
.apply();
}

@Override
public boolean isSelected(String appName) {
return checkedAppsPreferences.contains(appName) && checkedAppsPreferences.getBoolean(appName, false);
}

....
}

您的 AppList 类不应包含对 Context 或 SharedPreferences 的任何引用以及“检查”/“取消检查”的逻辑。它应该是简单的 POJO。

使用界面,您可以轻松地将共享首选项替换为另一种类型的存储库(例如 SQLite)

编辑:我更改 AppAdapter getView mwthod:

            listViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
appSelector.selectApp(listStorage.get(position).getName());
} else {
appSelector.removeApp(listStorage.get(position).getName());
}
}
});

您可以在 https://github.com/valeragit/AppListExample 查看完整项目

关于java - 保存动态创建的复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com