2

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

我从来没有使用过String-Array,我想要一些帮助来实现我的 ListView 中的每个项目。目前我正在这样做

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不存在了,因为我把它说成

<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">

4

1 回答 1

10

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

于 2013-06-10T23:19:11.423 回答