0
嘿,我正在工作的应用程序,其中包含listview在其中安装的应用程序填充一些按钮,如卸载。刷新从这里删除项目的列表视图
public class MainActivity extends ListActivity {
PackageManager packageManager;
List<ApplicationInfo> applist;
Listadapter listadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
new LoadApplications().execute();
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Choose option")
.setItems(R.array.options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
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();
}
break;
case 1:
ApplicationInfo app1 = applist.get(position);
Uri packageURI = Uri.parse("package:" + app1.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
applist.remove(listadapter.getItem(position));
listadapter.notifyDataSetChanged();
break;
case 2:
break;
}
}
});
dialogBuilder.setCancelable(true)
.show();
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<>();
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;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
setListAdapter(listadapter);
progress.dismiss();
super.onPostExecute(aVoid);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,");
super.onPreExecute();
}
}
@Override
protected void onPause() {
super.onPause();
listadapter.notifyDataSetInvalidated();
}
当我点击卸载时,应用程序的位置在卸载应用程序之前首先被删除。
这是strings.xml中
<resources>
<string name="app_name">AppSharer</string>
<string-array name="options">
<item>Open App</item>
<item>uninstall</item>
</string-array>
</resources>
我应该怎么刷新列表视图一旦我删除的项目。
谢谢!
兄弟居然卸载工作正常...但应用程序卸载即使我看到在我的列表视图该应用......此我试过adapter.notifydatasetcange,但这是nt工作,或者我可能把它放在一些错误的地方... – Rahul
可以请你给我一些代码刷新listview上resume.thanx – Rahul
在applist.remove(listadapter.getItem(位置))applsit是listview对象还是arraylist对象? –