notifyDataSetChanged() 动态更新ListView
来源:互联网 发布:数据共享方式比较 编辑:程序博客网 时间:2024/06/05 23:01
有时候我们需要修改已经生成的列表,添加或者修改数据,notifyDataSetChanged()可以在修改适配器绑定的数组后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通过Handler AsyncTask两种方式来动态更新ListView.从今天起,每次学习的源代码都会打包上传,方便各位同学学习,注册帐号即可下载。
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))); } };}
查看原文
0 0
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- (转)notifyDataSetChanged() 动态更新ListView
- [转]notifyDataSetChanged() 动态更新ListView
- 【Android】使用notifyDataSetChanged()动态更新ListView
- notifyDataSetChanged() 动态更新ListView,知识点比较全
- notifyDataSetChanged() 动态更新ListView android线程延迟
- Codeforces 567F
- 黑马程序员-----java基础之运算符----与(&)、或(|)、非(!)、异或(^)、短路与(&&)等
- [POJ 3169] Layout 差分约束
- 计蒜客 难题题库 014 罗马数字转换成整数
- I学霸官方免费教程五:Java基础之运算符
- notifyDataSetChanged() 动态更新ListView
- 082 Remove Duplicates from Sorted List II [Leetcode]
- SSH整合环境下Spring配置文件的配置
- 计蒜客 难题题库 015 单独的数字
- [leetcode-89]Gray Code(c++)
- 父母之间——我的独立
- 正则表达式的知识点汇总(一)
- 最近的一些小感慨
- 计蒜客 难题题库 016 爬楼梯