BaseAdapter的使用方法介绍

BaseAdapter是Android中常用的适配器之一,SimpleAdapter虽然好用,但是在填充一些复杂布局的时候SimpleAdapter用起来还是会觉得捉襟见肘。而BaseAdapter给复杂界面填充数据时就比SimpleAdapter好用多了。

不多BB,直接看代码:

首先写一个继承自BaseAdapter的类MyAdapter。

BaseAdapter最基本的几个方法:

1. getCount    填充的数据集数

2. getItem       数据集中指定索引对应的数据项

3. getItemId    指定行所对应的ID   

4. getView       每个Item所显示的类容

class MyAdapter extends BaseAdapter {
   Context context;
   List data;    //数据源
   int layout;       //Item布局

   public MyAdapter(Context context, int layout ,List data){
      this.context=context;
      this.layout=layout;
      this.data=data;
   }

   @Override
   public int getCount() {
      return data == null ? 0 : data.size();
   }

   @Override
   public Object getItem(int position) {
      return data == null ? null : data.get(position);
   }

   @Override
   public long getItemId(int position) {
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder=null;
      ImageView imageView_sex;
      TextView textView_name;
      TextView  textView_sex;
      TextView  textView_age;
      final PeopleBeans peopleBeans=data.get(position);
      if (convertView==null) {
         viewHolder = new ViewHolder();
         convertView=LayoutInflater.from(context).inflate(layout,null);
         viewHolder.btn_setting= (Button) convertView.findViewById(R.id.btn_setting);
         convertView.setTag(viewHolder);
      }else {
         viewHolder= (ViewHolder) convertView.getTag();
      }
      imageView_sex= (ImageView) convertView.findViewById(R.id.image_sex);
      textView_name= (TextView) convertView.findViewById(R.id.textview_name);
      textView_sex= (TextView) convertView.findViewById(R.id.textview_sex);
      textView_age= (TextView) convertView.findViewById(R.id.textview_age);
      imageView_sex.setImageResource(peopleBeans.getSexImage());
      textView_name.setText(peopleBeans.getName());
      textView_sex.setText(peopleBeans.getSex());
      textView_age.setText(peopleBeans.getAge()+"");
      viewHolder.btn_setting.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(context, peopleBeans.toString(), Toast.LENGTH_SHORT).show();
         }
      });
      return convertView;
   }
}
 
  
class ViewHolder{   
   Button btn_setting;
}

class PeopleBeans{
   int sexImage;
   String name;
   String sex;
   int age;

   public int getSexImage() {
      return sexImage;
   }

   public void setSexImage(int sexImage) {
      this.sexImage = sexImage;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getSex() {
      return sex;
   }

   public void setSex(String sex) {
      this.sex = sex;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   @Override
   public String toString() {
      return "sexImage=" + sexImage +
            ", name='" + name + '\'' +
            ", sex='" + sex + '\'' +
            ", age=" + age;
   }
}

Item布局代码

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

            android:id="@+id/image_sex"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

            android:id="@+id/textview_name"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

            android:id="@+id/textview_sex"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

            android:id="@+id/textview_age"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    

现在我们的MyAdapter类已经写好了接下来我们来测试一下吧

listViewTest = (ListView) view.findViewById(R.id.listView_test);
for (int i = 0; i <4 ; i++) {
   PeopleBeans peopleBeans=new PeopleBeans();
   peopleBeans.setSexImage(i==2?R.drawable.touxiang_1:R.drawable.touxiang_2);
   peopleBeans.setName(names[i]);
   peopleBeans.setSex(i==2?"女":"男");
   peopleBeans.setAge(random.nextInt(60));
   data.add(peopleBeans);
}
adapter=new MyAdapter(getActivity(),R.layout.item_people,data);
listViewTest.setAdapter(adapter);

效果图:

BaseAdapter的使用方法介绍_第1张图片







你可能感兴趣的:(BaseAdapter的使用方法介绍)