从字符串数组填充列表视图 [英] Populate a Listview from a String-Array

查看:24
本文介绍了从字符串数组填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,我在其中通过 String tutorialTitle1 = getResources().getString(R.string.tutorial1_title); 填充它,但是为了我的应用程序的目的和功能,我有别无选择,只能使用 String-Array

I have a ListView where I'm filling it in through String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);, but for the purpose and functionality of my app I have no choice but to use a String-Array

我从未使用过 String-Array,我需要一些帮助来实现我的 ListView 中的每个项目.目前我是这样做的

I've never worked with the String-Array and I'd like some help to implement every item in my ListView. At the moment I'm doing it this way

public class tutorialActivity extends Activity{
    String[] values; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial);
        registerClickCallBack();
        ListView listView = (ListView) findViewById(R.id.tutorialList);

        String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
        String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
        String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
        String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
        String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
        String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
        String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
        String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
        String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
        String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
        String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
        String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
        String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
        String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);

        values= new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter);


    }

    private void registerClickCallBack() {
        ListView list = (ListView) findViewById(R.id.tutorialList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {

                Intent i = new Intent(viewClicked.getContext(), tutorialContentActivity.class);
                i.putExtra("tutorialNumber", position);
                startActivity(i);
            }
        });
    }
}

但是现在 R.string.tutorial1_title 不存在了,因为我把它像

But now R.string.tutorial1_title doesn't exist anymore because I've put it like

<string-array name="tutorialTitle">
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
    <item>Tutorial Content</item>
</string-array>

所以基本上我怎样才能用我的 <string-array name="tutorialTitle">

So basically how can I populate my Listview with all the items I have in my <string-array name="tutorialTitle">

推荐答案

调用 getResources().getStringArray(R.array.tutorialTitle) 来检索 String[]与您的 string-array 资源相关联.

Call getResources().getStringArray(R.array.tutorialTitle) to retrieve the String[] associated with your string-array resource.

这篇关于从字符串数组填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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