【问题标题】:Refresh the listview on deleting the item from there刷新列表视图以从那里删除项目
【发布时间】:2015-12-08 15:41:39
【问题描述】:

嘿,伙计,我正在开发包含列表视图的应用程序,其中安装的应用程序填充了一些按钮,例如卸载。

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>

删除项目后我应该如何刷新列表视图。

谢谢!!!

【问题讨论】:

    标签: android string listview android-listview


    【解决方案1】:

    我从问题中得到的是,如果用户单击“是”进行卸载,但用户从设置屏幕返回应用程序而不卸载它,那么您一定会遇到问题。在这种情况下,如果用户在卸载时单击“是”并将应用程序包名称保存在实例级别的字符串变量中并在类的恢复方法中检查用户布尔值是否为真然后检查定义的包是否存在,则创建一个变量布尔值,然后检查是否存在定义的包和如果它不存在更新您的列表视图。请参考这个,我还添加了 cmets 来通知你做了哪些更改

     public class MainActivity extends ListActivity {
        PackageManager packageManager;
        List<ApplicationInfo> applist;
        Listadapter listadapter;
        // add three variables
        String packageName="";
        boolean isUnInstallClicked;
        int mPosition;
    
        @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);
                                    //remove applist.remove and notify from here and add it to on resume
                                    packageName=app1.packageName;
                                    isUnInstallClicked=true;
                                    mPosition=position;
    
                                    break;
                                case 2:
                                    break;
                            }
                        }
                    });
            dialogBuilder.setCancelable(true)
                    .show();
    
        }
        @Override
            protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
                // here check for whether unistall was clicked and was app unistalled
                if(isUnInstallClicked && !appInstalledOrNot(packageName)){
                    //check whther app unistalled or not
    
                    applist.remove(listadapter.getItem(position));
                    listadapter.notifyDataSetChanged();
                    }
                }
            }
        private boolean appInstalledOrNot(String uri) {
            PackageManager pm = getPackageManager();
            boolean app_installed;
            try {
                pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
                app_installed = true;
            }
            catch (PackageManager.NameNotFoundException e) {
                app_installed = false;
            }
            return app_installed;
        }
        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();
    

    【讨论】:

    • 兄弟实际上卸载工作正常......但即使在应用程序被卸载后我在我的列表视图中看到该应用程序......为此我尝试了 adapter.notifydatasetcange 但这不起作用或者我可能正在放置它在某个错误的地方...
    • 你能给我一些关于 resume.thanx 的代码刷新列表视图
    • in applist.remove(listadapter.getItem(position)) applsit是listview对象还是arraylist对象?
    • 我已经编辑并发布了我的完整代码,请告诉我我哪里错了。
    • 我对此不确定,但不是 applist.remove(listadapter.getItem(position)) 使用 applist.remove((position) 并从 onPause 中删除 listadapter.notifyDataSetInvalidated()
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    相关资源
    最近更新 更多