У меня есть данные JSON, поступающие корректно с моего сервера, я просто хочу поместить его в следующий массив, но я не уверен, что данные JSON правильно вставляются в ArrayList.Ввод данных JSON в список
Вот массив
private List<ShopInfo> createList(int size) {
List<ShopInfo> result = new ArrayList<ShopInfo>();
for (int i = 1; i <= size; i++) {
ShopInfo ci = new ShopInfo();
ci.name = TAG_NAME+i;
ci.address = TAG_ADDRESS+i;
result.add(ci);
}
return result;
}
Мой JSON
{"success":1,"shops":[{"name":"Test_Shop","address":"1 Big Road Dublin"}
Файл
public class OrderCoffee extends Activity {
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> shopList;
private static String url_all_products = "xxxxxxxxxx/ordercoffee.php";
// products JSONArray
JSONArray shops = null;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_SHOPS = "shops";
private static final String TAG_NAME = "name";
private static final String TAG_ADDRESS = "address";
//get a list of participating coffee shops in the locality that are using the app
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_coffee);
new LoadAllProducts().execute();
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
shopList = new ArrayList<HashMap<String, String>>();
ShopAdapter ca = new ShopAdapter(createList(3));
recList.setAdapter(ca);
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array
shops = json.getJSONArray(TAG_SHOPS);
// looping through All Products
for (int i = 0; i < shops.length(); i++) {
JSONObject c = shops.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ADDRESS);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ADDRESS, id);
map.put(TAG_NAME, name);
shopList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<ShopInfo> createList(int size) {
List<ShopInfo> result = new ArrayList<ShopInfo>();
for (int i = 1; i <= size; i++) {
ShopInfo ci = new ShopInfo();
ci.name = TAG_NAME+i;
ci.address = TAG_ADDRESS+i;
result.add(ci);
}
return result;
}
}
ShopAdapter
package com.example.strobe.coffeetime;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by root on 10/04/15.
*/
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopViewHolder> {
private List<ShopInfo> shopList;
public ShopAdapter(List<ShopInfo> shopList) {
this.shopList = shopList;
}
@Override
public int getItemCount() {
return shopList.size();
}
@Override
public void onBindViewHolder(ShopViewHolder shopViewHolder, int i) {
ShopInfo ci = shopList.get(i);
shopViewHolder.vName.setText(ci.name);
shopViewHolder.vAddress.setText(ci.address);
}
@Override
public ShopViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout, viewGroup, false);
return new ShopViewHolder(itemView);
}
public static class ShopViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vAddress;
public ShopViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.name);
vAddress = (TextView) v.findViewById(R.id.address);
}
}
}
Что вы имеете в виду * "это, кажется, не входите в массив правильно для меня »*? – Rami
Я не уверен, что способ ввода данных JSON в список result = new ArrayList (); правильно, я не был уверен, как сформулировать вопрос, перефразировать. –
Strobes
вы можете добавить этот метод к вашей асинхронной загрузке LoadAllProducts и напечатать свой массив: protected void onPostExecute (результат строки) {Log.d ("shopList", shopList.toString());} –