I made an app that have a list of all installed app in your device and I added a checkbox to the recyclerview to select an app on the list(I'm going to use this app as a base fo an app blocker). The problem is that the checkbox state always change when scrolling up/down. ex. I checked facebook then I scrolled down and when I scrolled up facebook is unchecked and sometimes a random app is checked.
here are my codes:
ApkInfoextractor:
public class ApkInfoExtractor {
Context context1;
public ApkInfoExtractor(Context context2){
context1 = context2;
}
public List<String> GetAllInstalledApkInfo(){
List<String> ApkPackageName = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN,null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
//Information that is returned from resolving an intent against an IntentFilter
List<ResolveInfo> resolveInfoList = context1.getPackageManager().queryIntentActivities(intent,0);
for(ResolveInfo resolveInfo : resolveInfoList){
ActivityInfo activityInfo = resolveInfo.activityInfo;
if(!isSystemPackage(resolveInfo)){
ApkPackageName.add(activityInfo.applicationInfo.packageName);
}
}
return ApkPackageName;
}
public boolean isSystemPackage(ResolveInfo resolveInfo){
return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
public Drawable getAppIconByPackageName(String ApkTempPackageName){
Drawable drawable;
try{
drawable = context1.getPackageManager().getApplicationIcon(ApkTempPackageName);
}
catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
drawable = ContextCompat.getDrawable(context1, R.mipmap.ic_launcher);
}
return drawable;
}
public String GetAppName(String ApkPackageName){
String Name = "";
ApplicationInfo applicationInfo;
PackageManager packageManager = context1.getPackageManager();
try {
applicationInfo = packageManager.getApplicationInfo(ApkPackageName, 0);
if(applicationInfo!=null){
Name = (String)packageManager.getApplicationLabel(applicationInfo);
}
}catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return Name;
}
}
AppsAdapter:
public class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder>{
private Context context1;
private List<String> stringList;
public AppsAdapter(Context context, List<String> list){
context1 = context;
stringList = list;
}
//viewholder initialized
@Override
public AppsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view2 = LayoutInflater.from(context1).inflate(R.layout.cardview_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(view2);
return viewHolder;
}
private SparseBooleanArray sba = new SparseBooleanArray();
//DATA IS BOUND TO VIEWS
@Override
public void onBindViewHolder(ViewHolder viewHolder,final int position){
viewHolder.setIsRecyclable(false);
ApkInfoExtractor apkInfoExtractor = new ApkInfoExtractor(context1);
final String ApplicationPackageName = (String) stringList.get(position);
//calling apps name and icon
String ApplicationLabelName = apkInfoExtractor.GetAppName(ApplicationPackageName);
Drawable drawable = apkInfoExtractor.getAppIconByPackageName(ApplicationPackageName);
//setting app name and icon for every card
viewHolder.textView_App_Name.setText(ApplicationLabelName);
viewHolder.imageView.setImageDrawable(drawable);
//saving states of the checkbox
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sba.put(position, !sba.get(position));
notifyDataSetChanged();
}
});
viewHolder.checkBox.setChecked(sba.get(position));
}
//viewholder
public class ViewHolder extends RecyclerView.ViewHolder{
public CardView cardView;
public ImageView imageView;
public TextView textView_App_Name;
public CheckBox checkBox;
public ViewHolder (View view){
super(view);
checkBox = (CheckBox) view.findViewById(R.id.chckbox);
cardView = (CardView) view.findViewById(R.id.card_view);
imageView = (ImageView) view.findViewById(R.id.imageview);
textView_App_Name = (TextView) view.findViewById(R.id.Apk_Name);
//textView_App_Package_Name = (TextView) view.findViewById(R.id.Apk_Package_Name);
}
}
@Override
public int getItemCount(){
return stringList.size();
}
}
I've tried many solutions from google and none of them worked for me. So a detailed answer is going to be helpful (sample codes or other references are much appreciated.)
Aucun commentaire:
Enregistrer un commentaire