单击复选框时显示图像阵列列表

时间:2019-06-05 05:05:10

标签: java android arraylist android-checkbox

我有一个整数数组列表,可以从单击的复选框中获取可绘制对象。单击复选框后,我无法显示图像。

我尝试不使用example.get函数来显示图像。

public class MainActivity extends AppCompatActivity {
    ImageView rib;
    ArrayList<Integer> example = new ArrayList<>();


    private CheckBox ribbon1, ribbon2, ribbon3, ribbon4, ribbon5, ribbon6;
    private Button button;
    private ArrayList<String> selection = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rib = findViewById(R.id.imageView);
        button = findViewById(R.id.button);
        ribbon1 = findViewById(R.id.checkBox);
        ribbon2 = findViewById(R.id.checkBox2);
        ribbon3 = findViewById(R.id.checkBox3);
        ribbon4 = findViewById(R.id.checkBox4);
        ribbon5 = findViewById(R.id.checkBox5);
        ribbon6 = findViewById(R.id.checkBox6);
        selectItem();
    }

    public void selectItem(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ribbon1.isChecked()) example.add(R.drawable.ribbon1);
                else example.remove(R.drawable.ribbon1);

                if(ribbon2.isChecked()) example.add(R.drawable.ribbon2);
                else example.remove(R.drawable.ribbon2);

            }
        });
        if(example.size() == 1)
        {
            rib.setImageResource(example.get(1));
        }

}

此处的XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="395dp"
        android:layout_height="340dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="8dp">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox"
            tools:text="Ribbon 1"
            android:onClick="selectItem"/>

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ribbon 2" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ribbon 3" />

        <CheckBox
            android:id="@+id/checkBox4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ribbon 4" />

        <CheckBox
            android:id="@+id/checkBox5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RIbbon 5" />

        <CheckBox
            android:id="@+id/checkBox6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ribbon 6" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Yes"

            />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:srcCompat="@drawable/ribbon6" />

</android.support.constraint.ConstraintLayout>

我希望一旦单击复选框,图像就会出现。

收到错误消息

 2019-06-05 00:57:05.765 15015-15015/com.example.dennis.checkboxribbons E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dennis.checkboxribbons, PID: 15015
    java.lang.IllegalStateException: Could not find method selectItem(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatCheckBox with id 'checkBox'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
        at android.view.View.performClick(View.java:6256)

1 个答案:

答案 0 :(得分:0)

在复选框更改侦听器(setOnCheckedChangeListener)上编写代码,为此,请执行OnCheckedChangeListener

 ribbon1.setOnCheckedChangeListener(this);
 ribbon2.setOnCheckedChangeListener(this);