我正在成功解析JSON数据,我想根据类别项存储这些数据。根据类别,我想将数据传递到不同的点击监听器上的同一个活动中。
这是我的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String TAG = "dinesh";
ProgressDialog progressDialog;
ArrayList<GetdealData> getdealDataList = new ArrayList<>();
ArrayList<GetdealData> getdealDataListFood = new ArrayList<>();
ArrayList<GetdealData> getdealDataListFashion = new ArrayList<>();
ArrayList<GetdealData> getdealDataListEnt = new ArrayList<>();
RecyclerView recyclerView;
LinearLayout food,fashion,ent,misc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));*/
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
food=(LinearLayout) findViewById(R.id.food);
fashion=(LinearLayout)findViewById(R.id.fashion);
ent=(LinearLayout)findViewById(R.id.ent);
misc=(LinearLayout)findViewById(R.id.misc);
food.setOnClickListener(this);
fashion.setOnClickListener(this);
ent.setOnClickListener(this);
misc.setOnClickListener(this);
setSupportActionBar(toolbar);
new GetPost().execute();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.food:
Intent intent=new Intent(MainActivity.this,AllActivity.class);
intent.putExtra("mylist", getdealDataListFood);
startActivity(intent);
break;
case R.id.fashion:
Intent fashion=new Intent(MainActivity.this,AllActivity.class);
fashion.putExtra("mylist", getdealDataListFashion);
startActivity(fashion);
break;
case R.id.ent:
Intent ent=new Intent(MainActivity.this,AllActivity.class);
ent.putExtra("mylist", getdealDataListEnt);
startActivity(ent);
break;
case R.id.misc:
Intent misc=new Intent(MainActivity.this,AllActivity.class);
misc.putExtra("mylist", getdealDataList);
startActivity(misc);
break;
}
}
public class GetPost extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpUrlConnectionJson httpUrlConnectionJson = new HttpUrlConnectionJson();
JSONObject jsonObject = new JSONObject();
GetdealData getData = new GetdealData();
try {
jsonObject.put("cust_id","20234702");
jsonObject.put("lng","85.332416");
jsonObject.put("lat","27.700000");
jsonObject.put("rad","10");
Log.d(TAG, "get near by deal : " + jsonObject.toString());
String result = httpUrlConnectionJson.sendHTTPData("https://node.qpaysolutions.net/QPay.svc/getnearbydeals", jsonObject);
JSONObject jsonObject1 = new JSONObject(result);
JSONObject jsonObject2 = jsonObject1.getJSONObject("GetNearByDealsResult");
JSONArray jsonArray = jsonObject2.getJSONArray("nearByDeals");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject3 = jsonArray.getJSONObject(i);
String category = jsonObject3.getString("category");
if (category.endsWith("Food")){
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
String locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
getdealDataListFood.add(getData);
}
else if (category.equalsIgnoreCase("Fashion")){
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
String locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
getdealDataListFashion.add(getData);
}else if (category.equalsIgnoreCase("Entertainment")){
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
String locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
getdealDataListEnt.add(getData);
}else if (category.equals("misc")){
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
String locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
getdealDataList.add(getData);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
}
}
}
我在Allactivity.class
中获得了相同的数据,但是当我只添加一个这样的类别数据时:
if (category.endsWith("Food")){
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
getdealDataListFood.add(getData);
}
在这种情况下,我得到了理想的结果。
答案 0 :(得分:1)
您的代码太长,只需创建字符串,然后检查它的类别
String dealDesc = jsonObject3.getString("dealDesc");
String dealId = jsonObject3.getString("dealId");
String distance = jsonObject3.getString("distance");
String estbName = jsonObject3.getString("estbName");
String featureFlag = jsonObject3.getString("featureFlag");
String imgUrl1 = jsonObject3.getString("imgUrl1");
String imgUrl2 = jsonObject3.getString("imgUrl2");
String imgUrl3 = jsonObject3.getString("imgUrl3");
String imgUrl4 = jsonObject3.getString("imgurl4");
String imgUrl5 = jsonObject3.getString("imgurl5");
String imgUrl6 = jsonObject3.getString("imgurl6");
String imgUrl7 = jsonObject3.getString("imgurl7");
String imgUrl8 = jsonObject3.getString("imgurl8");
String locLat = jsonObject3.getString("locLat");
String locLng = jsonObject3.getString("locLng");
String locPhone = jsonObject3.getString("locPhone");
String website = jsonObject3.getString("website");
getData.setDealDesc(dealDesc);
getData.setDealId(dealId);
getData.setDistance(distance);
getData.setEstbName(estbName);
getData.setImgUrl1(imgUrl1);
getData.setImgUrl2(imgUrl2);
getData.setImgUrl3(imgUrl3);
getData.setLocLat(locLat);
getData.setLocLng(locLng);
getData.setLocPhone(locPhone);
getData.setWebsite(website);
getData.setFeatureFlag(featureFlag);
getData.setImgUrl4(imgUrl4);
getData.setImgUrl5(imgUrl5);
getData.setImgUrl6(imgUrl6);
getData.setImgUrl7(imgUrl7);
getData.setImgUrl8(imgUrl8);
if (category.endsWith("Food"))
getdealDataListFood.add(getData);
else if(category.endsWith("Fashion"))
getdealDataListFashion.add(getData);
else if(category.endsWith("Entertainment"))
getdealDataListEnt.add(getData);
else if(category.endsWith("misc"))
getdealDataList.add(getData);
答案 1 :(得分:0)
您是否尝试过使用GSON反序列化json。 这样,您无需手动执行任何getter / setter方法,它将自动进行转换。然后,您可以遍历转换后的列表以检查类别并添加到相关列表中。
示例:
private List<SampleModel> createSampleModelClientFromJson(String jsonString) {
Gson gson = new Gson();
Type collectionType = new TypeToken<List<SampleModel>>() {}.getType();
return gson.fromJson(jsonString, collectionType);
}
只需确保您的模型使用了正确的@Expose或@SerializedName(“ ids”)键