Android通過(guò)Handler與AsyncTask兩種方式動(dòng)態(tài)更新ListView(附源碼)
本文實(shí)例講述了Android通過(guò)Handler與AsyncTask兩種方式動(dòng)態(tài)更新ListView的方法。分享給大家供大家參考,具體如下:
有時(shí)候我們需要修改已經(jīng)生成的列表,添加或者修改數(shù)據(jù),notifyDataSetChanged()可以在修改適配器綁定的數(shù)組后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通過(guò)Handler AsyncTask兩種方式來(lái)動(dòng)態(tài)更新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項(xiàng)");//把第一項(xiàng)內(nèi)容改為"第一項(xiàng)" Handler handler=new Handler(); handler.postDelayed(add,3000);//延遲3秒執(zhí)行 } Runnable add=new Runnable(){ @Override public void run() { // TODO Auto-generated method stub arr.add("增加一項(xiàng)");//增加一項(xiàng) Adapter.notifyDataSetChanged(); } }; class editItem extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... params) { arr.set(Integer.parseInt(params[0]),params[1]); //params得到的是一個(gè)數(shù)組,params[0]在這里是"0",params[1]是"第1項(xiàng)" //Adapter.notifyDataSetChanged(); //執(zhí)行添加后不能調(diào)用 Adapter.notifyDataSetChanged()更新UI,因?yàn)榕cUI不是同線程 //下面的onPostExecute方法會(huì)在doBackground執(zhí)行后由UI線程調(diào)用 return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); Adapter.notifyDataSetChanged(); //執(zhí)行完畢,更新UI } } private OnItemClickListener lvLis=new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //點(diǎn)擊條目時(shí)觸發(fā) //arg2即為點(diǎn)中項(xiàng)的位置 setTitle(String.valueOf(arr.get(arg2))); } }; }
打包的源代碼中有錯(cuò)誤,Adapter.notifyDataSetChanged();在doInBackground中,請(qǐng)作相應(yīng)修改,感謝同學(xué)提醒。
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android搜索框(SearchView)的功能和用法詳解
這篇文章主要為大家詳細(xì)介紹了Android搜索框SearchView的功能和用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來(lái)和大家詳細(xì)聊聊2023-01-01Android開(kāi)發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹
這篇文章主要介紹了Android開(kāi)發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹,本文直接用圖片說(shuō)明Genymotion的安裝和模擬效果,并提供官網(wǎng),需要的朋友可以參考下2015-04-04Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞
這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)教程之Route跳轉(zhuǎn)以及數(shù)據(jù)傳遞的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08支持多項(xiàng)選擇的ExpandableListView
這篇文章主要為大家詳細(xì)介紹了支持多項(xiàng)選擇的ExpandableListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android開(kāi)發(fā)使用URLConnection進(jìn)行網(wǎng)絡(luò)編程詳解
這篇文章主要介紹了Android開(kāi)發(fā)使用URLConnection進(jìn)行網(wǎng)絡(luò)編程,結(jié)合實(shí)例形式分析了Android URLConnection對(duì)象創(chuàng)建、屬性、方法及相關(guān)使用技巧,需要的朋友可以參考下2018-01-01Android源碼系列之深入理解ImageView的ScaleType屬性
Android源碼系列第一篇,這篇文章主要從源碼的角度深入理解ImageView的ScaleType屬性,感興趣的小伙伴們可以參考一下2016-06-06藍(lán)牙原理Android代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了藍(lán)牙原理Android代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android一個(gè)類實(shí)現(xiàn)錄音與播放實(shí)例
大家好,本篇文章主要講的是Android一個(gè)類實(shí)現(xiàn)錄音與播放實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02