I am new to android development. I’m trying to have my string.xml file add the string instead of hard coding the string in the .java file.
I’m using this android developer tutorial and copied and pasted the code to make sure there are no typos.
The problem I am having is when I enter R.array does not show up as an option. I am not really sure what is wrong. Here is the Java-code. I have put a // by the lines that is giving me trouble:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HelloListActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries_array);
//The R. does not offer array as an option
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
ListView lv =getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?>parent, View view,
int posistion, long id){
Toast.makeText(getApplicationContext(), ((TextView)view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
and here is my strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
Go to
Project -> Cleanand then Rebuild. The R.java is not holding the resource you just added. EDIT: also make sure toimport YourPackage.ProjectName.R;