How to populate a hashmap from an array - java

i have two arrays (actually one, but i created two for each columns). I want to populate a hashmap with the values for a listview but all elements of the listview is the last element of the arrays:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int i=0; i<13; i++)
{
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);
Any ideas?
Thanks

You're adding the same map to every slot of the array. Try this instead:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i=0; i<13; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("left1", date[i]);
map.put("right1", name[i]);
mylist.add(map);
}

Related

Two-line ListView does not show content

I currently have a problem with my two-line ListView. The two-line ListView shows the list separator, however, there is no data or text inside it. Where could I possibly have done wrong?
For additional information, the results.get(i) will show Tungro,18.92%, for example.
ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
HashMap<String, String> listItemData;
for (int i=0; i<results.size(); i++) {
if (results.size() != 0) {
listItemData = new HashMap<String, String>();
String resultStr = results.get(i).toString();
String[] resultStrVar = resultStr.split(",");
listItemData.put(resultStrVar[0], resultStrVar[1]);
listItems.add(listItemData);
} else {
listItemData = new HashMap<String, String>();
listItemData.put("No predictions found", "Kindly shot again");
listItems.add(listItemData);
}
}
SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
android.R.layout.simple_list_item_2,
new String[] {"First Line", "Second Line"},
new int[] {android.R.id.text1, android.R.id.text2 });
listView.setAdapter(adapter);
Below is the screenshot of the ListView, where you could see the list separator but without text or data:
Is there something wrong with how I tokenize the string or with the adapter? Thanks a lot.
Thanks Mike M! I have changed my code to the following:
ArrayList<HashMap<String, String>> listItems = new ArrayList<>();
HashMap<String, String> listItemData;
for (int i=0; i<results.size(); i++) {
if (results.size() != 0) {
listItemData = new HashMap<String, String>();
String resultStr = results.get(i).toString();
String[] resultStrVar = resultStr.split(",");
listItemData.put("disease_name", resultStrVar[0]);
listItemData.put("confidence", resultStrVar[1]);
listItems.add(listItemData);
} else {
listItemData = new HashMap<String, String>();
listItemData.put("disease_name", "No predictions found");
listItemData.put("confidence", "Kindly shot again");
listItems.add(listItemData);
}
}
SimpleAdapter adapter = new SimpleAdapter(Results.this, listItems,
android.R.layout.simple_list_item_2,
new String[] {"disease_name", "confidence"},
new int[] {android.R.id.text1, android.R.id.text2 });
listView.setAdapter(adapter);

How can we get the data from map in a spinner when we select the city and internally select the value of that filed

When we select the spinner the internally we use the id in this spinner
stringArray = new ArrayList<String>();
myList = new ArrayList<HashMap<String,String>>();
for(int i=0; i<createdtrs_array.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", createdtrs_array.getJSONObject(i).getString("id"));
map.put("name", createdtrs_array.getJSONObject(i).getString("name"));
myList.add(map);
stringArray.add(createdtrs_array.getJSONObject(i).getString("id"));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(BusTickets.this,android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
destnation.setAdapter(adapter);

ArrayList of HashMaps to string, and then back to ArrayList of HashMaps

I have a ArrayList<HashMap<String,String>> that I am using the toString() method on to store in a database.
Here is the code that I use to store it the toString() to a database (it works):
HashMap<String, String> commentsHash = null;
ArrayList<HashMap<String, String>> test2 = new ArrayList<HashMap<String, String>>();
for (int i=0; i < test.size(); i++)
{
String timestamp = test.get(i).get("timestamp");
String last_name = test.get(i).get("last_name");
String first_name = test.get(i).get("first_name");
String comment = test.get(i).get("comment");
commentsHash = new HashMap<String, String>();
commentsHash.put("creation_timestamp", timestamp);
commentsHash.put("first_name", first_name);
commentsHash.put("last_name", last_name);
commentsHash.put("comment", comment);
test2.add(commentsHash);
}
dbHelper.addCommentsToMyLiPost(Integer.parseInt(sqlId), test2.toString());
Here is the method I want to use to convert a string to a HashMap<String, String>:
protected HashMap<String,String> convertToStringToHashMap(String text){
HashMap<String,String> data = new HashMap<String,String>();
Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
String[] split = p.split(text);
for ( int i=1; i+2 <= split.length; i+=2 ){
data.put( split[i], split[i+1] );
}
return data;
}
I have tried using .split(",") on the string to split the string into 2 parts, but instead of returning two, it returns 8.
Here is what the toString() method prints. It is an ArrayList of HashMaps, and I am trying to grab the two HashMaps that are inside of the ArrayList.
[{comment=hello, last_name=u1, first_name=u1, creation_timestamp=1404938643772}, {comment=hello2, last_name=u2, first_name=u2, creation_timestamp=1404963221598}]
In convertToStringToHashMap, when you put your data into HashMap, the old value will be replaced since they have same key for each records, such as comment, last_name, etc.
public static Map<String, Map<String, String>> convertToStringToHashMap(String text)
{
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
String[] split = p.split(text);
Map<String, String> data = new HashMap<String, String>();
int gap = 8;
int key = 1;
for (int i = 1; i + 2 <= split.length; i += 2)
{
data.put(split[i], split[i+1]);
if((i + 1) % gap == 0)
{
map.put(String.valueOf(key++), data);
data = new HashMap<String, String>();
data.clear();
}
}
return map;
}
This will return a Map:
2={first_name=u2, last_name=u2, comment=hello2, creation_timestamp=1404963221598}
1={first_name=u1, last_name=u1, comment=hello, creation_timestamp=1404938643772}
This program will recreate the whole list from the database entry
Pattern firstPat = Pattern.compile("\\{.*?\\}");
Matcher firstMat = firstPat.matcher(text);
ArrayList<HashMap<String, String>> list = new ArrayList<>();
while(firstMat.find()){
HashMap<String, String> map = new HashMap<>();
String assignStrings = firstMat.group();
String [] assignGroups = assignStrings.substring(1,assignStrings.length()-1).split("\\s*\\,\\s*");
for(String assign:assignGroups){
String [] parts = assign.split("\\=");
map.put(parts[0], parts[1]);
}
list.add(map);
}
return list

ArrayList<HashMap<String,String>> to String[]

i have data fetched from my webservice in
ArrayList<HashMap<String,String>>
Now i want to convert each object of the above to
String[]
how do i do this?
any help would be much appreciated!
try
ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
HashMap<String, String> n = new HashMap<String, String>();
n.put("a", "a");
n.put("b", "b");
test.add(n);
HashMap<String, String> m = test.get(0);//it will get the first HashMap Stored in array list
String strArr[] = new String[m.size()];
int i = 0;
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
strArr[i] = current;
i++;
}
}
The uses for an Hashmap should be an Index of HashValues for finding the values much faster. I don't know why you have Key and Values as Strings but if you only need the values you can do it like that:
ArrayList<HashMap<String, String>> test = new ArrayList<>();
String sum = "";
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
sum = sum + current + "<#>";
}
}
String[] arr = sum.split("<#>");
It's not a nice way but the request isn't it too ;)
ArrayList<HashMap<String, String>> meterList = controller.getMeter();
HashMap<String, String> mtiti = meterList.get(0);//it will get the first HashMap Stored in array list
String[] strMeter = new String[mtiti.size()];
String meter = "";
for (HashMap<String, String> hash : meterList) {
for (String current : hash.values()) {
meter = meter + current + "<#>";
}
}
String[] arr = meter.split("<#>");

how to get hashmap content of arraylist in java?

I am using the following code to save hashmap content into arraylist.
HashMap jediSaber = new HashMap();
ArrayList<HashMap> valuesList = new ArrayList();
for(int i = 0; i< 4;i++) {
jediSaber.put("white","white_name"+i);
jediSaber.put("blue","blue_name"+i);
valuesList.add(i, jediSaber);
System.out.println("list ontent:"+i+":"+valuesList.get(i).values());
}
`
output is as follows:
list content:0:[blue_name0, white_name0]
list content:1:[blue_name1, white_name1]
list content:2:[blue_name2, white_name2]
list content:3:[blue_name3, white_name3]
When i try to display the content of arraylist in outside with the following code,
System.out.println("list content:");
for(int i = 0;i<valuesList.size();i++){
System.out.println("list:"+i+":"+valuesList.get(i).values());
}
It is showing the following output,
list content:0:[blue_name3, white_name3]
list content:1:[blue_name3, white_name3]
list content:2:[blue_name3, white_name3]
list content:3:[blue_name3, white_name3]
My problem is i need to display the content of arraylist of hashmap.
I think something i missed in second part. Can anybody help me to solve this minor issue?
Thanks in advance!!..
This is adding the same HashMap each time to the ArrayList:
valuesList.add(i, jediSaber);
Create a new HashMap each time within the for and add it:
List<HashMap<String, String>> valuesList =
new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 4; i++)
{
HashMap<String, String> m = new HashMap<String, String>();
m.put("white", "white_name" + i);
m.put("blue", "blue_name" + i);
valuesList.add(m);
}
System.out.println(valuesList.toString());
List<Map> valuesList = new ArrayList();
for (int i = 0; i < 4; i++) {
Map<Object, Object> jediSaber = new HashMap<>();
jediSaber.put("white", "white_name" + i);
jediSaber.put("blue", "blue_name" + i);
valuesList.add(jediSaber);
Set<Entry<Object, Object>> entrySet = jediSaber.entrySet();
for (Entry<Object, Object> entry : entrySet) {
System.out.println(entry.getKey() + "-" + entry.getValue());
}
}
Try pulling jediSaber inside your for loop, like so:
for(int i = 0; i < 4; i++) {
Map<String, String> jediSaber = new HashMap<String, String>();
You should also parameterize valuesList too:
List<Map<String, String>> valuesList = new ArrayList<Map<String, String>>();
P.S. There's no need to call add(i, jediSaber) with the index argument: valuesList.add(jediSaber) will have the same effect.

Categories

Resources