Android編程實現(xiàn)動態(tài)更新ListView的方法
本文實例講述了Android編程實現(xiàn)動態(tài)更新ListView的方法。分享給大家供大家參考,具體如下:
有時候我們需要修改已經(jīng)生成的列表,添加或者修改數(shù)據(jù),notifyDataSetChanged()可以在修改適配器綁定的數(shù)組后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通過Handler AsyncTask兩種方式來動態(tài)更新ListView.從今天起,每次學(xué)習的源代碼都會打包上傳,方便各位同學(xué)學(xué)習,注冊帳號即可下載。
布局main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
ListView列表布局playlist.xml:
<?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="30px" android:textSize="18sp" > </TextView>
程序代碼:
import java.util.ArrayList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
publicclass main extends Activity {
/** Called when the activity is first created. */
ListView lv;
ArrayAdapter<String> Adapter;
ArrayList<String> arr=new ArrayList<String>();
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.lv);
arr.add("123");
arr.add("234");
arr.add("345");
Adapter =new ArrayAdapter<String>(this,R.layout.playlist, arr);
lv.setAdapter(Adapter);
lv.setOnItemClickListener(lvLis);
editItem edit=new editItem();
edit.execute("0","第1項");//把第一項內(nèi)容改為"第一項"
Handler handler=new Handler();
handler.postDelayed(add,3000);//延遲3秒執(zhí)行
}
Runnable add=new Runnable(){
@Override
publicvoid run() {
// TODO Auto-generated method stub
arr.add("增加一項");//增加一項
Adapter.notifyDataSetChanged();
}
};
class editItem extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
arr.set(Integer.parseInt(params[0]),params[1]);
//params得到的是一個數(shù)組,params[0]在這里是"0",params[1]是"第1項"
//Adapter.notifyDataSetChanged();
//執(zhí)行添加后不能調(diào)用 Adapter.notifyDataSetChanged()更新UI,因為與UI不是同線程
//下面的onPostExecute方法會在doBackground執(zhí)行后由UI線程調(diào)用
returnnull;
}
@Override
protectedvoid onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Adapter.notifyDataSetChanged();
//執(zhí)行完畢,更新UI
}
}
private OnItemClickListener lvLis=new OnItemClickListener(){
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//點擊條目時觸發(fā)
//arg2即為點中項的位置
setTitle(String.valueOf(arr.get(arg2)));
}
};
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android實現(xiàn)listview動態(tài)加載數(shù)據(jù)分頁的兩種方法
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實例代碼
- android動態(tài)布局之動態(tài)加入TextView和ListView的方法
- Android實現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動態(tài)更新的方法
- Android開發(fā)之利用ListView動態(tài)刷新某個Item
- Android listview動態(tài)加載列表項實現(xiàn)代碼
- Android實現(xiàn)ListView數(shù)據(jù)動態(tài)加載的方法
- Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView(附源碼)
- Android ListView中動態(tài)顯示和隱藏Header&Footer的方法
- Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法
- Android開發(fā)中Listview動態(tài)加載數(shù)據(jù)的方法示例
相關(guān)文章
詳解Android TextView屬性ellipsize多行失效的解決思路
這篇文章主要介紹了Android TextView屬性ellipsize多行失效的解決思路,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

