0
嘿傢伙我試圖在我的應用程序中創建一個自定義ListView,通過json請求加載數據fdrom mysql數據庫。我使用的從mysql中的json請求創建自定義列表視圖
android.R.id.simple_list_item_1
,但現在我已經創建了具有2個textviews的名稱和價格我自己的自定義XML文件。 我的代碼是
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 提前顯示股票的名稱的任何幫助將welcomed.Thanks!
但你提出的android佈局simple_list_item_2,我想用我自己是list_tem。我如何設置textview1的名稱和textview 2的編號? –
@KostasMatrix,請參閱更新的答案。來自我的String []中的 – Naveen
必須具有「metoxes」,因爲這是來自數據庫的json數組。否則我會得到一個空的列表視圖。 –