我已根据此引用https://www.youtube.com/watch?v=luxE7oEKiic实现了我的应用程序但是,我删除的项目将在重新启动应用程序时自动添加回列表中。知道如何永久删除该项目?
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppAdapter listadapter = null;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
new LoadApplications().execute();
list = (ListView) findViewById(android.R.id.list);
listadapter = new AppAdapter(this, R.layout.activity_list_app, applist);
list.setAdapter(listadapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode arg0) {
listadapter.removeSelection();
}
@Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
arg0.getMenuInflater().inflate(R.menu.menu_main, arg1);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
switch (arg1.getItemId()) {
case R.id.delete:
SparseBooleanArray selected = listadapter.getSelectedIds();
for(int i = (selected.size() - 1); i >= 0; i--) {
if(selected.valueAt(i)) {
ApplicationInfo selecteditem = listadapter.getItem(selected.keyAt(i));
listadapter.remove(selecteditem);
}
}
arg0.finish();
return true;
default:
return false;
}
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
final int checkedCount = list.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
listadapter.toggleSelection(position);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.setSelected(true);
}
});
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
try{
Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
if(intent != null){
startActivity(intent);
}
}catch(ActivityNotFoundException e){
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
for(ApplicationInfo info : list){
try{
if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
appList.add(info);
}
}catch(Exception e){
e.printStackTrace();
}
}
return appList;
}
private class LoadApplications extends AsyncTask<Void, Void, Void>{
private ProgressDialog progress = null;
protected Void doInBackground(Void... params){
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
return null;
}
protected void onPostExecute(Void result){
setListAdapter(listadapter);
progress.dismiss();
super.onPostExecute(result);
}
protected void onPreExecute(){
progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
super.onPreExecute();
}
}
答案 0 :(得分:1)
当您致电new LoadApplications().execute();
时,您的数据来自来源。
此任务从源列表中获取所有项目。为了完全删除项目(即使应用程序关闭并重新打开后也是如此)您需要在数据库或共享首选项中保存用户删除的项目列表。
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
switch (arg1.getItemId()) {
case R.id.delete:
SparseBooleanArray selected = listadapter.getSelectedIds();
for(int i = (selected.size() - 1); i >= 0; i--) {
if(selected.valueAt(i)) {
ApplicationInfo selecteditem = listadapter.getItem(selected.keyAt(i));
listadapter.remove(selecteditem);
// Save the selected item in a Content Provider/Shared Pref
}
}
arg0.finish();
return true;
default:
return false;
}
}
在postExecute
类的LoadApplications
方法中,在调用notifyDataSetChanged
之前,从源代码返回的列表中删除存储在数据库或共享首选项中的项目。
private class LoadApplications extends AsyncTask<Void, Void, Void>{
private ProgressDialog progress = null;
protected Void doInBackground(Void... params){
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
// Remove the items me stored in the removedList
listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
return null;
}
protected void onPostExecute(Void result){
setListAdapter(listadapter);
progress.dismiss();
super.onPostExecute(result);
}
protected void onPreExecute(){
progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
super.onPreExecute();
}
}
为您提供快速入门。您可以使用这个非常容易使用的班级TinyDB来保存您的列表。但我建议您详细介绍Content Providers和SharedPrefs。
答案 1 :(得分:0)
您应该将修改后的数据存储在onSaveInstanceState
中。当您“重新启动”活动时,请先检查savedInstanceState
捆绑包。如果它不为空,则从Bundle
恢复数据,而不是从原始源获取数据。
如果您的意思是没有savedInstanceState
的开始,则必须修改数据源。如果不能,您可以将操作或修改后的数据记录在自己的存储中。