自动选择自定义列表视图中的复选框

时间:2016-05-31 10:42:44

标签: android listview checkbox

我正在构建一个Android应用程序卸载程序,该列表包含程序包名称列表,复选框以选择要卸载的程序包。问题是,当列表变得比电话屏幕更长并且滚动处于活动状态时;当我选中一个复选框时,会在列表底部自动选择第二个复选框。

问题是第二个复选框的自动选择;请让我知道我该如何解决? 这里是主要活动的代码。

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);

        }

适配器类:

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文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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://schemas.android.com/apk/res/android"
        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>

1 个答案:

答案 0 :(得分:1)