如何从字符串数组填充列表视图

时间:2014-03-01 21:54:54

标签: android arrays

我在listview中填写了主要活动中的项目,这是MainActivity.class数据

public class MainActivity extends Activity {

// Declare Variables
ListView list;
ListViewAdapter adapter;
EditText editsearch;
String[] office_name;
String[] town_name;
String[] phone_number_01;
String[] phone_number_02;
String[] fax_number_01;
String[] fax_number_02;
ArrayList<ISF_LandLines> arraylist = new ArrayList<ISF_LandLines>();

@Override
public void onCreate(Bundle savedInstanceState) {
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);
    // Generate sample data
    office_name = new String[] { "فصيلة سن الفيل", "", "", "", "", "" };

    town_name = new String[] { "حرش تابت", "", "", "", "", "" };

    phone_number_01 = new String[] { "00961-1-511989", "------", "------",
            "------", "------", "------" };

    phone_number_02 = new String[] { "------", "------", "------",
            "------", "------", "------" };

    fax_number_01 = new String[] { "00961-1-480757", "------", "------",
            "------", "------", "------" };

    fax_number_02 = new String[] { "------", "------", "------", "------",
            "------", "------" };

    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.listview);

    for (int i = 0; i < office_name.length; i++) {
        ISF_LandLines wp = new ISF_LandLines(office_name[i], town_name[i],
                phone_number_01[i], phone_number_02[i], fax_number_01[i],
                fax_number_02[i]);
        // Binds all strings into an array
        arraylist.add(wp);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (EditText) findViewById(R.id.search);

    // Capture Text in EditText
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

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

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

}
}

但是如何从strings.xml

填充我的列表视图中的列表视图

因为使用字符串数组

要容易得多

1 个答案:

答案 0 :(得分:0)

values文件夹中添加xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<array name="myresource">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
</array>
</resources>

在您的代码中:

String[] mystringArray = getResources().getStringArray(R.array.myresource);