10

正如我在标题中写的那样,我需要一些帮助来从字符串数组中获取项目并在文本视图中一一显示我有代码将它们全部显示在列表视图中,但我需要每次在文本视图中随机显示它们我的代码很抱歉我的英语不好,无论如何感谢您的帮助...

public class MainActivity extends ListActivity {

String[] mTestArray;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Create an ArrayAdapter that will contain all list items
        ArrayAdapter<String> adapter;

        mTestArray =   getResources().getStringArray(R.array.planets_array);    

        /* Assign the name array to that adapter and 
        also choose a simple layout for the list items */ 
        adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                mTestArray);

        // Assign the adapter to this ListActivity
        setListAdapter(adapter);
    }
}

XML 文件:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@array/planets_array"
/>
</RelativeLayout>

和字符串数组文件:

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>
4

1 回答 1

21

好的,通过评论,我了解您的需求并为此编辑了我的答案。您想在 textView 中随机显示数组的值。

使用此活动:

public class MainActivity extends Activity {

    String[] mTestArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample);

        mTestArray =   getResources().getStringArray(R.array.planets_array);    

    }

    @Override
    protected void onResume() {
        super.onResume();
        updateTextView();
    }

    private void updateTextView() {
        TextView textView = (TextView)findViewById(R.id.randomTextView); 
        Random random = new Random();

        int maxIndex = mTestArray.length;
        int generatedIndex = random.nextInt(maxIndex);

        textView.setText(mTestArray[generatedIndex]);   
    }
}

将此布局放在 res/layout 文件夹下,并将其命名为 sample.xml。

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
            android:id="@+id/randomTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>
</RelativeLayout>
于 2013-10-27T11:31:06.860 回答