有时候ListView的数据是一种类型,可以直接解析数据进行显示不需要进行额外的解析,但是有时候ListView里面有很多数据比如中间穿插了广告等内容,不止一个条目的时候,需要对解析的数据进行区分。
需要重写BaseAdapter的2个接口getItemViewType获取当前的条目类型,getViewTypeCount获取总共的条目种类的个数,下面的例子是2种
MainActivity.java
public class MainActivity extends Activity {
private ListView lv;
private List<People> lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
lists=new ArrayList<>();
//0代表学生,1代表老师
People people1 = new People(0, "10");//类型,钱
People people2 = new People(1, "100");
People people3 = new People(1, "100");
People people4 = new People(0, "10");
lists.add(people1);
lists.add(people2);
lists.add(people3);
lists.add(people4);
lv.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int i) {
return lists.get(i );
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemViewType(int position) {
if(lists.get(position).getType()==0){//当前JavaBean对象的类型
return 0;//学生类型
}else if(lists.get(position).getType()==1){
return 1;//老师类型
}else {
return 100;
}
}
@Override
public int getViewTypeCount() {
return 2;//总共有两个类型
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
int currentType = getItemViewType(position);//当前类型
if(currentType==0){//学生类型
StudentViewHolder studentViewHolder;
if(convertView==null){
studentViewHolder=new StudentViewHolder();
convertView=View.inflate(MainActivity.this,R.layout.item_lv_student,null);
studentViewHolder.tv0= (TextView) convertView.findViewById(R.id.num_money_stu);
convertView.setTag(studentViewHolder);
}else{
studentViewHolder= (StudentViewHolder) convertView.getTag();
}
//数据填充
studentViewHolder.tv0.setText(lists.get(position).getMoney());
}else if(currentType==1){//老师类型
TeacherViewHolder teacherViewHolder;
if(convertView==null){
teacherViewHolder=new TeacherViewHolder();
convertView=View.inflate(MainActivity.this,R.layout.item_lv_teacher,null);
teacherViewHolder.tv1= (TextView) convertView.findViewById(R.id.num_money_teacher);
convertView.setTag(teacherViewHolder);
}else{
teacherViewHolder= (TeacherViewHolder) convertView.getTag();
}
//数据填充
teacherViewHolder.tv1.setText(lists.get(position).getMoney());
}
return convertView;
}
}
/**学生item的Holder*/
class StudentViewHolder {
TextView tv0;
}
/**老师item的Holder*/
class TeacherViewHolder {
TextView tv1;
}
}
数据对象javaBean
/**模仿的Json对象,Json对象里一定要带类型,区分学生和老师
* Created by xhj on 16-1-8.
*/
public class People {
/**类型,0表示学生,1表示老师*/
public int type;
public String money;
public People(int type, String money) {
this.type = type;
this.money = money;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
ListView学生条目数据的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#00ff00"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生的钱:" />
<TextView
android:id="@+id/num_money_stu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学生布局"
android:id="@+id/button" />
</LinearLayout>
ListView老师条目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#f9a5b2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="老师的钱:"/>
<TextView
android:id="@+id/num_money_teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="100"/>
</LinearLayout>
显示结果