0
我想解析json數據並在我的自定義列表視圖中顯示。我的列表視圖沒有被填充
public class MainActivity extends Activity {
\t ListView list;
\t ArrayList<listview> array_list = new ArrayList<listview>();
\t class listview {
\t \t String description;
\t \t public listview(String description) {
\t \t \t this.description = description;
\t \t }
\t }
\t
\t class MyAdapter extends BaseAdapter { \t \t
\t \t
\t \t
\t \t public MyAdapter(Context context) { \t \t \t
\t \t }
\t \t @Override
\t \t public int getCount() {
\t \t \t return array_list.size();
\t \t }
\t \t @Override
\t \t public Object getItem(int position) {
\t \t \t return array_list.get(position);
\t \t }
\t \t @Override
\t \t public long getItemId(int position) {
\t \t \t return position;
\t \t }
\t \t @SuppressLint("ViewHolder")
\t \t @Override
\t \t public View getView(int position, View convertView, ViewGroup parent) {
\t \t \t LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
\t \t \t View view = inflater.inflate(R.layout.listview, parent, false);
\t \t \t TextView description = (TextView) view
\t \t \t \t \t .findViewById(R.id.description);
\t \t \t listview list = array_list.get(position);
\t \t \t description.setText(list.description);
\t \t \t return view;
\t \t }
\t }
\t class getJson_data extends AsyncTask<Void, Void, Void> {
\t \t @Override
\t \t protected Void doInBackground(Void... params) {
\t \t \t Get_data data = new Get_data();
\t \t \t String output = "";
\t \t \t try {
\t \t \t \t output = data.get_info();
\t \t \t \t JSONArray jarray = new JSONArray(output);
\t \t \t \t for (int i = 0; i < jarray.length(); i++) {
\t \t \t \t \t JSONObject jobject = jarray.getJSONObject(i);
\t \t \t \t \t JSONObject product = jobject.getJSONObject("product");
\t \t \t \t \t String description = product.getString("desc");
\t \t \t \t \t array_list.add(new listview(description));
\t \t \t \t }
\t \t \t } catch (ClientProtocolException e) {
\t \t \t \t e.printStackTrace();
\t \t \t } catch (IOException e) {
\t \t \t \t e.printStackTrace();
\t \t \t } catch (JSONException e) {
\t \t \t \t e.printStackTrace();
\t \t \t }
\t \t \t return null;
\t \t }
\t }
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.activity_main);
\t \t if (android.os.Build.VERSION.SDK_INT > 9) {
\t \t \t StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
\t \t \t \t \t .permitAll().build();
\t \t \t StrictMode.setThreadPolicy(policy);
\t \t }
\t \t new getJson_data().execute();
\t \t list = (ListView) findViewById(R.id.listview1);
\t \t list.setAdapter(new MyAdapter(this));
\t }
\t
}
下面u能找到我的自定義佈局listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:text="@string/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignBottom="@+id/description"
android:layout_alignTop="@+id/imageView3"
android:layout_marginRight="21dp"
android:layout_toLeftOf="@+id/imageView3"
android:contentDescription="@string/image_info"
android:src="@drawable/arrow_icon_circle" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="@+id/description"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:contentDescription="@string/image_info"
android:src="@drawable/delete" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/title"
android:contentDescription="@string/image_info"
android:src="@drawable/verified_circle_check_icon" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView4"
android:layout_toRightOf="@+id/title"
android:text="@string/text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="19dp"
android:contentDescription="@string/image_info"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
下面是一個輔助類(它的正常工作)提取JSON數據,轉換它轉換爲字符串並將其返回給doInBackground方法。
Get_data data = new Get_data();
String output = "";
try {
\t output = data.get_info();
這裏是我從助手類方法
[
{
"@type": "item",
"ID": 1036,
"product": {
"active": true,
"categories": [],
"desc": "thubnil image icon testing",
"gtins": [],
"id": 443,
"image": "",
"modelNumber": "thubnil im",
"name": "test image"
}
},
{
"@type": "item",
"ID": 781,
"product": {
"active": true,
"categories": [],
"desc": "cars",
"gtins": [],
"id": 46,
"image": "",
"modelNumber": "car1234",
"name": "Cars"
}
},
{
"@type": "item",
"ID": 783,
"product": {
"active": true,
"categories": [],
"desc": "cars",
"gtins": [],
"id": 46,
"image": "",
"modelNumber": "car1234",
"name": "Cars"
}
}
]
但沒有出現在我的列表視圖...哪兒我去錯了JSON數據?請有關如何這方面的任何建議會幫助我很多。在此先感謝
獲取MyAdapter的對象並調用異步任務的onPostExecute()中的notifyDataSetChanged(),或者您可以在onPostExecute方法中設置適配器 – 2014-09-26 07:40:47
這是我的代碼是否正確。 JSONArray jarray = new JSONArray(output); \t \t \t \t對(INT I = 0; I
2014-09-26 07:42:42
你是不是在'onPostExecute'請諮詢本[鏈接]更新您的視圖(http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) – 2014-09-26 07:42:50