发布于 2016-05-19 12:44:21 | 115 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现动态更新ListView的方法,结合实例形式详细分析了ListView的布局及动态更新实现方法,需要的朋友可以参考下

本文实例讲述了Android编程实现动态更新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>

程序代码:


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项");//把第一项内容改为"第一项"
     Handler handler=new Handler();
     handler.postDelayed(add,3000);//延迟3秒执行
   }
   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得到的是一个数组,params[0]在这里是"0",params[1]是"第1项"
 //Adapter.notifyDataSetChanged();
 //执行添加后不能调用 Adapter.notifyDataSetChanged()更新UI,因为与UI不是同线程
 //下面的onPostExecute方法会在doBackground执行后由UI线程调用
 returnnull;
     }
     @Override
 protectedvoid onPostExecute(String result) {
 // TODO Auto-generated method stub
 super.onPostExecute(result);
       Adapter.notifyDataSetChanged();
 //执行完毕,更新UI
     }
   }
 private OnItemClickListener lvLis=new OnItemClickListener(){
     @Override
 publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
 long arg3) {
 //点击条目时触发
 //arg2即为点中项的位置
       setTitle(String.valueOf(arr.get(arg2)));
     }
   };
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务