ListView加载两种布局
package com.jsontest.utils;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.example.newstextdemo.R;
import com.jsontest.bean.DataClass;
/**
*
* @author FuShanLin
*
*/
public class TheBaseAdapter extends BaseAdapter{
private Context mContext;
static final int TYPE_ONE = 0;// 第一种布局
static final int TYPE_TWO = 1;// 第二种布局
private List<DataClass> data_list;
public TheBaseAdapter(Context context, ArrayList<DataClass> mList) {
super();
this.mContext = context;
this.data_list = mList;
}
//布局1
class viewHolder1{
TextView newsFlash_tv_time;
TextView newsFlash_content;
}
//布局2
class viewHolder2{
TextView indexEvent_tv_time;
TextView indexEvent_content;
TextView qianZhi;
TextView yuQi;
TextView gongBu;
RatingBar zhibiao;
CircleImg circle_photo;
ImageView positiveItem;
ImageView negativeItem;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int getItemViewType(int positions) {
if ("Newsflash".equals(data_list.get(positions).getType())) {
return TYPE_ONE;
} else if("IndexEvent".equals(data_list.get(positions).getType())){
return TYPE_TWO;
}
return super.getItemViewType(positions);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
viewHolder1 holder1 = null;
viewHolder2 holder2 = null;
if(convertView == null){
switch (type) {
case TYPE_ONE:
convertView = LayoutInflater.from(mContext).inflate(R.layout.newsflash, null);
holder1 = new viewHolder1();
holder1.newsFlash_tv_time = (TextView) convertView.findViewById(R.id.newsFlash_tv_time);
holder1.newsFlash_content = (TextView) convertView.findViewById(R.id.newsFlash_content);
convertView.setTag(holder1);
break;
case TYPE_TWO:
convertView = LayoutInflater.from(mContext).inflate(R.layout.indexevent, null);
holder2 = new viewHolder2();
holder2.indexEvent_tv_time = (TextView) convertView.findViewById(R.id.indexEvent_tv_time);
holder2.indexEvent_content = (TextView) convertView.findViewById(R.id.indexEvent_content);
holder2.qianZhi = (TextView) convertView.findViewById(R.id.qianZhi);
holder2.yuQi = (TextView) convertView.findViewById(R.id.yuQi);
holder2.gongBu = (TextView) convertView.findViewById(R.id.gongBu);
holder2.zhibiao = (RatingBar) convertView.findViewById(R.id.zhibiao);
holder2.circle_photo = (CircleImg) convertView.findViewById(R.id.circle_photo);
holder2.positiveItem = (ImageView) convertView.findViewById(R.id.positiveItem);
holder2.negativeItem = (ImageView) convertView.findViewById(R.id.negativeItem);
convertView.setTag(holder2);
break;
}
}
else {
switch (type) {
case TYPE_ONE:
holder1 = (viewHolder1) convertView.getTag();
break;
case TYPE_TWO:
holder2 = (viewHolder2) convertView.getTag();
break;
}
}
switch (type) {
case TYPE_ONE:
//解析第一种Json,需要两层解析,获得第position个Java类填入适配器
holder1.newsFlash_content.setText(data_list.get(position).getEntity());
break;
case TYPE_TWO:
//解析第二种Json
holder2.indexEvent_tv_time.setText(data_list.get(position).getType());
// JSONObject jsonObject,jsonObject2;
// try {
// jsonObject = new JSONObject("{"+"data"+":["+data_list.get(position).getEntity()+"]}");
// JSONArray jsonArray = jsonObject.getJSONArray("data");
// System.out.println(jsonArray);
// jsonObject2 = jsonArray.getJSONObject(0);
// Type2_Entity type2 = new Type2_Entity(jsonObject2.getString("name"),
// jsonObject2.getString("country"),
// jsonObject2.getString("basicIndexId"),
// jsonObject2.getString("period"),
// jsonObject2.getString("importance"),
// jsonObject2.getString("lastValue"),
// jsonObject2.getString("predictValue"),
// jsonObject2.getString("value"),
// jsonObject2.getString("year"),
// jsonObject2.getString("positiveItem"),
// jsonObject2.getString("negativeItem"),
// jsonObject2.getString("level"),
// jsonObject2.getString("url"),
// jsonObject2.getString("date"),
// jsonObject2.getString("time"));
//holder2.indexEvent_tv_time.setText(type2.getYear()+type2.getTime());
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
break;
}
return convertView;
}
}