【问题标题】:How to take an array index list if condition in programming for android?如果在android编程中出现条件,如何获取数组索引列表?
【发布时间】:2023-03-24 09:45:02
【问题描述】:

我使用数组列表imageView shuffle,如何将输入列表中的数组索引值带入if条件?示例代码如下,但如果条件不想走路,解决办法是什么?

这是代码: if (soal.equals(list.get(0)) && text.getText().toString().equals("one") )

public class tebakgambar extends Activity implements OnClickListener {

    private Button ok;
    private ImageView soal;
    private EditText text;

    ArrayList<Integer> list = new ArrayList<Integer>() {{
        add(R.drawable.siji);
        add(R.drawable.loro);
        add(R.drawable.telu);
        add(R.drawable.papat);
        add(R.drawable.limo);
        add(R.drawable.enem);
        add(R.drawable.pitu);
        add(R.drawable.wolu);
        add(R.drawable.songo);
        add(R.drawable.nol);
    }};



    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.soal);


        soal = (ImageView) findViewById(R.id.imageView);
        text = (EditText) findViewById(R.id.editText);
        ok = (Button) findViewById(R.id.button);

        ok.setOnClickListener(this);
    }
    public void onClick(View v) {
        acak();
       jawaban();

    }

    private void acak(){

        Collections.shuffle(list);
        soal.setImageResource(list.get(0));
        soal.setTag(list.get(0));

    }

    private void jawaban() {

        if (soal.equals(list.get(0)) && text.getText().toString().equals("one") ) {
            text.setText("");
            // get your custom_toast.xml ayout
            LayoutInflater inflater = getLayoutInflater();

            View layout = inflater.inflate(R.layout.custom_toast,
                    (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            // set a dummy image
            ImageView image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.benar);

            // set a message
            // TextView text = (TextView) layout.findViewById(R.id.text);
            //text.setText("Benar");

            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        } else {
            text.setText("");
            LayoutInflater inflater = getLayoutInflater();

            View layout = inflater.inflate(R.layout.custom_toast,
                    (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            // set a dummy image
            ImageView image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.salah);

            // set a message
            // TextView text = (TextView) layout.findViewById(R.id.text);
            //text.setText("Benar");

            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
    }

【问题讨论】:

    标签: java android arraylist


    【解决方案1】:

    如果我正确理解了你的问题,

    soal.equals(list.get(0))
    

    错了。 “soal”是一个 ImageView 对象,您将它等同于一个 Integer(因为 ArrayList 是 Integer 类型)。这无论如何都是不正确的。

    你需要这样做,

    soal.getId().equals(list.get(0))
    

    我不知道你的其余代码逻辑,但这应该可以。

    【讨论】:

    • 您的问题解决了吗?或者您需要更多帮助?
    猜你喜欢
    • 2015-08-25
    • 2018-12-12
    • 1970-01-01
    • 2012-08-24
    • 2014-01-10
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多