【问题标题】:how to check multiple checkboxes in a custom listview如何检查自定义列表视图中的多个复选框
【发布时间】:2018-11-22 09:19:17
【问题描述】:

我有一个 ListView,我在 xml 中为它创建了一个 custom_row。我的 custom_row 包含一个 ImageView、2 个 Textviews 和一个 Checkbox。 listView 的数据源现在是 3 个数组,即 MedName、Describe 和 Image of 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;
    }

}

}

这是运行AppCustomListView App的截图

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

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

            }
        });

【问题讨论】:

标签: android listview


【解决方案1】:

您的列表视图中的每个元素都有自己的复选框,您应该在 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;
    }

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

【讨论】:

  • 感谢您的贡献。 @Srijay,如果条件之后的点,我需要获取 Listview 项目的位置,我需要 setOnItemClickListener 下的位置参数,但在这里我没有那个事件。那么如何在这个 OnClickListener 中获取 listView 属性呢?
  • @user87 我应该给出位置的变量
【解决方案2】:

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

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

cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

       }
   }
);  

【讨论】:

  • 抱歉,您想要的是 CheckBox 而不是 CompoundButton。我已经更新了答案
  • 我已按照您在我原来的问题中的指示添加了一段新代码。但它使 App ManxDev 崩溃
  • 如何添加错误。它在 Android Monitor 中的 loooong 错误?
  • 应该有一行“Caused by xxxxx Exception”
  • --------- 崩溃开始 11-22 11:06:40.311 3181-3181/? E/AndroidRuntime:致命异常:主进程:com.durrani.customlistview,PID:3181 android.content.res.Resources$NotFoundException:字符串资源 ID #0xffffffff
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多