0

Tell me how to implement this example, understand with List view , in strings :

<string name="ml350">300</string>
<string name="ml400">400</string>
<string name="ml600">600</string>
<string name="ml800">800</string>
<string name="ml1000">1000</string>

.

public static final String[] descriptions = new String[] {
        "300",
        "400", "600",
        "800","1000" };

ListView listView;
List<RowItem> rowItems;

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < descriptions.length; i++) {
        //RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
        RowItem item = new RowItem(R.drawable.ic_untitled_2, descriptions[i], descriptions[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.listview_item_row, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);

the problem is that when I try to substitute:

public static final String[] descriptions = new String[] {
        R.strings.300,
       R.strings.400, R.strings.600,
        R.strings.800,R.strings.1000 };

I receive an error.

1
  • consider making descriptions as non-static variable. Just declare the descriptions variable. and initialize it in onCreate() like descriptions = getResources().getStringArray(int). but here you have just declared Strings, not a string-array... Commented Mar 16, 2014 at 10:33

3 Answers 3

1

you must use:

R.strings.ml350,
R.strings.ml400,
R.strings.ml600,
R.strings.ml800,
R.strings.ml1000

instead of :

R.strings.300,
R.strings.400,
R.strings.600,
R.strings.800,
R.strings.1000 

and you need:

 getResources().getString("your key");

for first:

300 is a value of ml350, so you need key of that for accessing the value.

second:

R.string.Key return id of String but you need Value of String so you must access that with getResources().getString() method

and better way is create one array and call that in your activity.

//Edit

so your code must be like:

public static final String[] descriptions = new String[] {
        getResources().getString(R.strings.ml350),
        getResources().getString(R.strings.ml400),
        getResources().getString(R.strings.m1600),
        getResources().getString(R.strings.ml800,
        getResources().getString(R.strings.m11000) };

and you must call this after initializing your view because this need context of your layout, so if you call before onCreate() you got NPE

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks corrected his inattention, but it did not solve the problem
thank you! helped this code :final String [] descriptions = new String [] { getResources().getString(R.string.ml350), getResources().getString(R.string.ml400), getResources().getString(R.string.ml600), getResources().getString(R.string.ml800), getResources().getString(R.string.ml1000)};
1
<string-array name="system">
   <item>300</item>
   <item>400</item>
   <item>600</item>
   <item>800</item>
   <item>1000</item>
</string-array>

you can create an array in your xml and then call

 String[] descriptions  = getResources().getStringArray(R.array.your_string_array) //in example is system

1 Comment

getResources() is not a static method. You need an instance of the View to call it ...
0

R.strings.300 returns int type but your array requires String type, that's why you get error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.