在Android客户端进行与服务器端交互的时候需要将一组数据显示到手机上,ListView和Adapter是很常用的,顺便说一下simpleadapter,这个控件时间长了你就知道了,其实就是给初学者的一个试验品,有时候很鸡肋,也着急,所以一般都是自己定义Adapter,但是这样的话涉及的东西就多了,博主比较注意程序的耦合性,所以一般都是分开写bean,adapter,还有activity或者fragment,其实这个很好的体现了java的面向对象思想,将我的东西封装起来,好比从兜兜里拿东西,简单可以这么理解,好理论说了一大堆,上代码:
1.先来说最简单的bean,这个好比我们的兜兜里的东西,但是提供了get和set方法,可供主人进行拿或者放,还有在这着重说一下为什么imp一下,因为ListView基本一个的时候很少吧,但是如果进行多个的话,需要进行上个的引导来寻找下个,还有tostring(),这个大家可以选择性的重写
public class BuddhistServicesBean implements java.lang.Comparable{
private String BSTitle;
private String BSDetail;
private String BSFirst;
private String BSSecond;
private String BSThird;
public BuddhistServicesBean(String BSTitle, String BSDetail, String BSFirst, String BSSecond, String BSThird) {
this.BSTitle = BSTitle;
this.BSDetail = BSDetail;
this.BSFirst = BSFirst;
this.BSSecond = BSSecond;
this.BSThird = BSThird;
}
public String getBSTitle() {
return BSTitle;
}
public void setBSTitle(String BSTitle) {
this.BSTitle = BSTitle;
}
public String getBSDetail() {
return BSDetail;
}
public void setBSDetail(String BSDetail) {
this.BSDetail = BSDetail;
}
public String getBSFirst() {
return BSFirst;
}
public void setBSFirst(String BSFirst) {
this.BSFirst = BSFirst;
}
public String getBSSecond() {
return BSSecond;
}
public void setBSSecond(String BSSecond) {
this.BSSecond = BSSecond;
}
public String getBSThird() {
return BSThird;
}
public void setBSThird(String BSThird) {
this.BSThird = BSThird;
}
@Override
public String toString() {
return "BuddhistServicesBean{" +
"BSTitle='" + BSTitle + '\'' +
", BSDetail='" + BSDetail + '\'' +
", BSFirst='" + BSFirst + '\'' +
", BSSecond='" + BSSecond + '\'' +
", BSThird='" + BSThird + '\'' +
'}';
}
@Override
public int compareTo(Object another) {
return 0;
}
}
2.说一下Adapter,必须要继承base,然后进行相应的方法的重写,还有,如果图片需要缓存的话,一般都是把缓存解析图片的方法写在这,博主是这个习惯
public class BuddhistServicesAdapter extends BaseAdapter {
private Context context;
private List<BuddhistServicesBean> buddhistServicesBeans;
public BuddhistServicesAdapter(Context context, List<BuddhistServicesBean> buddhistServicesBeans) {
this.context = context;
this.buddhistServicesBeans = buddhistServicesBeans;
}
@Override
public int getCount() {
return buddhistServicesBeans.size();
}
@Override
public Object getItem(int position) {
return buddhistServicesBeans.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_listview_buddhistservices, null);
holder = new ViewHolder();
view.setTag(holder);
holder.bs_title = (TextView) view.findViewById(R.id.bs_title);
holder.bs_detial = (TextView) view.findViewById(R.id.bs_detail);
holder.bs_first = (TextView) view.findViewById(R.id.bs_first);
holder.bs_second = (TextView) view.findViewById(R.id.bs_second);
holder.bs_third = (TextView) view.findViewById(R.id.bs_third);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.bs_title.setText(buddhistServicesBeans.get(position).getBSTitle());
holder.bs_detial.setText(buddhistServicesBeans.get(position).getBSDetail());
holder.bs_first.setText(buddhistServicesBeans.get(position).getBSFirst());
holder.bs_second.setText(buddhistServicesBeans.get(position).getBSSecond());
holder.bs_third.setText(buddhistServicesBeans.get(position).getBSThird());
return view;
}
static class ViewHolder {
TextView bs_title;
TextView bs_detial;
TextView bs_first;
TextView bs_second;
TextView bs_third;
}
}
3.说一下最重要的activity或者fragment中的ListView的是实现
private void initData() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request;
try {
request = new HttpGet(new URI(FOSHIFUWU_API));
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity, "UTF-8");
try {
JSONObject jsonObject = new JSONObject(out);
JSONArray bsArray = jsonObject.getJSONArray("content");
buddhistServicesBeans = new ArrayList<BuddhistServicesBean>();
for (int i = 0; i < bsArray.length(); i++) {
JSONObject bsobj = bsArray.getJSONObject(i);
String title = bsobj.getString("title");
String des = bsobj.getString("des");
String money = bsobj.getString("money");
String unitId = bsobj.getString("unitId");
String enddate = bsobj.getString("endDate");
if (!enddate.equals("null")) {
endDate = getMilliToDate(enddate);
} else {
endDate = null;
}
buddhistServicesBeans.add(new BuddhistServicesBean(title, des, money, unitId, endDate));
}
isNet = true;
} catch (JSONException e) {
e.printStackTrace();
}
}
} else {
isNet = false;
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message msg = Message.obtain();
if (isNet) {
msg.what = 0;
handler.sendMessage(msg);
} else {
msg.what = 1;
handler.sendMessage(msg);
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
buddhistServicesAdapter = new BuddhistServicesAdapter(getActivity(), buddhistServicesBeans);
buddhistservices_listview.setAdapter(buddhistServicesAdapter);
mDialog.cancel();
buddhistservices_listview.setOnItemClickListener(new bsInfoOnItemClickListener());
break;
case 1:
break;
}
}
};
在这里大家需要将jsonarray解析,楼主为了更好的进行精细的控制,选择了json的自带的解析,但是gson封装了更好的方法,基本三四行代码就可以实现,如果只是将解析的数据放在ListView上可以果断的选择gson,但是如果大家需要对其进行操作的话还是建议大家用json,这个for循环有两个功能,一个是将json解析,另一个是将解析好的数据放在bean中,下面handler中的方法就是将其放在ListView上了,而且基本都是这么写,除非加入了Checkbox以及button,这些都是需要将操作卸载Adapter中,但是涉及到Checkbox的话则需要在fragment和activity进行具体的实现,等以后遇到博主会上代码