Data not bind as it came from service in android - 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.

Related

ArrayList of objects are not setting in new ArrayList object

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();

Convert string to an object

I have 3 string list and I want to add all values to menus but it gives error which is "Invalid index 0, size is 0". Briefly, menus is null and how can I add them?
private List<List<Restaurant.Menu>> menus = new ArrayList<>();
ArrayList<String> MenuName = new ArrayList<>();
ArrayList<String> FoodName = new ArrayList<>();
ArrayList<String> FoodPrice = new ArrayList<>();
//I get values in DB. DB is full.
MenusName = tinydb.getListString("MenuName");
FoodName = tinydb.getListString("FoodName");
FoodPrice = tinydb.getListString("FoodPrice");
int restaurantCounter = 0;
int menuCounter = 0;
for (int j = 0; j < MenusName.size(); j++)
{
menus.get(restaurantCounter).get(j).name = MenusName.get(j))
}
Example, I created them for each value, it works but if string is long, it enforces app and I wait 10 sec. for this process. I need efficient way. Thanks in advance.
menus.get(resCounter).add(new Restaurant.Menu());
menus.get(resCounter).get(menuCounter).foods.add(new Restaurant.Food());
.
.
menus.get(resCounter).get(menuCounter).name = MenuName.get(i));
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).name = FoodName.get(i);
menus.get(resCounter).get(menuCounter).foods.get(foodCounter).price = FoodPrice.get(i);
You seem to be confusing arrays with lists here. Arrays have a fixed size once initialised, Lists don't. You need to add something to your menus list using menus.add(/*Add Menu Object here*/);
You haven't add any values to menus.so the object doesn't contain value at the index 0. This might be a reason.
I couldn't find a solution so I changed structure. I get all strings in restaurants and I used gson to convert them before store them. Also you can use gson for each string. It it same logic.
Gson gson = new Gson();
ArrayList<String> gsonString = new ArrayList<>();
for(int i=0; i<restaurants.size(); i++)
gsonString.add(gson.toJson(restaurants.get(i)));
tinydb.putListString("tinyRestaurant",gsonString);
Convert again...
Gson gson = new Gson();
for(int i=0; i<tinydb.getListString("tinyRestaurant").size(); i++)
restaurants.add(gson.fromJson(tinydb.getListString("tinyRestaurant").get(i), Restaurant.class));

Get jsonArray objects without value name and pass it to list

I am trying to read json array without object name, and pass it to a list.
My json looks like :
"facilites": [
"Pool",
" Air Conditioning",
" Pets Allowed",
" Fitness center",
" Kitchen",
" Internet",
" Sona"
]
I am trying to retrieve it using the following code -
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list = new ArrayList<String>();
list.add(facilities);
}
Inside the main loop I put to my pojo class chalets.setList(list);
The issue is in this line list.add(facilities); it only add the last element. After looping through all, list carry sona only.
Your list should be instantiated outside the loop.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
An improvement would be directly add string to list instead of capturing it into a string variable like list.add(chaletFacilities.getString(l))
Move initialization of your ArrayList outside of your loop.
Do like this
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
list.add(chaletFacilities.getString(l))
}
What you doing is initializing yourlist again and again and adding the element. So while last iteration the list is getting initialized again and only single element is being added to it.
ArrayList list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
You are creating the new list always. So your list size will be 1 with the last value in chaletFacilities array.
Solution: Keep your list initialization outside the for loop as below, and add all the values under the array into single list you created in the top.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++)
{
list.add(chaletFacilities.getString(l));
}

Two separate ASyncTasks wrongly combining data when processing JSON

I have a project with a TabLayout + ViewPager to scroll through two different fragments (one shows data happening currently and the other shows all data).
In the onCreateView's of both of these I call the same ASyncTask class with the only difference being the params changing.
FetchItems asyncTask = new FetchItems(getContext(), this);
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"someParams");
The JSON result is then processed here:
try {
LinkedList<LeagueItem> leagues = new LinkedList<>();
LeagueItem newLeague;
//For every league in the array
for (int i = 0; i < jsonBody.length(); i++) {
newLeague = new LeagueItem();
//Hold the league games
JSONObject jsonLeagueInfo = jsonBody.getJSONObject(i);
newLeague.setLeagueName(jsonLeagueInfo.getString("name"));
JSONArray leagueMatches = jsonLeagueInfo.getJSONArray("matches");
//For every match in that league
for(int j = 0;j<leagueMatches.length();j++){
JSONObject matchInformation = leagueMatches.getJSONObject(j);
JSONArray teamsArray = matchInformation.getJSONArray("teams");
MatchItem currentMatch = new MatchItem();
//For both teams in the match
for(int k=0;k<2;k++){
JSONObject teamInfo = teamsArray.getJSONObject(k);
String name = teamInfo.getString("name");
String logoUrl = teamInfo.getString("logo");
JSONObject scores = teamInfo.getJSONObject("results");
String runningScore = scores.getString("runningscore");
TeamItem currentTeam = new TeamItem(shortName, logoUrl, Integer.parseInt(runningScore), homeNum);
currentMatch.addTeam(currentTeam);
}
newLeague.addMatch(currentMatch);
}
leagues.add(newLeague);
}
return leagues;
}
I'm finding that both objects returned have crossover data which shouldn't be there. Both of the parent objects are correct in that they add the correct number of league items, however every league contains pretty much all the data that I'm iterating over. Am I missing something huge here? I thought that by calling executeOnExecuter I would be getting two completely separate threads with different objects.
Thanks.

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");
.....
}

Categories

Resources