baseAdapter的用法
1.創(chuàng)建一個數(shù)組資源類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.定義字符串?dāng)?shù)組資源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,圖片資源數(shù)組變量
ListView listview;
List<GeneralBean> mGeneralBeans;
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<GeneralBean>();
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類型的對象
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);
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站欄目:安卓開發(fā)之BaseAdapter用法舉例,創(chuàng)建圖形文字混合列表項-創(chuàng)新互聯(lián)
新聞來源:http://jinyejixie.com/article24/digece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、App設(shè)計、網(wǎng)站收錄、自適應(yīng)網(wǎng)站、網(wǎng)站營銷、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容