先來看下項目主要內(nèi)容:
創(chuàng)新互聯(lián)專注于做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴謹?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。
ListView中填充數(shù)據(jù):
重現(xiàn)添加數(shù)據(jù)后置頂,具體闡明了決解方案,如下:
刷新適配器后沒有響應(yīng)的錯誤現(xiàn)象,具體闡明了決解方案,如下:
正確示范一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /**
* 正確示范一(正確運用,修改原始對象<Activity里面>對應(yīng)引用<Adapter里面>也改變)
*
* @author johnny
*
*/ public class ThreeListViewActivity extends Activity {
private ListView mContentLv;
private OneAdapter adapter;
private ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mContentLv = (ListView) findViewById(R.id.lv_content);
adapter= new OneAdapter( this ,arrayList);
mContentLv.setAdapter(adapter);
initData();
ToastUtils.show(getApplicationContext(), "6秒后延遲添加,刷新adapter" );
//開啟異步線程
setData();
}
private void setData() {
new Thread( new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
//做一些耗時的操作后添加數(shù)據(jù),之后刷新adapter
Thread.sleep( 6000 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//可能這個時候才重網(wǎng)絡(luò)上獲取到了新數(shù)據(jù),,現(xiàn)在開始添加數(shù)據(jù)
HashMap<String, String> hashMap;
for ( int i = 0 ; i < 5 ; i++) {
hashMap = new HashMap<String, String>();
if (i % 4 == 0 ) {
hashMap.put( "name" , "大海" );
hashMap.put( "address" , "上海" );
} else if (i % 4 == 1 ) {
hashMap.put( "name" , "老馬" );
hashMap.put( "address" , "深圳" );
} else if (i % 4 == 2 ) {
hashMap.put( "name" , "小三" );
hashMap.put( "address" , "東莞" );
} else if (i % 4 == 3 ) {
hashMap.put( "name" , "老哥" );
hashMap.put( "address" , "北京" );
}
arrayList.add(hashMap);
}
handler.sendEmptyMessage( 1 );
}
}).start();
}
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
adapter.notifyDataSetChanged(); //沒有重現(xiàn)設(shè)置adapter,只做了刷新數(shù)據(jù),listview不會到頂。
ToastUtils.show(getApplicationContext(), "adapter共有" +adapter.getAllList().size()+ "個數(shù)據(jù),activity共有" +arrayList.size()+ "個數(shù)據(jù),刷新結(jié)束!" );
};
};
private void initData() {
HashMap<String, String> hashMap;
for ( int i = 0 ; i < 15 ; i++) {
hashMap = new HashMap<String, String>();
if (i % 4 == 0 ) {
hashMap.put( "name" , "小明" );
hashMap.put( "address" , "上海" );
} else if (i % 4 == 1 ) {
hashMap.put( "name" , "老馬" );
hashMap.put( "address" , "深圳" );
} else if (i % 4 == 2 ) {
hashMap.put( "name" , "小三" );
hashMap.put( "address" , "東莞" );
} else if (i % 4 == 3 ) {
hashMap.put( "name" , "老哥" );
hashMap.put( "address" , "北京" );
}
arrayList.add(hashMap);
}
} } |
正確示范二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | /**
* 正確示范二(正確運用,Activity里面對象不用作Adapter原始對象,只做數(shù)據(jù)的備份。
* 這也是本人比較喜歡的寫法,原因:很多時候我們會不經(jīng)意間改變Activity里面的數(shù)據(jù)源后導(dǎo)致ListView的改變,
* 導(dǎo)致出錯后排除錯誤難解<正確示范一,當(dāng)然也有好處,比如:改變Activity數(shù)據(jù)后,不需要再做多余的Adapter數(shù)據(jù)的更改,方便>)
*
* @注意 這里只是數(shù)據(jù)添加方案本人贊成寫法,具體完整結(jié)合adapter請移步個人推薦一或二<有錯誤示范,才能慢慢完善改變,錯誤何嘗不是一種成長>
* @author johnny
*
*/ public class FourListViewActivity extends Activity {
private ListView mContentLv;
private OneAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mContentLv = (ListView) findViewById(R.id.lv_content);
adapter= new OneAdapter( this );
mContentLv.setAdapter(adapter);
initData();
ToastUtils.show(getApplicationContext(), "6秒后延遲添加,刷新adapter" );
//開啟異步線程
setData();
}
private void setData() {
new Thread( new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
//做一些耗時的操作后添加數(shù)據(jù),之后刷新adapter
Thread.sleep( 6000 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String,String>>();
//可能這個時候才重網(wǎng)絡(luò)上獲取到了新數(shù)據(jù),,現(xiàn)在開始添加數(shù)據(jù)
HashMap<String, String> hashMap;
for ( int i = 0 ; i < 5 ; i++) {
hashMap = new HashMap<String, String>();
if (i % 4 == 0 ) {
hashMap.put( "name" , "大海" );
hashMap.put( "address" , "上海" );
} else if (i % 4 == 1 ) {
hashMap.put( "name" , "老馬" );
hashMap.put( "address" , "深圳" );
} else if (i % 4 == 2 ) {
hashMap.put( "name" , "小三" );
hashMap.put( "address" , "東莞" );
} else if (i % 4 == 3 ) {
hashMap.put( "name" , "老哥" );
hashMap.put( "address" , "北京" );
}
//這里添加的只是在臨時數(shù)據(jù)存儲的對象,adapter里面不屬于同一個對象
arrayList.add(hashMap);
}
//和正確范例一區(qū)別在于此,每次都需要把數(shù)據(jù)copy到adapter里面
adapter.setAddList(arrayList);
handler.sendEmptyMessage( 1 );
}
}).start();
}
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
adapter.notifyDataSetChanged(); //沒有重現(xiàn)設(shè)置adapter,只做了刷新數(shù)據(jù),ListView不會到頂。
ToastUtils.show(getApplicationContext(), "adapter共有" +adapter.getAllList().size()+ "個數(shù)據(jù)。" );
};
};
private void initData() {
ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String,String>>();
HashMap<String, String> hashMap;
for ( int i = 0 ; i < 15 ; i++) {
hashMap = new HashMap<String, String>();
if (i % 4 == 0 ) {
hashMap.put( "name" , "小明" );
hashMap.put( "address" , "上海" );
} else if (i % 4 == 1 ) {
hashMap.put( "name" , "老馬" );
hashMap.put( "address" , "深圳" );
} else if (i % 4 == 2 ) {
hashMap.put( "name" , "小三" );
hashMap.put( "address" , "東莞" );
} else if (i % 4 == 3 ) {
hashMap.put( "name" , "老哥" );
hashMap.put( "address" , "北京" );
}
arrayList.add(hashMap);
}
adapter.setAddList(arrayList);
} } |
ListView 適配器推薦寫法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | @Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
//判斷是否為null,這是每個要做復(fù)用的都要寫的
if (v == null ) {
v = inflater.inflate(R.layout.lv_item, null );
}
//相比之下
//寫法太集中,沒有細化
/*if (null == convertView) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.lv_item, null);
holder.mNameTv = (TextView) convertView.findViewById(R.id.tv_name);
holder.mAddressTv = (TextView) convertView.findViewById(R.id.tv_address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}*/
//簡潔,把初始化控件放到了ViewHolder里面,是的getView方法更清晰簡潔,只需要做數(shù)據(jù)的賦值和復(fù)雜的邏輯,沒有混為一灘
ViewHolder holder = (ViewHolder) v.getTag();
if (holder == null ) {
holder = new ViewHolder(v);
v.setTag(holder);
}
HashMap<String, String> hashMap = getItem(position);
holder.mNameTv.setText(hashMap.get( "name" ));
holder.mAddressTv.setText(hashMap.get( "address" ));
return v;
} |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
//判斷是否為null,這是每個要做復(fù)用的都要寫的
if (v == null ) {
v = inflater.inflate(R.layout.lv_item, null );
}
//簡潔,方便,快速
TextView mNameTv=ViewHolder.get(v, R.id.tv_name);
TextView mAddressTv=ViewHolder.get(v, R.id.tv_address);
HashMap<String, String> hashMap = getItem(position);
mNameTv.setText(hashMap.get( "name" ));
mAddressTv.setText(hashMap.get( "address" ));
return v;
} |
ListView中疑難雜癥:
只做截圖具體下載代碼查看:不需要豆子
e
另鏈接單選刪除帖子效果如下:
地址:點擊進入
×××:
ListView中常用知識總結(jié)
當(dāng)前名稱:androidListView常用知識總結(jié)
路徑分享:http://aaarwkj.com/article18/gjgidp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、電子商務(wù)、做網(wǎng)站、品牌網(wǎng)站制作、App開發(fā)
聲明:本網(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)