How to store listview arrays in strings.xml

im working on a project in which i have a listview and i have few arrays in my code but i want to load arrays from a separate xml file which is "strings.xml"

but i cannot figure out how to do it

here is my activity

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);
}
 });   
}
}

Answers


Put below code in you string.xml

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

Then getResources().getStringArray(R.array.myArray) will return those values as an array of strings. So in your main activity file

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);
}

Need Your Help