HashMap
とListview
という2つの列データを表示しようとすると、正常に動作します。 2つ以上の列を表示しようとするとエラーが発生します。JSONObjectからListViewに間違った形式のAndroidデータが表示される
2_columns.java
public class Main4Activity extends AppCompatActivity {
String url = "http://192.168.1.103/web_service/omg.php/";
private static final String COLUMN_ID = "ID";
private static final String COLUMN_NAME = "Name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value = response.getString(key);
HashMap<String, String> map = new HashMap<>();
map.put(COLUMN_ID, key);
map.put(COLUMN_NAME, value);
aList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
if(aList.size() > 0) {
String[] from = {COLUMN_ID, COLUMN_NAME};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(Main4Activity.this, aList, R.layout.list_item, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "error ");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
}
}
ローカルホスト/ web_service/omg.php
{
"32":"Western Food",
"33":"Chinese Food",
"34":"Mix Food",
"35":"Japanese Food",
"36":"Korean Food",
"37":"Italian Food",
"38":"German Food"
}
上記符号化が正常に動作しています。私は複数の列にデータを表示しようとすると、しかし、エラーが
multiple_columns.java、来る
String url = "http://192.168.1.103/web_service/ohno.php/";
private static final String product_sku = "SKU";
private static final String product_name = "Name";
private static final String product_price = "Price";
private static final String product_quantity = "Quantity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value_sku = response.getString(key);
String value_name= response.getString(key);
String value_price= response.getString(key);
String value_quantity= response.getString(key);
HashMap<String, String> map = new HashMap<>();
map.put(product_sku, value_sku);
map.put(product_name, value_name);
map.put(product_price, value_price);
map.put(product_quantity, value_quantity);
aList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
if(aList.size() > 0) {
String[] from = {product_sku,product_name, product_price,product_quantity};
int[] to = {R.id.sku,R.id.name, R.id.price,R.id.quantity};
SimpleAdapter adapter = new SimpleAdapter(Main5Activity.this, aList, R.layout.list_item2, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "error ");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
}
はlocalhost/web_service/ohno.php
{
"482":["1","Chicken Rice","1","1"],
"483":["1","French Fries","1","1"],
"484":["1","apple","1","1"],
"492":["1","western+italian","1","1"],
"493":["1","no_cat","1","1"]
}
上記のスクリーンショットからわかるように、 yは482,483,484 ...となり、右側の値はyとなります。コーディングに何が問題なのですか?
thxすぐにあなたの返信から見たいと思っています:) – gosulove
btw、あなたのコーディングのために、onclickリスナーに拡張することは可能ですか? – gosulove