欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android notifyDataSetChanged() 動(dòng)態(tài)更新ListView案例詳解

 更新時(shí)間:2021年08月20日 10:32:29   作者:Thomas-Kwan  
這篇文章主要介紹了Android notifyDataSetChanged() 動(dòng)態(tài)更新ListView案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

有時(shí)候我們需要修改已經(jīng)生成的列表,添加或者修改數(shù)據(jù),notifyDataSetChanged()可以在修改適配器綁定的數(shù)組后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通過Handler AsyncTask兩種方式來動(dòng)態(tài)更新ListView。

<?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>
<?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;
  
 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)));
  
         }
  
     };
  
 }
 

到此這篇關(guān)于Android notifyDataSetChanged() 動(dòng)態(tài)更新ListView案例詳解的文章就介紹到這了,更多相關(guān)Android notifyDataSetChanged()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論