如何从Android上的字符串数组读取项目 [英] how to read items from string-array on android

查看:324
本文介绍了如何从Android上的字符串数组读取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字符串数组中逐项读取项目时遇到问题.例如,我得到了包含10个项目的字符串数组:

I got a problem with reading items from string-array one by one. For example, i got string-array with 10 items:

<string-array name="arr">
    <item>First</item>
    <item>Second</item>
    <item>...</item>
    <item>Tenth</item>
</string-array>

所以我知道如何使用此代码随机显示项目

So i know how to display items randomly, im using this code

Resources res = getResources();

myString = res.getStringArray(R.array.arr);

int length=myString.length;
int index=rgenerator.nextInt(length);
String q = myString[index];

tv = (TextView) findViewById(R.id.text);
tv.setText(q);

然后在TextView中的每个按钮上单击,它将显示数组中的随机项.

And in TextView on every button click it displays random item from array.

问题是,如何不随机地从字符串数组中显示项目.就像,它从显示"First"开始,然后单击它显示"Second",依此类推,直到数组的最后.
请帮忙!

Problem is, how to make display item from string-array not randomly. Like, it starts from displaying First, then on click it displays Second, and so on untill end of array.
Please help!

推荐答案

声明变量

int currentIndex=0;

onClick方法之外.

onClick(View v)
{
//Verify if only that btn is clicked
{
tv.setText(myString[(currentIndex++)%(myString.length)]);
}
}

希望它能起作用.

这篇关于如何从Android上的字符串数组读取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆