开发者

R. is not giving me an option for array

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 {
    /** Cal开发者_如何学编程led 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>


Just closing the project and re-opening it worked for me, array identifier appeared in the list options.


Go to Project -> Clean and then Rebuild. The R.java is not holding the resource you just added. EDIT: also make sure to import YourPackage.ProjectName.R;


Make sure you have built the project. Right click, build (or at the top, Project -> Build All).

It may not know about the array yet since you said you've just copied and pasted and therefore it is not registered as a valid resource.


You always say you put the strings in the string.xml-file. The file should be named strings.xml (with an "s" -> string s .xml).

It seams that it doesn't madder which name the file is being given, but it needs to be declared as a recourse-file by using the <resource>-element as the root-element for the XML-file and the file must be placed in the res/values-folder in your Project.

If there is a problem with the recourse-files, the ADB should give an Error-Message telling you what you should fix. In Eclipse, this shows up in the "Console"-View. Check if you get any error-message there.

You could also try to use only array instead of string-array (this always worked for me).


This is how it should look like: in strings.xml file under res/values:

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


If you want to be able to access it using R.array.countires_array, it needs to be put in array.xml

Since it's stored in string.xml, it will be located under R.string.countires_array


You are missing R.id.tv_id and this before setListAdapter Try it:

this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,  R.id.label, countries));
ListView lv =getListView();
//lv.setTextFilterEnabled(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜