ArrayList of objects are not setting in new ArrayList object - android

try {
JSONObject jsonObject = new JSONObject(botResponse.getResponseStringUrl());
JSONArray array = jsonObject.getJSONArray("value");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
String seq = jo.getString("sequenceNo");
Log.d("sequenceNo", seq + "iteration" + i);
lo = new ListObject();
lo.setStaffID(jo.getString("staffID"));
lo.setStatus(jo.getString("status"));
lo.setSequenceNo(jo.getString("sequenceNo"));
lo.setReason(jo.getString("reason"));
lo.setFrom(jo.getString("from"));
lo.setDays(jo.getString("days"));
lo.setLeaveType(jo.getString("leaveType"));
lo.setTo(jo.getString("to"));
lo.setName(jo.getString("name"));
developersLists.add(lo);
botresponselist.add(lo);
}
message.setMessageList(botresponselist);
message.setDevelopersList(developersLists);
}
developerlist's all multiple values are not setting in message.setDevelopersList. as setDevelopersList is new arraylist created in message class. only first object is getting set not all the object. but object lo is displaying multiple values. Thank you in Advance

The problem is you are creating new instance of ListObject and saving in the same variable due to this it will store only one value.
You have to create a new ListObject instance within the for loop.
Just add this in the for loop, instead of declaring the ListObject outside the for loop.
ListObject lo = new ListObject();

Related

why Setter Getter class variable only gets the last object from JSON, android,recyclerView

I just want to know why my Setter Getter class variable only gets the last object from JSON? it is supposed to fetch all json objects to setter getter variable so that it could transfer it to recyclerview.
Here is part of the Code, Im a begginer, pls help
JSONArray json = req
.getJSONArray("worldpopulation");
for (int i = 0; i < json.length(); i++) {
JSONObject jsonOb = json
.getJSONObject(i);
setterGetterClass = new SuperHeroes(jsonOb.getInt(Configg.TAG_rank),jsonOb.getString(Configg.TAG_country),jsonOb.getString(Configg.TAG_population),jsonOb.getString(Configg.TAG_flag));
Log.d("tag", setterGetterClass.getcountry().toString());
Log.d("show: ", setterGetterClass.toString());
}
Log.d("parseData: ", setterGetterClass.toString());
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
You for loop iterates & every time it initializes a new SuperHeroes instance to setterGetterClass. So after last iteration it will hold the last JsobObject data. Try
Create an ArrayList.
ArrayList<SuperHeroes> superHeroesList = new ArrayList<SuperHeroes>();
Inside your for loop
SuperHeroes superHeroes = new SuperHeroes(jsonOb.getInt(Configg.TAG_rank),jsonOb.getString(Configg.TAG_country),jsonOb.getString(Configg.TAG_population),jsonOb.getString(Configg.TAG_flag);
superHeroesList.add(superHeroes);
Now superHeroesList will have all your json objects.
You are setting the "setterGetterClass" variable in every iteration of your for loop. When your loop ends, your "setterGetterClass" will have the last value of your JSONArray. That is the reason you get every time the last value. If you want to fetch all values, then you should add them to a list or similar structure. For example:
JSONArray json = req.getJSONArray("worldpopulation");
List<SuperHeroe> superHeroeList = new ArrayList<>();
for (int i = 0; i < json.length(); i++) {
SuperHeroe superHeroe = new SuperHeroe(jsonOb.getInt(Configg.TAG_rank),jsonOb.getString(Configg.TAG_country),jsonOb.getString(Configg.TAG_population),jsonOb.getString(Configg.TAG_flag));
superHeroeList.add(superHeroe);
}
Then you'll have all the objects contained in the "superHeroeList" ArrayList.

how to set value in list object from json

I want to set array value in list , i am using volley library for it.Can you please help me how to set value in List dataset = new LinkedList<>(Arrays.asList(name)); like List dataset = new LinkedList<>(Arrays.asList("name1","name2"));.
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
List<String> dataset=new LinkedList<>();
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
String name = person.getString("Name");
dataset = (Arrays.asList(name));
}
btnService.attachDataSource(dataset);
}
above code only set last value in dataset.
ArrayList list = new ArrayList();
list.add(name);
and solved my self

Data not bind as it came from service in android

Data is not binding in array list as it came from the Service, as like first option which added in ArrayList is Lead, second is Qualified, and third is test. When i check list on first position it shows Qualified, Lead, test. But i want as i bind my list as it show in that sequence.
public static HashMap<String,ArrayList<LeadData>> LeadDataMap= new HashMap<String,ArrayList<LeadData>>();
public static ArrayList<String> aList = new ArrayList<String>();
for (int i = 0; i < LeadListsJSONArray.length(); i++) {
JSONObject Leadsobj = LeadListsJSONArray.getJSONObject(i);
//get stage name for hashmap key value..
String StageNameString = Leadsobj.getString("StageName");
String StageIdString = Leadsobj.getString("StageId");
System.out.println("stage id........................"+StageIdString);
//get new leads list..
JSONArray jaarr2 = new JSONArray(Leadsobj.getString("Leads"));
ArrayList<LeadData> leadDataList = new ArrayList<LeadData>();
for (int j = 0; j < jaarr2.length(); j++) {
LeadData ld = new LeadData();
JSONObject obj3 = jaarr2.getJSONObject(j);
ld.setLeadCompanyName(obj3.getString("LeadCompanyName"));
ld.setLeadId(obj3.getString("LeadId"));
ld.setTitle(obj3.getString("Title"));
leadDataList.add(ld);
}
//here we are puttin the leaddatalist inot map with respect to stage name...
LeadDataMap.put(StageNameString.trim().toString(), leadDataList);
//LeadDataMap.put(StageIdString, leadDataList);
}
In LeadDataMap data in not in that sequence in that i have put. This is the problem.
I get solution by using LinkedHashmap.

JSON data list dynamic

I am working in Android just for a heads up. I was able to retrieve JSON data but now I have come to the point where there are multiples. Example:
"workers":[
{
"id":3357,
"username":"Unreliable.worker",
"password":"x",
"monitor":0,
"count_all":41,
"count_all_archive":0,
"hashrate":477,
"difficulty":106.56
},
{
"id":4061,
"username":"Unreliable.worker2",
"password":"x",
"monitor":0,
"count_all":0,
"count_all_archive":null,
"hashrate":0,
"difficulty":0
}
would I have to do a for loop or is there another way to do it?
This is the code that I am using now to get them:
JSONObject workersJSONObject = personalJSONObject.getJSONObject("workers");
but I don't know how I would get for each and separate them. I'll get them by using:
id= workersJSONObject.getDouble("id");
[] in JSON refers to a JSON array. You could use something like:
JSONArray workersJSONArray = personalJSONObject.getJSONArray("workers");
Then loop through each item as (basically get each object inside array):
for (int i = 0; i < workersJSONArray.length(); i++) {
JSONObject workersJSONObject = workersJSONArray.getJSONObject(i);
// parse object just like before
}
On JSON, {} defines an object, [] defines an array instead. if you want details. see this and JSONArray
sample:
JSONArray workersJSOnArray = new JSONArray(stringData);
or
JSONArray workersJSOnArray = personalJSONObject.getJSONArray("workers");
/* THen you can loop from each data you want */
int len = workersJSOnArray.length();
for (int i = 0; i < len; i++)
{
JSONObject item = workersJSOnArray.optJSONObject(i);
int id = item.getInt ("id");
.....
}

Traverse array IN JSONArray

I'm making an app for android which makes a call to a webservice that returns a json with ARRAY as a parameter, I can go through all the settings and save them easily.
The problem is when I get the array that I returned within the JSON object.
Example of JSON:
[{"codigoArticulo":"0001","nombreArticulo":"CHULETAS DE CORDERO","factorVentasDefecto":"KG","precio":21.95,"factoresDeVenta":["KG","UN"]},{"codigoArticulo":"0007","nombreArticulo":"FALDETA DE CORDERO","factorVentasDefecto":"KG","precio":11.95,"factoresDeVenta":["KG","FL"]}]
I can save "codigoArticulo", "nombreArticulo", "factorVentasDefecto" and "precio easily, BUT i don't know how i can save "factoresDeVenta".
i have this code:
JSONArray resparray = new JSONArray(JSONdevuelto);
for (int i = 0; i < resparray.length(); i++) {
JSONObject respJSON = resparray.getJSONObject(i);
int IDArticulo = respJSON.getInt("codigoArticulo");
String NombreArticulo = respJSON.getString("nombreArticulo");
String FactordeVenta = respJSON.getString("factorVentasDefecto");
int PrecioArticulo = respJSON.getInt("precio");
}
How i can save in one array the variables on "factoresDeVenta"?
I try
String[] Factores = respJSON.getJSONArray("factoresDeVenta");
but no works because are incompatible types.
I need the array to make later a Spinner
Thank you.
factoresDeVenta is an JSONArray inside JSONObject so you will need to use getJSONArray or optJSONArray and use loop for getting values from JSONArray:
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
String str_value=jArray.optString(i); //<< jget value from jArray
}
JSONArray jArray = respJSON.getJSONArray("factoresDeVenta");
It would give you the array of data stored in factoresDeVenta, which you can again traverse to get the individual elements from that array.
You can store in ArrayList<String>
ArrayList<String> arrStr = new ArrayList<String>();
JSONArray jArray = respJSON.optJSONArray("factoresDeVenta");
for (int i = 0; i < jArray.length(); i++) {
arrStr.add(jArray.getString(i));
}
And then you can convert arraylist to string array
String[] Factores = arrStr.toArray(new String[arrStr.size()]);

Categories

Resources