0

I m using hashmap for a listview.

    map = new HashMap<String, Object>();
    map.put("name", R.string.information);
    map.put("address", R.drawable.info3);

    mylist.add(map);
    // ...

my problem is that i cant load string in my hashmap (R.string.information)..when i run it,i can only see some numbers instead of my text..am i doing something wrong?thanks

        <string name="information">Informations</string>

4 Answers 4

1

R.string.information is just an int. If you want the actual String, use

map.put("name", getString(R.string.information));
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for your answer..but this happens only in the hashmap?i mean, everywhere in my app i m getting data from the string.xml and i have no problem..
This is the case everywhere in code. In xml, you can use just the identifier, like android:text=@string/information.
dieuthinsi.setText(R.string.dieuthinsi1 );
No, it will not. As I said, R.string.* are just ints. You can't pass an int to a method that wants a String and expect to work. You must first obtain the String associated with that identifier, using getString(int identifier) method, then pass this String to setText.
i have tried it and its working...whats going on please?i m sorry if i m nerd but i m trying to understand
|
1

string.information returns a integer instead use

getString(R.string.information);

which will give you the string that you have stored in the String.xml

Comments

0

Yes you are doing wrong because you have print the id the the name and address not value you should put value of the name and address tag using this one according to your requirement.

String mess = getResources().getString(R.string.name);    
this.getString(R.string.name)//if you are in context    
String mess = Resources.getText(R.string.name);

Comments

0
R.string.information gives you int value not String

You have to make change in creating hashmap Your hasmap should be

HashMap map = new HashMap<String, Integer>();  instead of `HashMap map = new HashMap<String, Object>();`

Finally code wil be

HashMap map = new HashMap<String, Integer>();
map.put("name", R.string.information);
map.put("address", R.drawable.info3);

mylist.add(map);

Thanks Deepak

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.