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>
R.string.information is just an int. If you want the actual String, use
map.put("name", getString(R.string.information));
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
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);
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
Related
I want hashmap to array
I create the hashmap
Map<Integer,File> selectedFiles = new Hashmap<>();
and put it some Map data
And I convert this hashmap's values to array so
File[] files = (File[]) selectedFiles.values().toArray();
But errors occur;
java.lang.Object[] cannot be cast to java.io.File[]
I know that when I want the hashmap's values to array, use .values.toArray() but maybe it is not corret;
This way is wrong?
Map<Integer, File> selectedFiles = new HashMap<>();
selectedFiles.put(1, null);
File[] files = selectedFiles.values().toArray(
new File[selectedFiles.size()]);
System.out.println(files);// We will get the object [Ljava.io.File;#15db9742
arrays. toArray without parameters creates an object array because the type information of the list is lost at runtime.
Please use below Code to convert HashMap to Array ,You can change String to File in your case.
//Creating a HashMap object
HashMap<String, String> map = new HashMap<String, String>();
//Getting Collection of values from HashMap
Collection<String> values = map.values();
//Creating an ArrayList of values
ArrayList<String> listOfValues = new ArrayList<String>(values);
// Convert ArrayList to Array
String stringArray[]=listOfValues.toArray(new String[listOfValues.size()])
Why we add hash map in array list.For example
HashMap<String,String> hashMap = new HashMap<String,String>();
hashMap.put("1","bmw");
hashMap.put("2","mercedez");
hashMap.put("3","audi");
hashMap.put("4","sunny");
ArrayList<HashMap<String,String>> arrayList =new ArrayList<HashMap<String,String>>();
arrayList.add(hashMap);
Can we directly fetch data from hashmap like hashMap("1"); like this
We can directly fetch data from hashmap like hashMap.get("1");
In arrayList I can add n number of values like the below code
RowOneCollection = new ArrayList<Button>((Arrays.asList(btn1,btn2,btn3,btn4)));
Similarly in hash map I am adding n number of vales like this
Map<String, String>map= new HashMap<String, String>();
map.put("bi","biology");
map.put("ma","maths");
like this I am adding n number of values.How I can add all the values in single line like the arraylist
You can put the data using For loop if you have 2 String arrays consisting of Keys and Values.
String[] keys = {"a","aa","aaa"}; //keep both array of same size.
String[] values = {"a1","aa2","aaa3"};
Map<String, String> map = new HashMap<String, String>();
for(int i = 0 ; i < keys.length; i++)
{
map.put(kays[i], values[i]);
}
Hope this helps.
You probably want to adjust how you are populating your HashMap as follows:
HashMap<String,xyz> hMap=new HashMap<String,xyz>();
for(int i=0;i<list.size();i++)
{
hMap.put("Data"+i, list);
}
Where xyz is the object, that you are working with. This approach allows you to take advantage of Java's Generics capability in its Collection objects such as HashMap.
In Xamarin, I am trying this tutorial: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Here is the code:
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.Put(key, name);
return planet;
}
May I please have some help with the correct using statements for the following types:
Map
ArrayList
HashMap
Thanks in advance
Map and ArrayMap are in java.util. ArrayList is in System.Collections.
I have the json array below which I am parsing and displaying it in listview row wise but I would like to combine/group same users to one row .Should I do it in android or mysql ? I am giving the working code for mysql too .I really appreciate any help.Thanks in Advance.
MYSQL:
http://sqlfiddle.com/#!2/19ea8/5
ANDROID:
wihin mainactivity for listview:
JSONArray arr = result.getJSONArray("JsonResultArray");
for(int i=0;i<arr.length();i++)
{
JSONObject e1 = arr.getJSONObject(i);
JSONObject json2 = (JSONObject) e1.get("data");
String name = json2.getString("name").trim();
String receiver = json2.getString("receiver").trim();
String sender = json2.getString("sender").trim();
String date = json2.getString("date").trim();
String msg = json2.getString("msg").trim();
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, Integer.toString(i+1));
map.put(KEY_TITLE, name);
map.put(KEY_msg, msg);
map.put(KEY_date,date);
// adding HashList to ArrayList
msgList.add(map);
}
}catch(Exception e){
e.printStackTrace();
}
in convert view:
HashMap<String, String> msgs = new HashMap<String, String>();
msgs = data.get(position);
name.setText(msgs.get(PopoverViewActivity.KEY_TITLE));
msg.setText(msgs.get(PopoverViewActivity.KEY_msg));
date1.setText(msgs.get(PopoverViewActivity.KEY_date));
If you want to group same users to one row. I suggest you to edit your mysql. You need to use
select * from table group by user
in your mqsql query, that can make you convenient to change your db and doesn't influence your UI.
In Android, just call your api and present data to listView.