編輯:關於Android編程
本文實例講述了Android通過Handler與AsyncTask兩種方式動態更新ListView的方法。分享給大家供大家參考,具體如下:
有時候我們需要修改已經生成的列表,添加或者修改數據,notifyDataSetChanged()可以在修改適配器綁定的數組後,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通過Handler AsyncTask兩種方式來動態更新ListView.
布局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>
程序代碼:
package com.pocketdigi;
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;
public class main extends Activity {
/** Called when the activity is first created. */
ListView lv;
ArrayAdapter<String> Adapter;
ArrayList<String> arr=new ArrayList<String>();
@Override
public void 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項");//把第一項內容改為"第一項"
Handler handler=new Handler();
handler.postDelayed(add,3000);//延遲3秒執行
}
Runnable add=new Runnable(){
@Override
public void 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得到的是一個數組,params[0]在這裡是"0",params[1]是"第1項"
//Adapter.notifyDataSetChanged();
//執行添加後不能調用 Adapter.notifyDataSetChanged()更新UI,因為與UI不是同線程
//下面的onPostExecute方法會在doBackground執行後由UI線程調用
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Adapter.notifyDataSetChanged();
//執行完畢,更新UI
}
}
private OnItemClickListener lvLis=new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//點擊條目時觸發
//arg2即為點中項的位置
setTitle(String.valueOf(arr.get(arg2)));
}
};
}
打包的源代碼中有錯誤,Adapter.notifyDataSetChanged();在doInBackground中,請作相應修改,感謝同學提醒。
完整實例代碼代碼點擊此處本站下載。
希望本文所述對大家Android程序設計有所幫助。
Android學習之文件存儲讀取
前言相信大家都知道知道,在AndroidOS中,提供了五中數據存儲方式,分別是:ContentProvider存儲、文件存儲、SharedPreference存儲、SQL
魅族pro6怎麼制作GIF動態圖 魅族pro6 GIF動態圖制作教程
動態圖現今已經成為各位機友們發朋友圈和說說必不可少的神器,但是作為手機黨要做動態圖就得費上不少時間和功夫了。那麼魅族pro6怎麼制作GIF動態圖呢?下面咱們
Android開發常用功能
1.定時器的實現(1)采用Handler的postDelayed(Runnable, long)方法 Handler handler =&n
Android Launcher源碼研究(三) 加載app流程2
接上次的。 首先Launcher實現了LauncherModel.Callbacks接口,APP信息數據加載成功後 ,回調接口把app信息顯示到Launcher的 wor