How to Compare String of HashMap and arrange them? - android

Supose i have name Mink,Mark,Aashis. How do compare them and arrange them according to alphabetical order. Collections.sort() dont work for me. Here i should have results Aashis,Mark and Mark.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
Collections.sort( mylist, new Comparator<HashMap<String,String>>(){
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
//how do i compare string name
return 0;
}
});

Collections.sort(list, new Comparator(){
public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
return (o1.get("name")).compareToIgnoreCase(o2.get("name"));
}
Here "name" is the Key that you have provided at the time of insertion in the HashMap using put method.
EXTRA
If you want to parse double values then you can use the Double.parse() method.
return Double.compare(o1.get(value1), o1.get(value2));
NOTE - The nice thing about this approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dataOfBirth you could define different implementations of Comparator and sort the objects according to your needs.
Hope this will help you.

try this code
insert values
public ArrayList<HashMap<String, String>> list;
list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 5; i++) {
HashMap<String, String> info = new HashMap<String, String>();
info.put("title", "gdfgdfgdfgdfggdfgfdgdgdgdffghfghfgh");
info.put("time", "44:55");
info.put("view", "54,353");
list.add(info);
}
get and compare
String title1 = ketan;
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map = list.get(i);
if(map.get("title")==title1);
{
........
......
}
}

Related

Android adding to ArrayList replace older items and i have the same data

in below code i'm trying to add some data to items, on each adding data into for clause value of items have the same data, but in log cat there are different result.
ArrayList<HashMap<String, String>> items = new ArrayList<>();
HashMap<String, String> item = new HashMap<>();
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}
problem is adding data to items in this line of code: items.add(item);
how can i resolve this problem?
Try this
ArrayList<HashMap<String, String>> items = new ArrayList<>();
HashMap<String, String> item = null;
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
item = new HashMap<>();
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}
When ever if condition becomes false create new HashMap<>()
Move item initialization inside the for loop.
Having it initialized outside will keep on adding food beans in for-loop to same item Map instance and finally you would have a single Map item in items ArrayList instead of having multiple Map objects inside the ArrayList.
ArrayList<HashMap<String, String>> items = new ArrayList<>();
for (int p = 0; p < allFoodBean.size(); p++) {
if (allFoodBean.get(p).getItemId().equals("food1")) {
HashMap<String, String> item = new HashMap<>();
item.put("id", allFoodBean.get(p).getId());
item.put("name", allFoodBean.get(p).getName());
items.add(item);
}
}

Android get the Key in Hashmap in Arraylist within Listview CHOICE_MODE_MULTIPLE

I parse json data and display it in listview with CHOICE_MODE_MULTIPLE. I trying to get the key of a Hashmap in my case but I only manage to get the value.
Json data
57->Steak Doness
58->Size
59->Cooking Method
63->Coldness
Here is the coding
String option_data = jsonObject.getString("option_data");
JSONObject jsonObject1 = new JSONObject(option_data);
Iterator<String> iterator = jsonObject1.keys();
ArrayList arrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
while (iterator.hasNext()) {
String id = iterator.next();
String name = jsonObject1.getString(id);
map = new HashMap<String, String>();
map.put("id", id);
map.put("name", name);
arrayList.add(name);
}
ArrayAdapter adapter = new ArrayAdapter<String>(item_add.this, android.R.layout.simple_list_item_multiple_choice, arrayList);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String selected = "";
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
if (sparseBooleanArray.get(i)) {
selected += listView.getItemAtPosition(i).toString() + ",";
}
}
Log.d("selected", selected);
}
});
With the logcat, it shows
D/selected: Steak Doness,Size,
However, what I want to get is the Key in Hashmap which means "id" when click the button . Anyone knows how to do it?
Use the following way to iterate and find values of keys
HashMap <String,String> mapOne= new HashMap();
mapOne.put("1","1value");
mapOne.put("2","2value");
mapOne.put("3","3value");
for(Map.Entry<String, String> mapEntry : mapOne.entrySet()){
if (mapEntry.getValue().equals("3value")) {
//System.out.println(mapEntry.getKey());
Log.d("selected", mapEntry.getKey());
}
}

sum for loop quantity values?

i have this code..
JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");
cartarraylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
HashMap<String, String> lmap = new HashMap<String, String>();
JSONObject p = jsonarrayCartProducts.getJSONObject(i);
// Retrive JSON Objects
lmap.put("products_id", p.getString("products_id"));
lmap.put("products_name", p.getString("products_name"));
lmap.put("products_price", p.getString("products_price"));
lmap.put("products_image", p.getString("products_image"));
lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
lmap.put("products_price_total", p.getString("products_price_total"));
lmap.put("pcustomersid", customersid);
lmap.put("pcountryid", countryid);
lmap.put("customers_basket_id", p.getString("customers_basket_id"));
// Set the JSON Objects into the array
cartarraylist.add(lmap);
}
i want to sum the quantity p.getString("customers_basket_quantity") and set to my textview..
i tried to do create an
int qtySum=0;
int qtyNum;
and do this inside for loop..
qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
qtySum += qtyNum;
and set qtySum to my textview
textTotalitems.setText(qtySum);
but i got error, the app crashed..
this is updated code with sum i tried..
JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");
cartarraylist = new ArrayList<HashMap<String, String>>();
int qtySum=0;
int qtyNum;
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
HashMap<String, String> lmap = new HashMap<String, String>();
JSONObject p = jsonarrayCartProducts.getJSONObject(i);
// Retrive JSON Objects
lmap.put("products_id", p.getString("products_id"));
lmap.put("products_name", p.getString("products_name"));
lmap.put("products_price", p.getString("products_price"));
lmap.put("products_image", p.getString("products_image"));
lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
lmap.put("products_price_total", p.getString("products_price_total"));
lmap.put("pcustomersid", customersid);
lmap.put("pcountryid", countryid);
lmap.put("customers_basket_id", p.getString("customers_basket_id"));
// Set the JSON Objects into the array
qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
qtySum += qtyNum;
cartarraylist.add(lmap);
}
textTotalitems.setText(qtySum);
Use
textTotalitems.setText(String.valueOf(qtySum));
instead of
textTotalitems.setText(qtySum);
With your current implementation your trying to set a resource-Id to your TextView, because TextView has an overloaded setText(int resId)-method.

How to Convert ArrayList<HashMap<String, String>> to a StringArray?

I want to store a String array in the database, but I got values from another activity as an ArrayList<Hashmap<String, String>>. How should I convert this ArrayList into a String array?
Code:
HashMap <String, String> h1 = new HashMap <String, String> ();
h1.put("member_id", member_id);
h1.put("categoryname", category_name);
h1.put("routinetype", routine_type);
h1.put("targettype", target_type);
h1.put("days", countOfDays);
h1.put("point", points);
h1.put("daystring", listOfDayString);
h1.put("create_date", strDate);
arrayList1.add(h1);
Simply use:
String[] strArray = new String[urArrayList.size()];
strArray = urArrayList.toArray(strArray);
EDIT:
Then you can simply use this:
int i = 0;
String strArr[] = new String[urArrayList.size()];
for (HashMap<String, String> hashMap : urArrayList) {
for (String value : hashMap.values()) {
strArr[i] = value ;
i++;
}
}

Issue converting JSON to HashMap in Android

I have JSONArray and when I decode JSON to HashMap at that time HashMap take last value of JSONArray.
here my code:
QjArray = new JSONArray(Questionresult);
JSONObject json_data = new JSONObject();
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
json_data = QjArray.getJSONObject(i);
jQuestionName =json_data.getString("QuestionName");
objJMap.put("QuestionName",jQuestionName);
jQuestiontypeid = json_data.getInt("Questiontypeid");
String Qid = jQuestiontypeid.toString();
objJMap.put("Questiontypeid", Qid);
jAnswertypeid = json_data.getInt("Answertypeid");
String Aid = jAnswertypeid.toString();
objJMap.put("Answertypeid", Aid);
}
My JSONArray:
This is question list[{"QuestionID":"1","QuestionName":"when you come","Questiontypeid":"1","Answertypeid":"1"},{"QuestionID":"2","QuestionName":"about your words","Questiontypeid":"1","Answertypeid":"2"},{"QuestionID":"3","QuestionName":"you want extra service?","Questiontypeid":"1","Answertypeid":"3"},{"QuestionID":"4","QuestionName":"performance of quality ?","Questiontypeid":"1","Answertypeid":"4"},{"QuestionID":"5","QuestionName":"performance of staff?","Questiontypeid":"1","Answertypeid":"5"},{"QuestionID":"6","QuestionName":"when you left room ?","Questiontypeid":"2","Answertypeid":"1"},{"QuestionID":"7","QuestionName":"your words about roomservice ?","Questiontypeid":"2","Answertypeid":"2"},{"QuestionID":"8","QuestionName":"you like roomservice ?","Questiontypeid":"2","Answertypeid":"3"},{"QuestionID":"9","QuestionName":"performance room service ?","Questiontypeid":"2","Answertypeid":"4"},{"QuestionID":"10","QuestionName":"performance room service staff?","Questiontypeid":"2","Answertypeid":"5"}]
I think there are certain problems in your logic. For every JSON object u are creating new HashMap object inside for loop, so u will loose any previous data. Also HashMap will override new Data, so you will have only final data. What u can do is that create an arrayList of Hashmap....
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<STring, String>>();
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
........
data.add(objJMap);
...}
This is because your code re initializes the HashMap after every loop.
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
....
}
Place the HashMap outside the loop and it will work fine.
objJMap = new HashMap<String, String>();
for (int i = 0; i<QjArray.length(); i++) {
....
}

Categories

Resources