Android在ListView中移除某个Item条目

我做了个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);



    }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值