如何在自定义列表视图中选中多个复选框

时间:2018-11-22 09:19:17

标签: android listview

我有一个ListView,为此我在xml中创建了一个custom_row。我的custom_row包含一个ImageView,2个Textviews和一个复选框。 listView的数据源现在是3个数组,即MedName,Descrip和Meds图片。

现在,当应用程序运行时,ListView会填充Array中的数据。

问题:我想允许用户(通过复选框)选择尽可能多的药物,并且当按下“提交”按钮时,所有选中的(已检查的)物品都将存储在单独的表格中并在另一个表格中取消选中。但是现在我还没有附加任何数据库,所以我想在Toast中显示这些选中和未选中的项目。

这是我的custom_row(customlayout)代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15sp">

<ImageView
    android:id="@+id/iv1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="20dp"
    app:srcCompat="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginStart="90dp"
    android:textSize="10dp"
    android:text="TextView" />

<TextView
    android:id="@+id/tvDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/iv1"
    android:layout_marginStart="90dp"
    android:textSize="10dp"
    android:text="TextView" />

<CheckBox
    android:id="@+id/cbTaken"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvDescription"
    android:layout_marginStart="32dp"
    android:layout_toEndOf="@+id/tvDescription"
    android:text="Taken" />

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity {

ListView lv1;
CheckBox cbTaken;

int imgs[] = {R.drawable.flagyl, R.drawable.imodium, R.drawable.flagyl, R.drawable.imodium};
String[] medicinename = {"Flagyl", "Imodium", "Flagyl", "Imodium"};
String[] usage = {"Gas Trouble", "Diarrohea", "Gas Trouble", "Diarrohea"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv1 = findViewById(R.id.lvCustom);
    cbTaken = findViewById(R.id.cbTaken);

    CustomAdapter customAdapter = new CustomAdapter();
    lv1.setAdapter(customAdapter);

}

public void showToast(View view) {

    String status = "";

    if (cbTaken.isChecked())
    {
        status = "Taken";
    }
    Toast.makeText(this, status, Toast.LENGTH_SHORT).show();


}


class CustomAdapter extends BaseAdapter
{

    @Override
    public int getCount() { // used for size of our Data
        return imgs.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {


        return 0;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.customlayout, null); 

        ImageView imgView= view.findViewById(R.id.iv1); //Now our layout is in View so we search inside view
        TextView tvName = view.findViewById(R.id.tvName);
        TextView tvDesc = view.findViewById(R.id.tvDescription);

        // NOW WE BIND THE DATA WITH IMageView

        imgView.setImageResource(imgs[i]);
        tvName.setText("Med Name: " + medicinename[i]);
        tvDesc.setText("Usage: " + usage[i]);

        return view;
    }

}

}

这是运行App CustomListView App

的屏幕截图

我不知道如何在添加注释选项中添加代码,因此我在@ManxDev回复后添加注释: 这就是我所做的

It is crashing my App
cbTaken.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (cbTaken.isChecked())
                {
                    int posi = lv1.getSelectedItemPosition();
                    Toast.makeText(MainActivity.this, posi, Toast.LENGTH_SHORT).show();
                }

            }
        });

2 个答案:

答案 0 :(得分:0)

列表视图中的每个元素都有其自己的复选框,您应在getView方法内为该复选框添加一个onclicklistener

@Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.customlayout, null);

        ImageView imgView= view.findViewById(R.id.iv1); //Now our layout is in View so we search inside view
        TextView tvName = view.findViewById(R.id.tvName);
        TextView tvDesc = view.findViewById(R.id.tvDescription);

        // NOW WE BIND THE DATA WITH IMageView

        imgView.setImageResource(imgs[i]);
        tvName.setText("Med Name: " + medicinename[i]);
        tvDesc.setText("Usage: " + usage[i]);

        final CheckBox cb = view.findViewById(R.id.cbTaken);
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(cb.isChecked()){
                    //do your stuff here
                    //in your case you might want to add tvName.getText().toString() to a table which stores all the names of checked items.
                }else{
                    //remove tvName.getText().toString() from the table which stores checked items
                }
            }
        });
        return view;
    }

并单击提交按钮时,只需遍历存储所有选定项目的列表。

答案 1 :(得分:0)

您可以在CustomAdapter.getView中将侦听器添加到复选框。

然后,当选中此复选框时,将显示一个Toast,其中包含该ListItem的信息。您需要将Context传递给CustomAdapter。

cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

       }
   }
);