Android在ListView中移除某个Item条目

时间:2021-06-19 19:32:18

我做了个bug就是说快捷启动里得到所有的应用程序,然后由于某些原因需要移除某个应用条目,我是这样做的:
1.使用这个工具类获取所有应用程序:

public class ApkTool {
    static  String TAG = "ApkTool";
    private Context mContext;
    public static List<AppInfo> mLocalInstallApps = null;

    public ApkTool(Context mContext) {
        this.mContext = mContext;
    }

    public  List<AppInfo> scanLocalInstallAppList(PackageManager packageManager) {
        List<AppInfo> myAppInfos = new ArrayList<AppInfo>();
        try {
            List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
            for (int i = 0; i < packageInfos.size(); i++) {
                PackageInfo packageInfo = packageInfos.get(i);
            // 过滤掉系统app
                            if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
                                continue;
                            }
                AppInfo myAppInfo = new AppInfo();

                 myAppInfo.setAppPackageName(packageInfo.packageName);
                if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
                    continue;
                }

                myAppInfo.setAppName(packageInfo.applicationInfo.loadLabel(mContext.getPackageManager()).toString());
                myAppInfo.setImage(packageInfo.applicationInfo.loadIcon(packageManager));
                myAppInfos.add(myAppInfo);
            }
        }catch (Exception e){
            Log.e(TAG,"===============获取应用包信息失败");
        }
        return myAppInfos;
    }


}

2.在activity中的listview里加载所有应用,之后调用adapter里的removeData方法移除某个包名对应的activity,最关键的是removeData方法,在这里我先是将完整的应用列表通过setData方法传给适配器并调用了notifyDataSetChanged进行列表数据刷新,然后调用adapter里自己定义的一个removeData方法进行条目遍历逐个筛选,将满足条件的条目加到另一个list中,最后将这个list返回,再重新把设置给setData,同样adapter的列表也能得到更新,这样适配器得到的也是筛选后的数据

public class ApplicationInfoSelection extends Activity {
    Handler mHandler = new Handler();
    AppAdapter mAppAdapter;
    private List<AppInfo> appInfos;
    private ApkTool apkTool;
    private String packagename;
    private SharedPreferences sharedPreferences;
    private Intent intent;


        private List<AppInfo> nowList =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.application_info_activity);
        sharedPreferences= getSharedPreferences("data",Context.MODE_PRIVATE);

        apkTool = new ApkTool(ApplicationInfoSelection.this);

        ListView mylisiview = (ListView) findViewById(R.id.lv_app_list);
        mAppAdapter = new AppAdapter();
        mylisiview.setAdapter(mAppAdapter);

        if (mylisiview != null) {
            mylisiview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    packagename = nowList.get(position).getAppPackageName();
                    String appName =  nowList.get(position).getAppName();
                    intent = new Intent("MyWorld");
                    Bundle bundle =new Bundle();
                    bundle.putString("test1",appName);
                    intent.putExtras(bundle);
                    sharedPreferences= getSharedPreferences("data",Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                   // Settings.System.putInt(getContext().getContentResolver(), "is_gou_show",1);
                    edit.putString("last_appname",packagename);

                    edit.commit();

                    sendBroadcast(intent);

                    //1.让左方的打勾图片显示

                    Settings.System.putInt(ApplicationInfoSelection.
                                    this.getContentResolver(), "item_click",
                            1);
                    //2.记录包名
                    Settings.System.putString(ApplicationInfoSelection.this.getContentResolver(), "quickstart_package_name", packagename);
                    Toast.makeText(ApplicationInfoSelection.this,ApplicationInfoSelection.this.getResources().getString(R.string.your_choice)+ " "+appName, Toast.LENGTH_SHORT).show();
                    finish();
                    //根据包名启动activity
// Intent intent = getPackageManager().getLaunchIntentForPackage(packagename);
// startActivity(intent);
                }
            });
        }
        initAppList();
    }
    private void initAppList(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                //扫描得到APP列表
                appInfos = apkTool.scanLocalInstallAppList(ApplicationInfoSelection.this.getPackageManager());
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAppAdapter.setData(appInfos);
                        mAppAdapter.setData(mAppAdapter.removeData(appInfos));
                    }
                });
            }
        }.start();
    }

    @Override
    protected void onPause() {
        super.onPause();

// sendBroadcast(intent);
    }

    class AppAdapter extends BaseAdapter {
 List<AppInfo> myAppInfos = new ArrayList<AppInfo>();

     // List<AppInfo> myAppInfos = new ArrayList<AppInfo>();
     // private List<AppInfo> nowList =new ArrayList<>();

        public void setData(List<AppInfo> myAppInfos) {
            this.myAppInfos = myAppInfos;
            notifyDataSetChanged();
        }

        public List<AppInfo> removeData(List<AppInfo> myAppInfos){

            for (AppInfo mList : myAppInfos){
              // Log.d(TAG," "+mList.getAppPackageName());
                if (mList.getAppPackageName().equals("com.tencent.mobileqq")){

                }else if (mList.getAppPackageName().equals("com.tencent.mm")){

                }
                else {
                    if (mList.getAppPackageName().equals("com.qiku.android.soundrecorder")){
Log.d("mList.getAppPackageName().equals(com.qiku.android.soundrecorder","yes");

                    } else if (mList.getAppPackageName().equals("com.topwise.agingtestext")){

                    }
                    else {
Log.d("mList.getAppPackageName().equals(com.qiku.android.soundrecorder","add");
                        nowList.add(mList);
                    }

            // Log.d(TAG,""+mList.getAppPackageName());
                }
            }
            return nowList;
            // Log.d(TAG," "+myAppInfos.toString());

        }




        public List<AppInfo> getData() {
            return myAppInfos;
        }

        @Override
        public int getCount() {
            if (myAppInfos != null && myAppInfos.size() > 0) {
                return myAppInfos.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (myAppInfos != null && myAppInfos.size() > 0) {
                return myAppInfos.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder mViewHolder;
            AppInfo myAppInfo = myAppInfos.get(position);
            if (convertView == null) {
                mViewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.list_item_view, null);
                mViewHolder.iv_app_icon = (ImageView) convertView.findViewById(R.id.iv_app_icon);
                mViewHolder.tx_app_package_name = (TextView) convertView.findViewById(R.id.tv_app_package_name);
                mViewHolder.tv_appp_name = (TextView) convertView.findViewById(R.id.tv_appp_name);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }

            mViewHolder.iv_app_icon.setImageDrawable(myAppInfo.getImage());
            mViewHolder.tx_app_package_name.setText(myAppInfo.getAppPackageName());
            mViewHolder.tv_appp_name.setText(myAppInfo.getAppName());
            return convertView;
        }
        class ViewHolder {
            TextView tv_appp_name;
            ImageView iv_app_icon;
            TextView tx_app_package_name;
        }
    }
    @Override
    public void onBackPressed() {

        super.onBackPressed();
   // this.getSharedPreferences("data",Context.MODE_PRIVATE).getString("last_appname",""));
// Settings.System.putString(ApplicationInfoSelection.
// this.getContentResolver(), "quickstart_package_name",
// this.getSharedPreferences("data",Context.MODE_PRIVATE).getString("last_appname",""));
        Settings.System.putInt(ApplicationInfoSelection.
                        this.getContentResolver(), "item_click",
                0);
        Intent intent = new Intent("MIC");
        Bundle bundle =new Bundle();
        bundle.putString("TODO","OK");
        intent.putExtras(bundle);
        sendBroadcast(intent);



    }


}