mardi 31 mai 2016

Automatic selection of checkbox in the custom list view

I am building a Android apps Uninstaller and the list contains list of package names, checkbox to chose which one to uninstall. the problem is that when the list became longer than the phone screen and the scrolling is active; when i select a check box , a second one is automatically selected in the bottom of the list.

the problem is the automatic selection of the second checkbox; please let me know how can i fix it ?? Here the code of main Activity.

public class Mode extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener
{
    PackageManager packageManager;
    ListView apkList;

    List<PackageInfo> packageList1=new ArrayList<PackageInfo>();
    @Override
    public void onCreate(Bundle savedInstanceSpace)
    {
        super.onCreate(savedInstanceSpace);
        setContentView(R.layout.apklist_item1);
        apkList = (ListView) findViewById(R.id.applist);

        packageManager=getPackageManager();
        List<PackageInfo> packageList= packageManager.getInstalledPackages(0);

        for(PackageInfo pi : packageList)
        {
            boolean b = isSystemPackage(pi);
            if(!b)
            {
                packageList1.add(pi);

            }
        }


        Collections.sort(packageList1,  new Comparator<PackageInfo>() {
            @Override
            public int compare(PackageInfo lhs, PackageInfo rhs) {
                return lhs.applicationInfo.loadLabel(getPackageManager()).toString().compareTo(rhs.applicationInfo.loadLabel(getPackageManager()).toString());

            }
        });



        ApkAdapter apkAdapter =new ApkAdapter(this, packageList1, packageManager);
        apkList.setAdapter(apkAdapter);


    }

    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                : false;
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        int pos = apkList.getPositionForView(buttonView);


        if (pos != ListView.INVALID_POSITION) {
            PackageInfo packageInfo = (PackageInfo) apkList.getItemAtPosition(pos);
            AppData appData = (AppData) getApplicationContext();
            appData.setPackageInfo(packageInfo);

        }

Adapter class:

public class ApkAdapter extends  BaseAdapter {


    List<PackageInfo> packageList;
    Activity context;
    PackageManager packageManager;
    boolean itemChecked;
    public ApkAdapter(Activity context, List<PackageInfo> packageList,
                      PackageManager packageManager) {
        super();
        this.context = context;
        this.packageList = packageList;
        this.packageManager = packageManager;
    }

    private class ViewHolder {
        TextView apkName;
        TextView apkInstall;
        CheckBox cb;
    }

    public int getCount() {
        return packageList.size();
    }

    public Object getItem(int position) {
        return packageList.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.apklist_item, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            holder.apkInstall=(TextView)convertView.findViewById(R.id.appInstall);
            holder.cb.setChecked(isEnabled(position));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

PackageInfo         packageInfo = (PackageInfo) getItem(position);
           Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 50, 50);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);


        holder.apkName.setText(appName);
       holder.cb.setTag(position);

      File file=new File(packageInfo.applicationInfo.sourceDir);

        long sizeInBytes =  file.length();

        double sizeInMb=sizeInBytes/(1024);

        holder.apkInstall.setText(String.valueOf(sizeInMb)+"KB" );


        return convertView;
    }


}

        }

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:text="Uninstaller"
        android:onClick="uninstall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />



    <ListView
        android:id="@+id/applist"
        android:paddingTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayou





  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/appname"
            android:padding="@dimen/activity_horizontal_margin"
            android:textColor="#006400"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingTop="5dp"
            android:textSize="20sp"
            android:paddingLeft="10dp"
            android:paddingBottom="5dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/appInstall"
            android:layout_marginTop="20dp"
            android:padding="@dimen/activity_horizontal_margin"
            android:textColor="#006400"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingTop="5dp"
            android:textSize="15sp"
            android:paddingLeft="10dp"
            android:paddingBottom="5dp"
            android:textStyle="bold" />
        <CheckBox
            android:id="@+id/cb"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire