0
我一直堅持一點,我有一個簡單的mysql數據庫有名稱和地址表, 我能夠得到相同的json數組,但m無法填充listview。請幫我android mysql listview
我一直堅持一點,我有一個簡單的mysql數據庫有名稱和地址表, 我能夠得到相同的json數組,但m無法填充listview。請幫我android mysql listview
假設你已經知道如何使用ListView
控制和使用適配器可那麼你需要創建一個自定義接口,通過擴展BaseAdapter
,然後重寫getCount()
,getItem()
,getItemId()
和getView()
方法。
例如您的BaseAdapter實現可能需要JSONArray
,可能是這樣的......
public class JSONAdapter extends BaseAdapter {
private JSONArray array = null;
public JSONAdapter(JSONArray data) {
this.array=data;
}
public int getCount() {
return array.length();
}
public Object getItem(int position) {
return array.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View currentView, ViewGroup parent) {
// get the current item from the json array
JSONObject o = getItem(position);
// create a new view
View v;
if (convertView == null || newItemView) {
v = inflater.inflate(YOUR_ITEM_LAYOUT_VIEW_ID, parent, false);
} else {
v = convertView;
}
// populate the view
// use v.findViewById() to locate your layout items and then
// set a value from the JSONObject that you obtained above
return view;
}
}
上面的代碼可能無法直接編譯(並且不包含任何錯誤檢查),因爲我只是在這裏鍵入內聯......但希望它能讓你瞭解如何創建一個可以接受JSONArray
的ListAdapter
。
你是什麼意思「無法填充列表視圖」?你做了什麼? – Josnidhin
我能夠獲得靜態內容的列表視圖,如http://developer.android.com/resources/tutorials/views/hello-listview.html。但我想要一個動態的內容,在挫折中,我刪除了你當前的文件..可以ü指導PL,包括佈局文件 –
看到這個例子http://android-example-code.blogspot.in/p/dynamic-custoized- list-view-in-android.html – MAC