嘿伙计们,我正在尝试在我的应用程序中创建一个自定义ListView,它通过json请求加载数据fdrom mysql数据库。
android.R.id.simple_list_item_1
但是现在我已经创建了我自己的自定义xml文件,其中包含2个名称和价格的文本视图。 我的代码是
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;
}
我想在textview 1中显示股票的名称,在textview 2中显示价格 欢迎任何帮助。谢谢!
答案 0 :(得分:1)
你应该这样试试
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);
有关详细信息,请参阅this example