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>

private ListView list1;

private String array[] = getResources().getStringArray(R.id.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);

To store your array in xml, you can do this:

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

Then in your code, you can access it:

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

Check here: http://developer.android.com/guide/topics/resources/string-resource.html#StringArray


In your strings.xml

Iphone Tutorials Gallery Android

Add more items if you want.

In your code:

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

Need Your Help

display complex aggregated data structure in extjs

extjs extjs4

I have some complex data structure and am looking for a way to present them in a user friendly manner:

How to load current buffer into Python interpreter in Emacs?

python emacs

I'm trying to use emacs to edit and run python programs (emacs23 and python 2.6 on Ubuntu 10.10).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.