Reputation: 3260
Hey guys i am trying to create a custom ListView in my application that loads data fdrom mysql database by json request. i used the
android.R.id.simple_list_item_1
but now i have created my own custom xml file which has 2 textviews for the name and the price. My code is
public void ListDrwaer() {
List<Map<String, String>> stocksList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
TextView tv1 = (TextView)findViewById(R.id.textView1);
TextView tv2 = (TextView)findViewById(R.id.textView2);
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
String price = jsonChildNode.optString("price");
stocksList.add(createStockList(name, price));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
String[] from = { "name", "price" };
int[] to = { R.id.textView1, R.id.textView2 };
SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
R.layout.list_item,
from, to);
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createStockList(String name, String price) {
HashMap<String, String> stockNameNo = new HashMap<String, String>();
stockNameNo.put("name", name);
stockNameNo.put("price", price);
return stockNameNo;
}
i want to display the name of the stock in textview 1 and the price in textview 2 any help will be welcomed.Thanks in advance!
Upvotes: 0
Views: 576
Reputation: 1958
you should try like this
String[] from = { "name", "price" };
int[] to = { R.id.textView1, R.id.textView2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.list_item, from, to);
setListAdapter(adapter);
see this example for more information
Upvotes: 1