Android removes an Item entry from the ListView

I made a bug that means I get all the apps in a quick boot, and then for some reason I need to remove an app entry, whic...

I made a bug that means I get all the apps in a quick boot, and then for some reason I need to remove an app entry, which I do:
1. Use this tool class to get all applications:

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); // Filter out system app s 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,"===============Failed to get application package information"); } return myAppInfos; } }

2. Load all applications in the listview in the activity, then call the removeData method in the adapter to remove the activity corresponding to a package name, the most critical is the removeData method, where I first pass the complete list of applications to the adapter through the setData method and call notifyDataSetChanged to refresh the list data, then call a removeD defined by myself in the adapterThe ATA method iterates through the entries one by one, adds the qualified entries to another list, returns the list, and resets it to setData. The adapter's list can also be updated so that the adapter gets the filtered data as well.

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. Let the tick picture on the left display Settings.System.putInt(ApplicationInfoSelection. this.getContentResolver(), "item_click", 1); //2. Record package name 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(); //Start activity based on package name // Intent intent = getPackageManager().getLaunchIntentForPackage(packagename); // startActivity(intent); } }); } initAppList(); } private void initAppList(){ new Thread(){ @Override public void run() { super.run(); //Scan to get APP list 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); } }

28 June 2020, 12:10 | Views: 9686

Add new comment

For adding a comment, please log in
or create account

0 comments