ListView的适配器BaseAdapter使用详解

在Android中ListView的使用非常广泛,Android系统为我们提供了多中可供选择的适配器(Adapter),但我认为最好用的还是BaseAdapter。

一般我们继承自BaseAdapter来重写其中的方法,可以让我们实现各种各样的布局。下面贴上自己写的Demo,以便于理解:

首先是Activity的布局文件actibity_main.xml:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


            android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />


            android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/hello"
        android:background="#ff00ff00" >
   


接下来是ListVIew中单个条目list_item_layout.xml的布局:



    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
            android:id="@+id/image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:background="#ff000fff"
        android:layout_marginLeft="10dip"
        android:layout_centerVertical="true" />
    
            android:id="@+id/name"
        android:layout_width="80dip"
        android:layout_height="50dip"
        android:background="#fffff000"
        android:layout_marginLeft="120dip"
        android:layout_centerVertical="true" />


然后是定义的对象类People,用来将相关数据封装:

package com.archermind.testlistview;


public class People {


private int mId;
private int mIcon;
private String mName;


public People(int mIcon, String mName) {
super();
this.mId = (int) (Math.random() * 100);
this.mIcon = mIcon;
this.mName = mName;
}


public int getmId() {
return mId;
}


public void setmId(int mId) {
this.mId = mId;
}


public int getmIcon() {
return mIcon;
}


public void setmIcon(int mIcon) {
this.mIcon = mIcon;
}


public String getmName() {
return mName;
}


public void setmName(String mName) {
this.mName = mName;
}
}


最后就是Activity文件MainActivity.java内容:

package com.archermind.testlistview;


import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {
private ListView mListView;
private MyBaseAdapter mAdapter;
private ArrayList mData;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mData = new ArrayList();
mData.add(new People(R.drawable.ic_launcher, "Zhangsan"));
mData.add(new People(R.drawable.ic_action_search, "Lisi"));


mListView = (ListView) findViewById(R.id.list);
mAdapter = new MyBaseAdapter(mData, this);
mListView.setAdapter(mAdapter);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


private class MyBaseAdapter extends BaseAdapter {


private ArrayList mData;
private LayoutInflater mInflater = null;


public MyBaseAdapter(ArrayList data, Context context) {
this.mData = data;
mInflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return mData == null ? 0 : mData.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mData.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return mData.get(position).getmId();
}


/**

*在getView方法中使用ViewHolder与convertView可以减少绘制View,从而优化ListVIew性能

*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item_layout,
null);
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

//一般按如下方式将数据与UI联系起来
holder.image.setImageResource(mData.get(position).getmIcon());
holder.name.setText(mData.get(position).getmName());
return convertView;
}


class ViewHolder {
public ImageView image;
public TextView name;
}
}


}

你可能感兴趣的:(Android)