如何在strings.xml中存储listview数组

时间:2013-12-09 10:20:59

标签: android xml arrays listview

我正在开发一个项目,其中我有一个listview,我的代码中有几个数组,但我想从一个单独的xml文件加载数组,这是“strings.xml”

但我无法弄明白该怎么做

这是我的活动

package com.Example.myapk;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.EditText;
 import android.widget.ListView;

 public class MainActivity extends Activity {

 private ListView list1;

 private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

 "item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1",       "item 2", "item3", "item 4"};

 EditText inputSearch; 
 ArrayAdapter<String> adapter;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 inputSearch = (EditText) findViewById(R.id.inputSearch); 
 list1 = (ListView) findViewById(R.id.listView01);
 adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array);
  list1.setAdapter(adapter);
  inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);   
}

@Override
 public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
    int arg3) {
// TODO Auto-generated method stub

}

 @Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub                          
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
 Intent myIntent = new Intent(MainActivity.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
 });   
}
}

3 个答案:

答案 0 :(得分:1)

将以下代码放在string.xml中

<string-array name="myArray">
   <item >Iphone</item>
   <item >Tutorials</item>
   <item >Gallery</item>
   <item >Android</item>
   <item >item 1</item>
</string-array>

然后getResources().getStringArray(R.array.myArray)将这些值作为字符串数组返回。所以在你的主要活动文件中

private ListView list1;
private String array[] = getResources().getStringArray(R.array.myArray);
EditText inputSearch; 
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    inputSearch = (EditText) findViewById(R.id.inputSearch); 
    list1 = (ListView) findViewById(R.id.listView01);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array);
    list1.setAdapter(adapter);
}

答案 1 :(得分:0)

要将数组存储在xml中,您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="myArray">
        <item>Iphone</item>
        <item>Tutorials</item>
        <item>Gallery</item>
        <item>Android</item>
    </string-array>
</resources>

然后在您的代码中,您可以访问它:

Resources res = getResources();
String[] myArray = res.getStringArray(R.array.myArray);

点击此处:http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

答案 2 :(得分:0)

strings.xml

    苹果手机     教程     画廊     Android的

如果需要,可添加更多项目。

在您的代码中

package com.Example.myapk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView list1;

    EditText inputSearch;
    **ArrayAdapter<CharSequence> adapter;**

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        list1 = (ListView) findViewById(R.id.listView01);
        **adapter = ArrayAdapter.createFromResource(this,               R.array.menu, android.R.layout.simple_list_item_1);**
        list1.setAdapter(adapter);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });
        list1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent myIntent = new Intent(MainActivity.this,
                        WebViewActivity.class);
                myIntent.putExtra("key", position);
                startActivity(myIntent);
            }
        });
    }
}