您好,登錄后才能下訂單哦!
先來看下項目主要內容:
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 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里面>對應引用<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 {
//做一些耗時的操作后添加數據,之后刷新adapter
Thread.sleep( 6000 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//可能這個時候才重網絡上獲取到了新數據,,現在開始添加數據
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(); //沒有重現設置adapter,只做了刷新數據,listview不會到頂。
ToastUtils.show(getApplicationContext(), "adapter共有" +adapter.getAllList().size()+ "個數據,activity共有" +arrayList.size()+ "個數據,刷新結束!" );
};
};
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原始對象,只做數據的備份。
* 這也是本人比較喜歡的寫法,原因:很多時候我們會不經意間改變Activity里面的數據源后導致ListView的改變,
* 導致出錯后排除錯誤難解<正確示范一,當然也有好處,比如:改變Activity數據后,不需要再做多余的Adapter數據的更改,方便>)
*
* @注意 這里只是數據添加方案本人贊成寫法,具體完整結合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 {
//做一些耗時的操作后添加數據,之后刷新adapter
Thread.sleep( 6000 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<HashMap<String, String>> arrayList= new ArrayList<HashMap<String,String>>();
//可能這個時候才重網絡上獲取到了新數據,,現在開始添加數據
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" , "北京" );
}
//這里添加的只是在臨時數據存儲的對象,adapter里面不屬于同一個對象
arrayList.add(hashMap);
}
//和正確范例一區別在于此,每次都需要把數據copy到adapter里面
adapter.setAddList(arrayList);
handler.sendEmptyMessage( 1 );
}
}).start();
}
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
adapter.notifyDataSetChanged(); //沒有重現設置adapter,只做了刷新數據,ListView不會到頂。
ToastUtils.show(getApplicationContext(), "adapter共有" +adapter.getAllList().size()+ "個數據。" );
};
};
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,這是每個要做復用的都要寫的
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方法更清晰簡潔,只需要做數據的賦值和復雜的邏輯,沒有混為一灘
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,這是每個要做復用的都要寫的
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中常用知識總結
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。