获取网络数据中的数组显示成ListView的简单流程
首先说一下 这是我自己的个人笔记,如果想看看,不用看细节,可以看流程。
定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15);
运用线程获取网络数据 即编辑相关的访问方法以及参数
public static String sendDataByHttpClientPost(String url,List<NameValuePair> parameters) throws Exception {HttpClient client = new DefaultHttpClient();client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT);client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIME_OUT);HttpPost httppost = new HttpPost(url);UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");httppost.setEntity(entity);HttpResponse ressponse = client.execute(httppost);int code = ressponse.getStatusLine().getStatusCode();if (code == Status.SUCCESS) {InputStream is = ressponse.getEntity().getContent();byte[] result = getBytes(is);return new String(result);} else {throw new IllegalStateException("服务器状态异常");}}
获得相关JSON数据进行解析,当然首先得创建一个实体类 即下面方法的参数 clazz,参数jsonStr就是获得的网络JSON数据,clazz这个就是一个数组对象了,所包含的字段,可以在实体类中定义相关的变量。
STATUS STATUS_SUCCESS INFO 这些是我自己写的相关常量
public static <T> List<T> parseList(String jsonStr, Class<T> clazz)throws JSONException {JSONObject json = new JSONObject(jsonStr);String response = json.getString(STATUS);List<T> list = new ArrayList<T>();if (response != null && response.equals(STATUS_SUCCESS)) {String info = json.getString(INFO);if (!TextUtils.isEmpty(info) && !info.equals("null")) {list = (List<T>) JSON.parseArray(info, clazz);return list;} else {return list;}} else {throw new IllegalStateException();}}
上面方法得到的就是一个我们要在ListView当中显示的数组数据。然后将它通过消息Message发送到主线程的Handler中进行处理 即
message = handler.obtainMessage(Status.SUCCESS, list);message.sendToTarget();
SUCCESS 为自己写的常量
这样就拿到了数组了,现在的问题是,怎样实时显示到ListView当中, 步骤可以这样:
一 先定义一个类 它进行了适配器当中数据的更改、刷新 即
package com.sinosoft.foodsafemanagerv2.function.task.unionmeeting;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.content.Context;import android.widget.ListView;import com.sinosoft.adpter.MySimpleAdpter;import com.sinosoft.foodsafemanagerv2.R;import com.sinosoft.foodsafemanagerv2.entity.Case;public class UnionRecordListView {private ListView listView;private MySimpleAdpter myAdapter;private ArrayList<HashMap<String, Object>> datas;private Context context;private String[] from;public UnionRecordListView() {super();}public UnionRecordListView(final Context context, ListView listView) {this.context = context;this.listView = listView;from = new String[] { "img", "type_name", "time_type" };datas = new ArrayList<HashMap<String, Object>>();myAdapter = new MySimpleAdpter(context, datas,R.layout.layout_recordlistview_item, from, new int[] {R.id.imageview, R.id.name_or_type_text,R.id.type_or_time_text });listView.setAdapter(myAdapter);}/** * 设置数据 * * @param list */public void setDatas(List<Case> list) {ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();for (Case c : list) {HashMap<String, Object> map = new HashMap<String, Object>();String imgs = c.getImgIds();map.put(from[0], c.getCaseId());map.put(from[1], c.getUnitName());map.put(from[2], c.getTypeName());data.add(map);}this.datas = data;myAdapter.setListData(data);myAdapter.notifyDataSetChanged();}}