使用onActivityResult从ListView中删除项目

时间:2013-11-24 17:03:46

标签: java android xml listview

我有一个ListView,其中设备上安装了所有应用程序。点击该项目,部分卸载。到目前为止一切正常,问题是一旦卸载了应用程序,它就不再在设备上,它仍然在列表中。显然这并不好,因此我编写了这段代码,试图在卸载后删除它。

myListView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            { 
            int requestCode = 1;
                app = appInfoArrayList.get(position);
                Uri packageUri = Uri.parse("package:"+app.packagename);
                Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageUri);
                startActivityForResult(uninstallIntent, requestCode);
            }

    });   

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        String msg = "MyApp: ";
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1)
            {

                 try {
                     packageInfo = getPackageManager().getApplicationInfo(app.packagename, 0);
                 }
                 catch (PackageManager.NameNotFoundException e) {
                     Log.v("Catch",msg );          
                     appInfoArrayList.remove(position);
                     adapter.notifyDataSetChanged();  
                 }

                }

            else {

            }

            Log.v(msg, String.valueOf(resultCode)+", "+String.valueOf(requestCode));

    }

在LogCat中我读到:MyApp:Catch 0(resultCode),1(requestCode)所以这意味着在catch中正确输入,但尽管如此,即使已经卸载了应用程序仍然在列表中。关于如何解决这个问题的想法?提前谢谢。

0 个答案:

没有答案