OnActivityResult 的 Andriod Studio 初学者问题

问题描述 投票:0回答:0

应用程序显示一个有 25 个空框的空板,用户可以单击它并显示所有框。当他点击一个盒子时,他点击的图像视图变成了盒子的图像(盒子有 title desc 和 imageURL 在 firebase 实时数据库中引用)

我看不到发生了什么。

谢谢

我正在尝试使用 checkIfBoardIsFull() 检查董事会是否已满,但结果我没有意义,也没有按照我的想法去做。

所以我有这个代码:


public class CrearTaulell extends AppCompatActivity {
    private ImageView clickedBox;
    private  Board board;
    private Button btnGuardar;
    private boolean isFull;
    ImageView add_box1,add_box2,add_box3,add_box4,add_box5,add_box6,add_box7,add_box8,add_box9,add_box10,add_box11,add_box12,
            add_box13,add_box14,add_box15,add_box16,add_box17,add_box18,add_box19,add_box20,add_box21,add_box22,add_box23,add_box24,add_box25;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crear_taulell);

        board = new Board(); // create a new Board object
        btnGuardar = findViewById(R.id.btn_guardar_taulell);

        add_box1 = findViewById(R.id.add_box1);
        add_box2 = findViewById(R.id.add_box2);
        add_box3 = findViewById(R.id.add_box3);
        add_box4 = findViewById(R.id.add_box4);
        add_box5 = findViewById(R.id.add_box5);
        add_box6 = findViewById(R.id.add_box6);
        add_box7 = findViewById(R.id.add_box7);
        add_box8 = findViewById(R.id.add_box8);
        add_box9 = findViewById(R.id.add_box9);
        add_box10 = findViewById(R.id.add_box10);
        add_box11 = findViewById(R.id.add_box11);
        add_box12 = findViewById(R.id.add_box12);
        add_box13 = findViewById(R.id.add_box13);
        add_box14= findViewById(R.id.add_box14);
        add_box15 = findViewById(R.id.add_box15);
        add_box16 = findViewById(R.id.add_box16);
        add_box17 = findViewById(R.id.add_box17);
        add_box18 = findViewById(R.id.add_box18);
        add_box19 = findViewById(R.id.add_box19);
        add_box20 = findViewById(R.id.add_box20);
        add_box21 = findViewById(R.id.add_box21);
        add_box22 = findViewById(R.id.add_box22);
        add_box23 = findViewById(R.id.add_box23);
        add_box24 = findViewById(R.id.add_box24);
        add_box25 = findViewById(R.id.add_box25);


        //creacio dels 25 onclick listeners
        for (int i = 1; i <= 25; i++) {
            int resId = getResources().getIdentifier("add_box" + i, "id", getPackageName());
            ImageView addBox = findViewById(resId);

            addBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickedBox = addBox;
                    Intent i = new Intent(getApplicationContext(), GaleriaCaselles.class);
                    startActivityForResult(i, 100);


                }
            });
        }
}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {

            String imageUrl = data.getStringExtra("result");
            // Carregar l'imatge a la casella seleccionada
            Glide.with(this)
                    .load(imageUrl)
                    .into(clickedBox);
            Toast.makeText(this, "Casella afegida correctament! ", Toast.LENGTH_SHORT).show();

            checkIfBoardIsFull();
        }
    }
    private boolean checkIfBoardIsFull() {
        boolean isFull = true;
        for (int i = 1; i <= 25; i++) {
            int resId = getResources().getIdentifier("add_box" + i, "id", getPackageName());
            ImageView addBox = findViewById(resId);
            if (addBox.getDrawable() == null) {
                isFull = false;
                Toast.makeText(this, "isfull false! ", Toast.LENGTH_SHORT).show();
                break;
            }
        }
        Toast.makeText(this, "isfull true! ", Toast.LENGTH_SHORT).show();
        return isFull;
    }
}
android onactivityresult
© www.soinside.com 2019 - 2024. All rights reserved.