安卓开发之BaseAdapter用法举例,创建图形文字混合列表项
baseAdapter的用法
1.创建一个数组资源类GeneralBean
创新互联是专业的汉寿网站建设公司,汉寿接单;提供成都网站设计、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行汉寿网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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
3.定义列表横向布局(ImageView,TextView)
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:contentDescription="chenyi"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/chenyi"/>
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:text="陈毅"
android:textSize="20sp"
android:gravity="center_vertical" />
4.在主布局中添加listview
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#ccc"
android:dividerHeight="2dp"/>
5.定义listview,集合,GeneralAdapter,图片资源数组变量
ListView listview;
List
GeneralAdapter mAdapter;
int[] resid = {R.drawable.zhude,R.drawable.....};
6.将资源中的字符串数组转换成java中的字符串数组
private void initData(){
String[] names=getResources().getStringArray(R.array.city);
mGenerals = new ArrayList
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);
本文名称:安卓开发之BaseAdapter用法举例,创建图形文字混合列表项
本文URL:http://azwzsj.com/article/poiese.html