i want to send below data in row form is like :
{
"sender_id":"A1234567",
"sender_name":"abc",
"division_name":["test D1","test D2"]
}
how can i send devision name in hashmap ?
HashMap<String, String> map = new HashMap<>();
map.put("sender_id", edSenderId.getText().toString().trim());
map.put("sender_name", edSenderName.getText().toString().trim());
map.put("division_name","..." );
You can send array like below code;
int childCount = move_st_list.getChildCount();
ArrayList<String> student_ids = new ArrayList<>();
// Here I m fetching all students ids from RecyclerView's adapter class....
for(int i = 0; i < childCount; i++)
{
MyAdapter.ViewHolder childHolder = (MyAdapter.ViewHolder) move_st_list.findViewHolderForLayoutPosition(i);
student_ids.add(studentAttendancePojo.getStudentSerchList().get(i).getiStudentId());
}
JSONArray jsArray = new JSONArray(student_ids);
Then pass it in map;
map.put("division_name",jsArray);
Related
I have room database in android. but when i am retrieving data from database and showing in expandable RecyclerView it shows only last records in my child list. there is one child ArrayList under the parent ArrayList.
I am adding data from database in hashmap in the key and value format in ExpandableListView
HashMap<String, List<Movies>> expandableListDetail = new HashMap<String, List<Movies>>();
ArrayList<String> moviecategoriess = new ArrayList<>();
for (int i = 0; i < tasks.size(); i++) {
childDataItems = new ArrayList<>();
String movieCategory = tasks.get(i).getMovieCategory();
String moviename = tasks.get(i).getMovieName();
String description = tasks.get(i).getMovieDescription();
String bannerimage = tasks.get(i).getMovieImage();
// String maincat=expandableListDetail.get("").get(i).
childDataItems.add(new Movies(moviename, bannerimage, description));
expandableListDetail.put(movieCategory, childDataItems);
moviecategoriess.add(movieCategory);
}
Iterator it = expandableListDetail.entrySet().iterator();
final ArrayList<MovieCategory> movieCategories = new ArrayList();
moviesArrayList = new ArrayList();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
String name = pairs.getKey().toString();
moviesArrayList = (ArrayList<Movies>) pairs.getValue();
movieCategories.add(new MovieCategory(name, moviesArrayList));
}
moviesAdapter = new MoviesAdapter(MovieActivity.this, movieCategories);
Functions.setDatatoRecyclerView(rv_movies_list, moviesAdapter, MovieActivity.this);
moviesAdapter.notifyDataSetChanged();
}
I want to show data in ArrayList like this
Parent Item: Thriller
ChildItems In List:
1) Force2
2) Sarkar2
Parent Item: Action
ChildItems In List:
1) Border
2) Battlefield
In your case problem is with for loop each time you run for loop a new array list is created as per your code so simply replace your code with this:
HashMap<String, List<Movies>> expandableListDetail = new HashMap<String, List<Movies>>();
ArrayList<String> moviecategoriess = new ArrayList<>();
childDataItems = new ArrayList<>();
for (int i = 0; i < tasks.size(); i++) {
String movieCategory = tasks.get(i).getMovieCategory();
String moviename = tasks.get(i).getMovieName();
String description = tasks.get(i).getMovieDescription();
String bannerimage = tasks.get(i).getMovieImage();
// String maincat=expandableListDetail.get("").get(i).
childDataItems.add(new Movies(moviename, bannerimage, description));
expandableListDetail.put(movieCategory, childDataItems);
moviecategoriess.add(movieCategory);
}
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);
}
}
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.
I'm trying to get elements from my json array.
this is my json response:
{"IDs":["635426812801493839","635429094450867472","635433640807558204"]}
This is what I've tried so far:
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
JSONObject obj = a.getJSONObject(i);
stringArray.add(obj.toString());
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());
But I'm getting empty list. What is wrong with my code? Any help will be truly appreciated. Thanks.
The for loop should be
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
your the JSONArray contains already string
JSONObject obj = a.getJSONObject(i);
replace with
String str = a.getString(i);
Use this
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());
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++) {
....
}