I am calling this array and I am getting value but I am not able to assign on spinner.
This is my array data.
"data": {
"id": "105",
"prod_id": "FMSP-6815",
"prod_cat_id": "4",
"prod_name": "Aakash Fish",
"prod_price": "1",
"is_avlbl": "0",
"prod_weight": "500,1000",
"prod_quantity": "0",
"prod_desc": "Eat at your own risk",
"prod_image": "medicine.jpg",
"created_at": "2019-03-26 17:33:26",
"updated_at": "2019-03-26 17:33:26",
"bone_type": "0",
"skin_type": "0",
"cut_type": "0",
"cat_name": "ready_to_eat",
"prod_attributes": [
{
"id": "15",
"name": "Size",
"attribute_options": [
{
"id": "15",
"attribute_id": "15",
"attribute_name": "size",
"prod_id": "105",
"attr_option_id": "8",
"option_price": "10",
"option_name": "Large"
}
]
},
{
"id": "14",
"name": "Clean",
"attribute_options": [
{
"id": "16",
"attribute_id": "14",
"attribute_name": "clean",
"prod_id": "105",
"attr_option_id": "6",
"option_price": "5",
"option_name": "Skinny"
},
{
"id": "20",
"attribute_id": "14",
"attribute_name": "Clean",
"prod_id": "105",
"attr_option_id": "7",
"option_price": "30",
"option_name": "White Skin"
}
]
},
{
"id": "7",
"name": "Cut",
"attribute_options": [
{
"id": "18",
"attribute_id": "7",
"attribute_name": "Cut",
"prod_id": "105",
"attr_option_id": "4",
"option_price": "20",
"option_name": "Medium"
}
]
}
]
}
This is my array
and
this is how I am calling from API.
This is my Java code. I used for loop inside for loop.
if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {
attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
attribute_array_size = attributeData.size();
if (attributeData.size() == 0){
p_attribute.setVisibility(View.GONE);
} else {
p_attribute.setVisibility(View.VISIBLE);
// Loading attribute options
for (int j=0;j<attributeData.size();j++){
attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();
//String option = attributeOptions.get(j).getOption_name();
// Log.e("options",option);
dateList.clear();
dateList.add("Select");
for (int k=0;k<attributeOptions.size();k++){
String attribute_id = attributeOptions.get(k).getAttribute_id();
String option_name = attributeOptions.get(k).getOption_name();
String option_price = attributeOptions.get(k).getOption_price();
dateList.add(option_name);
//Log.e("option_id",attribute_id);
//Log.e("option_name",option_name);
// Log.e("option_price",option_price);
}
ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(),R.layout.date_dialog, R.id.textDate, dateList);
holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
}
}
}
From the question you asked I assume that your spinner shows last values. There are two problems with your code.
1- dateList.clear(); and dateList.add("Select"); should be outside for loop
2- Set spinner adapter outside for loop after all values are assigned to arrayList
Below you can check how it will go
if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {
attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
attribute_array_size = attributeData.size();
if (attributeData.size() == 0){
p_attribute.setVisibility(View.GONE);
} else {
p_attribute.setVisibility(View.VISIBLE);
// Loading attribute options
dateList.clear();
dateList.add("Select");
for (int j=0;j<attributeData.size();j++){
attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();
//String option = attributeOptions.get(j).getOption_name();
// Log.e("options",option);
for (int k=0;k<attributeOptions.size();k++){
String attribute_id = attributeOptions.get(k).getAttribute_id();
String option_name = attributeOptions.get(k).getOption_name();
String option_price = attributeOptions.get(k).getOption_price();
dateList.add(option_name);
//Log.e("option_id",attribute_id);
//Log.e("option_name",option_name);
// Log.e("option_price",option_price);
}
}
if (dateList.size() > 0) {
ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(), R.layout.date_dialog, R.id.textDate, dateList);
holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
}
}
}
Related
I'm new to android. I have a JSON file with multiple arrays and I'm able to fetch data from the first array and display it in listview (check the first for loop). But i tried to fetch the next array and i didn't get any results(blank screen). I need to display values from "open_days" array. Posted my JSON file and for loop portion.Please check and help me
MainActivity. java
try {
JSONObject jjsonObject =new JSONObject(result);
String getObject = jjsonObject.getString("message");
String getObject1 = jjsonObject.getString("status");
JSONArray getArray = jjsonObject.getJSONArray("favorite_facility_list");
Log.e("message",getObject);
Log.e("status",getObject1);
Log.e("favorite_facility_list",getArray.toString());
for(int i = 0; i < getArray.length(); i++)
{
JSONObject objects = getArray.getJSONObject(i);
String promotion_id = objects.getString("facility_id");
String promotion_image_name = objects.getString("facility_course_name");
String promotion_image_url = objects.getString("icon_image_name");
JSONArray array2=jjsonObject.getJSONArray("open_days");
for(int j=0;j<array2.length();j++)
{
JSONObject obj2=array2.getJSONObject(j);
String openid = obj2.getString("open_days_id");
String openname = obj2.getString("open_days_name");
//
promotionlists promotionlists = new promotionlists();
promotionlists.setFacility_id(promotion_id);
promotionlists.setFacility_course_name(promotion_image_name);
promotionlists.setIcon_image_name(promotion_image_url);
promotionlists.setOpen_days_id(openid);
promotionlists.setOpen_days_name(openname);
promotionArray.add(promotionlists);
adapter= new CustomAdapter(promotionArray,getApplicationContext());
lv.setAdapter(adapter);
}}
JSON file:
{
"status": "1",
"message": "Success",
"favorite_facility_list": [
{
"facility_id": "11",
"facility_course_name": "Facility 2",
"icon_image_name": "3.jpg",
"banner_image_name_list": [
"39.jpg"
],
"address": "test1",
"open_days": [
{
"open_days_id": "5",
"open_days_name": "Thursday"
},
{
"open_days_id": "6",
"open_days_name": "Friday"
},
{
"open_days_id": "8",
"open_days_name": "Sunday"
},
{
"open_days_id": "7",
"open_days_name": "Saturday"
}
],
"open_time_start": "6:8 pm",
"open_time_end": "12:8 pm",
"workouts": [
{
"workout_id": "1",
"workout_name": "Aerobic"
},
{
"workout_id": "2",
"workout_name": "Yoga"
},
{
"workout_id": "4",
"workout_name": "Gym"
},
{
"workout_id": "8",
"workout_name": "Pool"
}
],
"equipments": [
{
"equipment_id": "5",
"equipment_name": "Rowing"
},
{
"equipment_id": "6",
"equipment_name": "Stationary Bike"
},
{
"equipment_id": "7",
"equipment_name": "Bench Press"
},
{
"equipment_id": "8",
"equipment_name": "TRX"
}
],
"services": [
{
"service_id": "4",
"service_name": "Tranning"
},
{
"service_id": "1",
"service_name": "Personal Training"
}
],
"drop_in_rate": [
"10-test",
"20-test1",
"30-test3"
],
"parking_status": "1",
"parking_notes": "2 hrs available",
"facility_status": "1",
"facility_status_description": "Live",
"rating": "2.90",
"distance": "9412.2ml",
"already_like": 1,
"weburl": "",
"phone_no": "",
"facility_lattitude": "-3",
"facility_longitude": "151"
}
]
}
Your Logcat throws JSONException.
Don't
JSONArray array2=jjsonObject.getJSONArray("open_days");
Do
JSONArray array2=objects.getJSONArray("open_days");
You are using the wrong object to fetch open_days
Change
JSONArray array2 = jjsonObject.getJSONArray("open_days");
to
JSONArray array2 = objects.getJSONArray("open_days");
I need to get the images array data how to put into the adapter and not required adapter code? Only loop and how to add that's important for me
and then after second I can access option_value array.
First all images can be accessed then after a option_value array.
{
"success": true,
"data": {
"id": "50",
"seo_h1": "",
"name": "Shirt 10001",
"manufacturer": "",
"sku": "",
"model": "10001",
"image": "http://api.yellowskydemo.com/image/cache/data/shirts/7228-500x500.jpg",
"images": [
"http://api.yellowskydemo.com/image/cache/data/shirts/13-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/302-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/5-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/205-500x500.jpg"
],
"price": "$540.00",
"rating": 0,
"description": "<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n\r\n<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n",
"attribute_groups": [],
"special": "",
"discounts": [],
"options": [
{
"name": "Size",
"type": "select",
"option_value": [
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$50.00",
"price_prefix": "-",
"product_option_value_id": "17",
"option_value_id": "55",
"name": "L-40",
"quantity": "99"
},
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$40.00",
"price_prefix": "+",
"product_option_value_id": "18",
"option_value_id": "57",
"name": "XXL-44",
"quantity": "100"
}
],
"required": "1",
"product_option_id": "227",
"option_id": "14"
}
],
"minimum": "1",
"meta_description": "",
"meta_keyword": "",
"tag": "",
"upc": "",
"ean": "",
"jan": "",
"isbn": "",
"mpn": "",
"location": "",
"stock_status": "Out Of Stock",
"manufacturer_id": null,
"tax_class_id": "0",
"date_available": "2014-08-12",
"weight": "0.00000000",
"weight_class_id": "1",
"length": "0.00000000",
"width": "0.00000000",
"height": "0.00000000",
"length_class_id": "1",
"subtract": "0",
"sort_order": "1",
"status": "1",
"date_added": "2014-08-13 12:05:56",
"date_modified": "2015-06-30 11:19:39",
"viewed": "8",
"weight_class": "kg",
"length_class": "cm",
"reward": "0",
"points": "0",
"category": [
{
"name": "Shirts",
"id": "59"
},
{
"name": "Casual Shirts",
"id": "60"
}
],
"quantity": "99",
"reviews": {
"review_total": "0"
}
}
}
You should really look into How to parse JsonObject / JsonArray in android.
Please put your code of also what you have tried because people are here to help solve error/problem not to do coding.
Here is code from which you can Parse your json
JSONObject jsonObject = new JSONObject();
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONArray imagesArray = dataObject.getJSONArray("images");
ArrayList<String> listOfImagesUrl = new ArrayList<>();
for(int i = 0; i < imagesArray.length(); i++)
{
listOfImagesUrl.add(imagesArray.getString(i)); // listOfImages
}
// code for option value
JSONArray optionsArray = dataObject.getJSONArray("options");
for(int i = 0; i < optionsArray.length(); i++)
{
JSONObject option = optionsArray.getJSONObject(i);
JSONArray optionsValueArray = option.getJSONArray("option_value");
for(int j = 0 ; j < optionsValueArray.length(); j++)
{
JSONObject optionValueObject = optionsValueArray.getJSONObject(j);
String image = optionValueObject.getString("image");
String price = optionValueObject.getString("price");
String price_prefix = optionValueObject.getString("price_prefix");
//same like this
}
}
I am developing a quiz application and my questions are in a Json file
{
"name": "JHS ASANTE TWI",
"course_id": "73",
"courseID": "01JS88",
"package_code": "JHS",
"description": "",
"category": "",
"author": "content#exams.com",
"questions": {
"58242": {
"qid": "58242",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Kofi de sika no maa <span style=\"text-decoration: underline;\">aberante<span>ε</span></span> bi.</p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:29:33",
"rating": 0,
"answers": [
{
"id": "250310",
"text": "<p>ababaawa</p>",
"value": "1",
"solution": ""
},
{
"id": "250311",
"text": "<p>ab<span>ɔfra</span><span><br /></span></p>",
"value": "0",
"solution": ""
},
{
"id": "250312",
"text": "<p>aberewa</p>",
"value": "0",
"solution": ""
},
{
"id": "250313",
"text": "<p>abarimaa</p>",
"value": "0",
"solution": ""
}
]
},
"58245": {
"qid": "58245",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Mehunuu m'adamfo bi nnora <span style=\"text-decoration: underline;\">an</span><span style=\"text-decoration: underline;\">ɔpa</span>.</p>\n<p> </p>\n<p> </p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:43:29",
"rating": 0,
"answers": [
{
"id": "250329",
"text": "<p>awia</p>",
"value": "1",
"solution": ""
},
{
"id": "250328",
"text": "<p>anwummer<strong></strong>ε</p>",
"value": "0",
"solution": ""
},
{
"id": "250327",
"text": "<p>ahemadakye</p>",
"value": "0",
"solution": ""
},
{
"id": "250326",
"text": "<p>owigyinaeε</p>",
"value": "0",
"solution": ""
}
]
},
"58246": {
"qid": "58246",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Asomaning <span style=\"text-decoration: underline;\">kuro</span> no so.</p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:53:00",
"rating": 0,
"answers": [
{
"id": "250330",
"text": "<p><span>ɔmansini</span></p>",
"value": "0",
"solution": ""
},
{
"id": "250331",
"text": "<p><span>ɔman</span></p>",
"value": "0",
"solution": ""
},
{
"id": "250332",
"text": "<p>wiase</p>",
"value": "0",
"solution": ""
},
{
"id": "250333",
"text": "<p>akuraa</p>",
"value": "1",
"solution": ""
}
]
}
}
}
My question is: how do I access the next question of "questions" when i click next for the next question since "58242", "58245", etc are all dynamic values?
the answer on this post was very helpfull but now i want to now how loop through the questions How to parse a dynamic JSON key in a Nested JSON result
it helped me get the currentdynamic value but does not give me the next question when i click on next
private View.OnClickListener nextListener = new View.OnClickListener() {
public void onClick(View v) {
setAnswer();
try {
if (quesIndex != searchResult.getJSONObject("questions").length() - 1) {
quesIndex++;
progress.setProgress(0);
progress.setMax(100);
} else {
Intent myIntent = new Intent(TestActivity.this, Assesment.class);
myIntent.putExtra("intVariableName", score);
startActivity(myIntent);
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (quesIndex == searchResult.getJSONObject("questions").length() - 1) {
next.setText("Finish");
Intent myIntent = new Intent(TestActivity.this, Assesment.class);
myIntent.putExtra("intVariableName", score);
startActivity(myIntent);
// close this activity
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
showQuestion(quesIndex, review);
}
};
will appreciate the help thanks :)
Not sure what you problem is. Seems like the solution is in the link you gave above.
JSONObject questions = result.getJSONObject("questions");
Iterator keys = questions.keys();
//go through every question
while (keys.hasNext()) {
String currentDynamicKey = (String)keys.next();
JSONObject currentQuestion = questions.getJSONObject(currentDynamicKey);
}
I've json object like this:
JSon Object
{
"31": {
"basic": {
"node_id": "31",
"title": "test",
"alias": "test",
"description": "test",
"site_id": "151336557",
"node_type": "7",
"privacy": "7",
"deleted": "0",
"status": "1",
"created_date": "1379169518",
"updated_date": "1379169518",
"created_by": "140513626519686828",
"updated_by": null,
"readable_date": "14th Sep, 2013"
},
"meta": {
"forum_id": "61"
},
"comments": {
"count": 1
},
"likes": {
"count": 0
},
"tags": [],
"node_id": "31"
},
"32": {
"basic": {
"node_id": "32",
"title": "testing discussion",
"alias": "testing-discussion",
"description": "testing",
"site_id": "151336557",
"node_type": "7",
"privacy": "7",
"deleted": "0",
"status": "1",
"created_date": "1379493816",
"updated_date": "1379493816",
"created_by": "140513795022034166",
"updated_by": null,
"readable_date": "18th Sep, 2013"
},
"meta": {
"forum_id": "65"
},
"comments": {
"count": 1
},
"likes": {
"count": 0
},
"tags": [],
"node_id": "32"
}
}
and this is the code that I want to get certain object:
arrayTemplist = new ArrayList<FeedItem>();
for(int x=0;x<feedItems.size();x++)
{
int currentNodeType =feedItems.get(x).getNode_type();
if (currentNodeType!=0 && currentNodeType==nodeType)
{
arrayTemplist.add(feedItems.get(x));
Log.d("Cem", arrayTemplist.toString());
}
else{
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
listView.setEmptyView(findViewById(R.id.empty));
}
}
listAdapter = new FeedListAdapter(this, arrayTemplist);
listView.setAdapter(listAdapter);
listView.setEmptyView(findViewById(R.id.empty));
}
I am not able to display the view for the filtered data. I can log the data but I am not able to display it.
The whole code is here http://pastie.org/9662511
First, you should not set the adapter every iteration. Secondly, you should call notifyDataSetChanged method of the BaseAdapter to refresh the data displayed in the list view.
arrayTemplist = new ArrayList<FeedItem>();
for(int x=0;x<feedItems.size();x++)
{
int currentNodeType =feedItems.get(x).getNode_type();
if (currentNodeType!=0 && currentNodeType==nodeType)
{
arrayTemplist.add(feedItems.get(x));
Log.d("Cem", arrayTemplist.toString());
}
}
listAdapter = new FeedListAdapter(this, arrayTemplist);
listView.setAdapter(listAdapter);
listView.setEmptyView(findViewById(R.id.empty));
listAdapter.notifyDataSetChanged()
}
Here am Created the JSON Values in Static Way.
JSON Structure
{
"MatchType": {
"inningsperteam": "1",
"maxovers": "5",
"players": "11"
},
"Innings": [
{
"noballs": "0",
"BowlingDetail": [
{
"noballs": "0",
"balls": "0",
"maidens": "0",
"player": "Bowler 12",
"runs": "0",
"wickets": "0",
"bowlingposition": "1",
"wides": "0"
}
],
"byes": "0",
"battingteam": "Team 26",
"overs": "0",
"addpenaltyruns": "0",
"legbyes": "0",
"inplay": "true",
"balls": "0",
"InningsOvers": [],
"declared": "false",
"penaltyruns": "0",
"bowlingteam": "Team 46",
"runs": "0",
"wickets": "0",
"wides": "0",
"completed": "false"
}
],
"Umpire 1": "Umpire 12",
"Umpire 2": "Umpire 53",
"location": "England",
"Ground": "Oval",
"date": "20/6/2014",
"Club": [
{
"Team": {
"Player": [
{
"keeper": "false",
"captain": "false",
"firstname": "Batsman 1"
},
{
"keeper": "false",
"captain": "false",
"firstname": "Batsman 2"
}
],
"local": "1",
"name": "Team 26",
"toss": "false"
},
"local": "1",
"name": "Club1"
},
{
"Team": {
"Player": [
{
"keeper": "false",
"captain": "true",
"firstname": "Bowler 12"
}
],
"local": "2",
"name": "Team 46",
"toss": "true"
},
"local": "2",
"name": "Club2"
}
],
"CurrentPlayers": {
"bowler": "Bowler 12",
"playerA": "Batsman 1",
"playerB": "Batsman 2"
}
}
BattingDetails JSON
[
{
"BattingDetail": [
{
"balls": "0",
"sixes": "0",
"runs": "10",
"player": "Batsman 1",
"battingposition": "1",
"fours": "0"
},
{
"balls": "0",
"sixes": "0",
"runs": "0",
"player": "Batsman 2",
"battingposition": "1",
"fours": "0"
}
]
}
]
Coding
try {
json_scores = new JSONObject(test_str);
jsonArray = new JSONArray("["+test_str+"]");
json_scores = jsonArray.getJSONObject(0);
JSONArray nxt_test = new JSONArray(json_scores.getString("Innings"));
String str = new JSONArray(json_scores.getString("Innings")).getJSONObject(0).getString("BattingDetail");
JSONArray jArray = new JSONArray(str);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = new JSONObject();
json = jArray.getJSONObject(i);
if (json.getString("player").equalsIgnoreCase(player_name)) {
jArray.getJSONObject(i).put("runs", "10");
}
}
Log.v("Test Array", ""+nxt_test.put(0,new JSONObject().put("BattingDetail", jArray)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
I want to put the BattingDetail JSON array in Innings json array in JSON Structure.am not able solve this issue.can any one know help me to solve this issue.
Take your main json data into JSONObject.
JSONObject jsobj=new JSONObject(data);
JSONArray jsarBatting=new JSONArray(battingdetail);
JSONArray jsAr=jsobj.getJSONArray("innings");
JSONObject jsobj= jsAr.getJSONObject(0);
jsobj.put("BattingDetail",jsarBatting);