如何编码在自定义列表视图中创建的复选框

how to code checkboxes created in custom listview

我创建了一个自定义列表视图,它列出了设备上所有已安装的应用程序,在此列表中是应用程序图标、应用程序名称和一个复选框。我想编写 onCheckboxClicked 方法的代码,但我不知道将它放在哪里,我读过它应该在适配器中,但我一直在查看的其他示例在主 activity 中有代码,我可以似乎无论哪种方式都能正常工作。

    public class MainActivity extends ActionBarActivity {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //retrieve currently installed apps
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
    List<AppDetails> appList = new ArrayList<>();

    for (Object object : pkgAppsList)
    {
        ResolveInfo info = (ResolveInfo) object;
        Drawable icon    = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
        String strAppName   = info.activityInfo.applicationInfo.publicSourceDir.toString();
        String strPackageName  = info.activityInfo.applicationInfo.packageName.toString();
        final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
        AppDetails tmp = new AppDetails(title, icon);
        appList.add(tmp); //add title and icon into list
    }


    final ListView listview = (ListView) findViewById(R.id.listView);
    final AppDetailsAdapter adapter = new AppDetailsAdapter(this, appList);
    listview.setAdapter(adapter);

}

适配器:

    public class AppDetailsAdapter extends BaseAdapter {

private List<AppDetails> data;
private Context context;

public AppDetailsAdapter(Context context, List<AppDetails> data) {
    this.context = context;
    this.data = data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }


    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    TextView text = (TextView) view.findViewById(R.id.text);

    final AppDetails item = data.get(position);

    text.setText(item.name);
    icon.setImageDrawable(item.icon);


    return view;
}

}

自定义布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"

android:clickable="true"
android:orientation="horizontal"
android:weightSum="1">

<ImageView
    android:id="@+id/icon"
    android:layout_width="39dp"
    android:layout_height="39dp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="16dp"
    android:textSize="17sp" />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:onClick="onCheckboxClicked"/>/>


</LinearLayout>

我还读到需要修改 getView 方法来处理复选框,但我似乎无法用目前为止尝试过的方法来做到这一点

用以下代码替换适配器的获取视图代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }


    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    TextView text = (TextView) view.findViewById(R.id.text);

CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);

    final AppDetails item = data.get(position);

    text.setText(item.name);
    icon.setImageDrawable(item.icon);

    checkBox .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // Handle your conditions here

            }
        });


    return view;
}

这里setOnCheckedChangeListener是你必须处理的方法。