Создание массива Список изображений появляется при нажатии флажка - PullRequest
0 голосов
/ 05 июня 2019

У меня есть Массив Список целых чисел, который может принимать рисованные элементы из отмеченных флажков. Я не могу заставить изображения появляться, когда установлен флажок.

Я пытался использовать функцию 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 голосов
/ 05 июня 2019

напишите свой код на приемнике изменений чекбокса (setOnCheckedChangeListener), для этого реализуйте OnCheckedChangeListener

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