从数组中挑选项目

时间:2016-01-13 17:39:42

标签: java android arrays

我有这个数组,它包含12个项目,我随机化数组并希望随机选择一个问题,唯一的问题是什么都没有回来,我不知道为什么,有什么帮助吗?

我的阵列:

<array name="OneTimesTables">
    <item>1 x 1 = </item>
    <item>1 x 2 = </item>
    <item>1 x 3 = </item>
    <item>1 x 4 = </item>
    <item>1 x 5 = </item>
    <item>1 x 6 = </item>
    <item>1 x 7 = </item>
    <item>1 x 8 = </item>
    <item>1 x 9 = </item>
    <item>1 x 10 = </item>
    <item>1 x 11 = </item>
    <item>1 x 12 = </item>
</array>

我调用数组的代码:

CurrectQuestions = (TextView)findViewById(R.id.CurrentQuestion);

final TypedArray MathsArray = getResources().obtainTypedArray(R.array.OneTimesTables);
ArrayList<Integer> RandomNumbers = new ArrayList<>();
for (int i = 0; i < 12; i++){
    RandomNumbers.add(i);
}

Collections.shuffle(RandomNumbers);
int i = 0;

final int Question = MathsArray.getResourceId(RandomNumbers.get(i++), -1);
Toast.makeText(getApplicationContext(), "Question " + Question, Toast.LENGTH_SHORT).show();
CurrectQuestions.setText(Question);

1 个答案:

答案 0 :(得分:1)

我已经添加了代码,你可以从数组中获取随机文本,你可以调整它以获得随机播放列表以及你在你面前的选择,

这是代码,它也可以在github

上找到
public class RandomTestActivity extends Activity {

    private String[] strings;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random_test);

        strings = getResources().getStringArray(R.array.OneTimesTables);
        textView= (TextView)findViewById(R.id.RandomTestActivity_textView);
        Button button = (Button)findViewById(R.id.RandomTestActivity_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = (new Random()).nextInt(12);
                textView.setText(""+strings[count]);
            }
        });


        initializeUI();
    }

    private void initializeUI() {
        ArrayList<String> list = new ArrayList<>();

        for(String item: strings){
            list.add(item);
        }

        Collections.shuffle(list);

        for (String item:list){
            Log.d("TAG",""+item);
        }
    }

}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="activities.list.first.RandomTestActivity">

    <TextView
        android:id="@+id/RandomTestActivity_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/RandomTestActivity_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Next"
        android:textAllCaps="false" />
</LinearLayout>
  

输出   enter image description here