Johnny Johnny - 5 months ago 27
Android Question

Android string-array from xml file to ListView

First of all, thank you for your answers! I'm new for Android and this is my problem;

I want to take values of string array to listview.

Program works fine with these codes:

private String [] fiilliste={"A","A─člamak","Almak","B","Bulmak"};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste);
vlist.setAdapter(adapter);


But I wanna use xml file for string-array. This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="fi">
<item>A</item>
<item>Almak</item>
<item>Anlamak</item>
<item>Anlasmak</item>
<item>Anlatmak</item>
<item>Ayrilmak</item>
</string-array>

</resources>


and I just changed the code with following code, and now program doesn't work.

String [] fiilliste= getResources().getStringArray(R.array.fi);


Can you help me to solve this problem? Thank you!

Activity codes:

package com.gazi.tezproje;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class verbs extends Activity {

public boolean onCreateOptionsMenu(Menu menu) {
ActionBar bar=getActionBar();
bar.setDisplayHomeAsUpEnabled(true);

MenuInflater menu1 = getMenuInflater();
menu1.inflate(R.menu.info, menu);


return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuinfo:
Toast.makeText(getApplicationContext(),getString(R.string.verbs), Toast.LENGTH_LONG).show();
break;
case android.R.id.home:
finish();
break; }
return false;}


private ListView vlist;
EditText ara;


String [] fiilliste= getResources().getStringArray(R.array.fi);
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbs);

vlist=(ListView)findViewById(R.id.listView1);

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste);

vlist.setAdapter(adapter);
ara = (EditText) findViewById(R.id.editText1);
vlist.setTextFilterEnabled(true);
ara.addTextChangedListener(new TextWatcher(){

public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text

verbs.this.adapter.getFilter().filter(cs);
}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub

}

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

}
});

}
}

Answer

Change to

String [] fiilliste;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verbs);
    fiilliste = getResources().getStringArray(R.array.fi); 
    // move this in onCreate

I assume you have the string array fi in strings.xml.

You probably got NullPointerException. You require activity context for getResources(). So move it inside onCreate.