谢谢帮忙反正…
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>解决方法 好的,有了评论,我明白你需要什么,并为此编辑我的答案.你想在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>总结
以上是内存溢出为你收集整理的android:从字符串数组中获取项目,并在文本视图中逐个显示全部内容,希望文章能够帮你解决android:从字符串数组中获取项目,并在文本视图中逐个显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)