2013-10-27 21 views
9

當我在標題中寫入時,我需要一些幫助以獲取字符串數組中的項目,並在文本視圖中逐個顯示它我有代碼所有在列表視圖,但我需要一個每個隨機這裏我的代碼和對不起我的英語不好 ,感謝您的幫助反正時間顯示他們在文本視圖一個...android:從字符串數組中獲取項目並在文本視圖中逐個顯示

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> 
+0

你想讓它們隨機或按順序顯示嗎? –

+0

請注意。檢查我的答案。如果你的問題是回答請。接受答案。 :) – lokoko

+0

你不想要一個listView,對不對?只有一個textView隨機顯示你的數組值? – Devrim

回答

19

好的,帶着評論我明白你需要什麼並編輯我的答案。你想隨機顯示你的數組的值。

使用此活動:

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 /佈局文件夾並將其命名爲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> 
+0

感謝您的幫助,是的,我想讓它顯示隨機值每次應用程序啓動時,當應用程序再次運行它給另一個隨機值我希望你明白什麼 –

+0

所以你不想要一個listView? – Devrim

+0

不,我不需要列表查看我需要的所有文本視圖顯示從字符串數組中隨機值1每次我運行此應用程序..感謝回覆:) –

相關問題