使用的onActivityResult从ListView中删除项目 [英] Remove item from ListView using onActivityResult

查看:128
本文介绍了使用的onActivityResult从ListView中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次提出我的问题。很抱歉的重复。

我哪里有安装在设备上的所有应用程序一个ListView。在该项目中,卸载一部分的点击。到目前为止,一切正常,问题是,一旦卸载应用程序,那么它是在设备上不再,但它仍然在列表中。显然,这是不好的,所以我写了这篇code尝试删除它,一旦它已被卸载。

  INT位置;
myListView.setOnItemClickListener(新OnItemClickListener(){            公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的ID)
            {
            INT申请code = 1;
                应用= appInfoArrayList.get(位置);
                乌里packageUri = Uri.parse(套餐+ app.packagename);
                //从理论上讲,我应该把
                //this.position=position;
                //但是Eclipse说的位置不能得到解决或不是场
                意图uninstallIntent =新意图(Intent.ACTION_DELETE,packageUri);
                startActivityForResult(uninstallIntent,请求code);
            }    });保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
    {        弦乐味精=MyApp的:;
            super.onActivityResult(要求code,结果code,数据);
            如果(要求code == 1)
            {                 尝试{
                     packageInfo = getPackageManager()getApplicationInfo(app.packagename,0)。
                 }
                 赶上(PackageManager.NameNotFoundException E){
                     Log.v(抓,味精);
                     appInfoArrayList.remove(位置);
                     adapter.notifyDataSetChanged();
                 }                }            其他{            }            Log.v(MSG,将String.valueOf(结果code)+,+将String.valueOf(要求code));    }

在LogCat中我读:MyApp的:捕捉0(结果code),1(要求code),因此,这意味着,在捕捉正确输入,但尽管这样,即使应用程序已被卸载仍然在名单上。我如何才能解决这个问题的想法?先谢谢了。

适配器声明

  AppAdapter适配器;


解决方案

appInfoArrayList.remove(位置)的位置; 未初始化请确保您分配的值了。

Propose again my question. Sorry for the repetition.

I have a ListView where there are all the applications installed on the device. At the click on the item, part of the uninstall. So far everything works fine, the problem is that once the application is uninstalled, then it is no longer on the device, it remains in the list. Obviously this is not good and therefore I wrote this code to try to remove it once it has been uninstalled.

int position;
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);
                //In theory i should put
                //this.position=position;
                //But Eclipse says "position cannot be resolved or is not a field"
                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));

    }

In the LogCat i read: MyApp: Catch 0 (resultCode) , 1 (requestCode) So this means that enters correctly in the catch, but despite this, even if the application has been uninstalled is still on the list. Ideas on how I can fix this? Thanks in advance.

Adapter declaration

AppAdapter adapter;

解决方案

The position in appInfoArrayList.remove(position); is uninitialized make sure that you assign value to it.

这篇关于使用的onActivityResult从ListView中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆