1、WebServices的返回结果
2、ListView内容布局代码
<?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="wrap_content" android:background="@color/colorWhite" > <ImageView android:id="@+id/app_icon" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:src="@mipmap/ic_launcher_round1" /> <TextView android:id="@+id/customitem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/app_icon" android:gravity="center" android:paddingLeft="20dp" android:text="app name" android:textSize="20sp" android:textColor="@color/colorPrimaryDark" android:layout_marginTop="5dp"/> <TextView android:id="@+id/customdesc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/customitem" android:layout_toRightOf="@id/app_icon" android:gravity="center" android:paddingLeft="20dp" android:singleLine="true" android:text="app describe" android:textSize="14sp" android:textColor="@color/colorPrimaryDark" android:layout_marginBottom="5dp"/> </RelativeLayout>
3、Adapter的代码
1 import android.content.Context; 2 import android.view.LayoutInflater; 3 import android.view.View; 4 import android.view.ViewGroup; 5 import android.widget.BaseAdapter; 6 import android.widget.ImageView; 7 import android.widget.TextView; 8 9 import java.util.List; 10 import java.util.Map; 11 12 public class MyCustomAdapter extends BaseAdapter { 13 private Context context; 14 private List<Map<String,Object>> list; 15 private LayoutInflater inflater; 16 public MyCustomAdapter(List<Map<String,Object>> list, Context context) 17 { 18 this.context=context; 19 this.list=list; 20 inflater=LayoutInflater.from(context); 21 } 22 @Override 23 public int getCount() { 24 return list==null?0:list.size(); 25 } 26 27 @Override 28 public Object getItem(int position) { 29 return position; 30 } 31 32 @Override 33 public long getItemId(int position) { 34 return position; 35 } 36 class ViewHolder{ 37 public ImageView imageView; 38 public TextView appText; 39 public TextView descText; 40 } 41 42 @Override 43 public View getView(int position, View convertView, ViewGroup parent) { 44 ViewHolder holder =null; 45 if (holder == null){ 46 holder=new ViewHolder(); 47 if(convertView ==null){ 48 convertView=inflater.inflate(R.layout.drawelist,null); 49 holder.imageView = (ImageView) convertView.findViewById(R.id.app_icon); 50 holder.appText = (TextView) convertView.findViewById(R.id.customitem); 51 holder.descText = (TextView) convertView.findViewById(R.id.customdesc); 52 convertView.setTag(holder); 53 }else { 54 //根据Tag获取已经存在的ViewHolder 55 holder = (ViewHolder) convertView.getTag(); 56 } 57 //设置显示内容 58 holder.appText.setText(list.get(position).get("name").toString()); 59 holder.descText.setText(list.get(position).get("desc").toString()); 60 holder.imageView.setBackgroundResource((Integer)list.get(position).get("image")); 61 } 62 63 return convertView; 64 } 65 66 67 }
4、访问WEB代码
1 public static List<EmpInfo> GetEmpInfo(String PageNum, String PageSize, String strwhere) { 2 3 4 List<EmpInfo> listEmpinfo = new ArrayList<>(); 5 InputStreamReader in = null; 6 try { 7 final String SERVER_URL = GlobalAppliaction.WebUrl + "GetEmpList"; 8 HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求 9 List params = new ArrayList(); 10 params.add(new BasicNameValuePair("PageNum", PageNum)); // 添加必须的参数 11 params.add(new BasicNameValuePair("PageSize", PageSize)); // 添加必须的参数 12 params.add(new BasicNameValuePair("strwhere", strwhere)); // 添加必须的参数 13 request.setEntity(new UrlEncodedFormEntity(params, org.apache.http.protocol.HTTP.UTF_8)); // 设置参数的编码 14 HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈 15 16 // 解析返回的内容 17 if (httpResponse.getStatusLine().getStatusCode() != 404) { 18 InputStream inputStream = httpResponse.getEntity().getContent(); 19 in = new InputStreamReader(inputStream); 20 BufferedReader bufferedReader = new BufferedReader(in); 21 22 StringBuffer strBuffer = new StringBuffer(); 23 String line = null; 24 while ((line = bufferedReader.readLine()) != null) { 25 strBuffer.append(line); 26 } 27 inputStream.close(); 28 bufferedReader.close(); 29 String result = strBuffer.toString(); 30 31 32 String errCode = ""; 33 34 String strresult = ""; 35 JSONObject root = new JSONObject(result); 36 37 errCode = root.getString("Code"); 38 if (errCode.equals("1")) { 39 strresult = root.getString("Result"); 40 41 42 JSONArray array = root.getJSONArray("Result");//解析result 43 Gson gson = new Gson(); 44 listEmpinfo = gson.fromJson(strresult, new TypeToken<List<EmpInfo>>() { 45 }.getType()); 46 47 48 49 } 50 51 return listEmpinfo; 52 53 54 } 55 56 57 } catch (IOException e) { 58 e.printStackTrace(); 59 Log.d("debug", e.toString()); 60 return null; 61 } catch (JSONException e) { 62 e.printStackTrace(); 63 return null; 64 } finally { 65 66 if (in != null) { 67 try { 68 in.close(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 return listEmpinfo; 75 }
5、UI展示获取结果
//启动后台任务 GetWebResultTask queryAddressTask = new GetWebResultTask(); //分页显示 queryAddressTask.execute("1", "10", "");
ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; public class GetWebResultTask extends AsyncTask<String, Integer, List<EmpInfo>> { @Override protected List<EmpInfo> doInBackground(String... params) { List<EmpInfo> GetResult = null; try { GetResult = WebByHttp.GetEmpInfo(params[0], params[1], params[2]); } catch (Exception e) { e.printStackTrace(); } //将结果返回给onPostExecute方法 return GetResult; } //此方法可以在主线程改变UI protected void onPostExecute(List<EmpInfo> result) { try { for (int ii = 0; ii < result.size(); ii++) { map = new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher_background); map.put("name", result.get(ii).EmpName); map.put("desc", result.get(ii).CreateTime); list.add(map); } myCustomAdapter = new MyCustomAdapter(list, getActivity()); listviewemp.setAdapter(myCustomAdapter); listviewemp.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getActivity(), list.get(position).get("name").toString(), Toast.LENGTH_LONG).show(); } }); } catch (Exception e) { e.printStackTrace(); } } }
6、界面效果
原文地址:https://www.cnblogs.com/lzsin/p/10632994.html
时间: 2024-10-07 23:06:21