如果在android编程条件中如何获取数组索引列表?

时间:2015-06-29 03:01:29

标签: java android arraylist

我使用数组列表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();
        }
    }

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,

soal.equals(list.get(0))

错了。 &#34; SOAL&#34;是一个ImageView对象,你将它与一个Integer等同(因为ArrayList是Integer类型)。这在任何方面都是不正确的。

你需要这样做,

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

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