baseAdapter的用法
1.创建一个数组资源类GeneralBean
package com.example.hoyin0211.entry;
public class GeneralBean {
private int resid;
private String name;
@Override
public String toString() {
return "GeneralBean [resid=" + resid + ", name=" + name + "]";
}
public GeneralBean(int resid, String name) {
super();
this.resid = resid;
this.name = name;
}
public int getResid() {
return resid;
}
public void setResid(int resid) {
this.resid = resid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.定义字符串数组资源string-array
<string-array name="city">
<item name="bj">北京</item>
<item name="sh">上海</item>
<item name="sz">深圳</item>
<item name="gz">广州</item>
<item name="wh">武汉</item>
<item name="xa">西安</item>
<item name="hb">嘻嘻哈哈</item>
</string-array>
3.定义列表横向布局(ImageView,TextView)
<?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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivThumb"
android:contentDescription="chenyi"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/chenyi"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:text="陈毅"
android:textSize="20sp"
android:gravity="center_vertical" />
</LinearLayout>
4.在主布局中添加listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mlvTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#ccc"
android:dividerHeight="2dp"/>
</LinearLayout>
5.定义listview,集合,GeneralAdapter,图片资源数组变量
ListView listview;
List<GeneralBean> mGeneralBeans;
GeneralAdapter mAdapter;
int[] resid = {R.drawable.zhude,R.drawable.....};
6.将资源中的字符串数组转换成java中的字符串数组
private void initData(){
String[] names=getResources().getStringArray(R.array.city);
mGenerals = new ArrayList<GeneralBean>();
for(int i = 0 ; i < names.length; i++){
GeneralBean bean = new GeneralBean(Resid[i],names[i]);
mGenerals.add(bean);
}
}
7.创建BaseAdapter适配器
class GeneralAdapter extends BaseAdapter{
public int getCount(){
retuen mGenerals.size();
}
public GeneralBean getItem(int position){
return mGenerals.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position,View convertView,ViewGroup parent){
//拿到listviewitem布局,转换成view类型的对象
View layout = View.inflate(MainActivity.this,R.layout.item_general,null);
//找到p_w_picpathview
ImageView ivThube = (ImageView) layout.findViewById(R.id.ivThumb);
TextView tvName = (TextView) layout.findViewById(R.id.tvName);
//获取下标为position的图片
GeneralBean bean = mGenerals.get(position);
//显示图片
ivThumb.setImageResource(bean.getResid());
//显示姓名
tvName.setText(bean.getName());
return layout;
}
}
8.关联适配器
listview = (ListView) findViewById(R.id.mlvTest);
mAdapter = new GeneralAdapter();
listview.setAdapter(mAdapter);