我的列表视图未填充

时间:2014-09-26 07:35:05

标签: android

我正在尝试解析json数据并在我的自定义列表视图中显示它。

public class MainActivity extends Activity {

	ListView list;
	ArrayList<listview> array_list = new ArrayList<listview>();

	class listview {
		String description;

		public listview(String description) {
			this.description = description;
		}
	}
	
	class MyAdapter extends BaseAdapter {		
		
		

		public MyAdapter(Context context) {			
		}

		@Override
		public int getCount() {
			return array_list.size();
		}

		@Override
		public Object getItem(int position) {
			return array_list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("ViewHolder")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(R.layout.listview, parent, false);
			TextView description = (TextView) view
					.findViewById(R.id.description);
			listview list = array_list.get(position);
			description.setText(list.description);
			return view;
		}
	}

	class getJson_data extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			Get_data data = new Get_data();
			String output = "";
			try {
				output = data.get_info();

				JSONArray jarray = new JSONArray(output);
				for (int i = 0; i < jarray.length(); i++) {
					JSONObject jobject = jarray.getJSONObject(i);
					JSONObject product = jobject.getJSONObject("product");
					String description = product.getString("desc");
					array_list.add(new listview(description));
				}

			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		new getJson_data().execute();
		list = (ListView) findViewById(R.id.listview1);
		list.setAdapter(new MyAdapter(this));
	}

	
}

下面你可以找到我的自定义布局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 {
	output = data.get_info();

以下是我从辅助类方法获得的json数据

[
   {
       "@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"
       }
   }
]

但我的列表视图中没有出现任何内容......我哪里出错了?关于如何做到这一点的任何建议都会对我有所帮助。提前致谢

1 个答案:

答案 0 :(得分:0)

要更新列表视图,您需要编写代码以更新onPostExecute()中的列表视图。请参阅此link