baseAdapter的用法
1.創(chuàng)建一個(gè)數(shù)組資源類GeneralBean
創(chuàng)新互聯(lián)是專業(yè)的漢壽網(wǎng)站建設(shè)公司,漢壽接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行漢壽網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
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.定義字符串?dāng)?shù)組資源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,圖片資源數(shù)組變量
ListView listview;
List
GeneralAdapter mAdapter;
int[] resid = {R.drawable.zhude,R.drawable.....};
6.將資源中的字符串?dāng)?shù)組轉(zhuǎn)換成java中的字符串?dāng)?shù)組
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.創(chuàng)建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布局,轉(zhuǎn)換成view類型的對(duì)象
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);
//獲取下標(biāo)為position的圖片
GeneralBean bean = mGenerals.get(position);
//顯示圖片
ivThumb.setImageResource(bean.getResid());
//顯示姓名
tvName.setText(bean.getName());
return layout;
}
}
8.關(guān)聯(lián)適配器
listview = (ListView) findViewById(R.id.mlvTest);
mAdapter = new GeneralAdapter();
listview.setAdapter(mAdapter);