开发者

How to translate ListView texts?

My android application includes ListView binded with SimpleCursorAdapter to the database. This database contains column, which contains data like

somenameofrow
anothername
thirdname

However instead of directly displaying these texts in the ListView, I would like to read according texts (actually - translations) from the Android resources. How should I do it?

I think that I can use setViewValue for the same, name in the code below is TextView item, which displays my text:

case R.id.name:
    TextView elName = (TextView) view;
    elName.setText(getApplicationContext().getString(R.string.));
    return true;

I am not sure how to understand what is correct id of the resource I am looking for (probably, cu开发者_StackOverflow社区rsor.getString(columnIndex) or elName.getText()) and how to get the value?

Upd. What if I exclude names from the database and will use ids instead. Will it help?


If you read the android guide ListView tutorial, you will find:

Note that using a hard-coded string array is not the best design practice. One is used in this tutorial for simplicity, in order to demonstrate the ListView widget. The better practice is to reference a string array defined by an external resource, such as with a resource in your project res/values/strings.xml file

Simply create different string-arrays items in res/values-XX, where XX are the locales you want to translate.

See the dev guide for more details about resources and i18n: http://developer.android.com/guide/topics/resources/localization.html


No you can't, since id's are generated in some basically sem-random way during compilation, so you can't be sure which id will have specific String resource.

I would suggest to use translation from DB or you can read translations from assets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜