欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之利用ListView动态刷新某个Item

程序员文章站 2024-02-23 15:31:40
前言 本文实现的是使用viewholder来刷新某项数据,而不用每次都全部刷新数据。下面话不多说,来看看详细的介绍。 实现方法 继承baseadapter,新建v...

前言

本文实现的是使用viewholder来刷新某项数据,而不用每次都全部刷新数据。下面话不多说,来看看详细的介绍。

实现方法

继承baseadapter,新建viewholder类。

public class testlistadapter extends baseadapter { 
 private context mcontext; 
 
 private list<string> strlist; 
 
 public testlistadapter(context context, list<string> list) { 
  super(); 
  this.mcontext = context; 
  this.strlist = list; 
 } 
 
 @override 
 public int getcount() { 
  // todo auto-generated method stub 
  return strlist.size(); 
 } 
 
 @override 
 public object getitem(int position) { 
  // todo auto-generated method stub 
  return position; 
 } 
 
 @override 
 public long getitemid(int position) { 
  // todo auto-generated method stub 
  return position; 
 } 
 
 @override 
 public view getview(int position, view convertview, viewgroup parent) { 
  // todo auto-generated method stub 
  viewholder holder = null; 
  if (null == convertview) { 
   convertview = layoutinflater.from(mcontext).inflate(r.layout.line, null); 
 
   holder = new viewholder(); 
   holder.idtext = (textview) convertview.findviewbyid(r.id.textview_id); 
   holder.strtext = (textview) convertview.findviewbyid(r.id.textview_str); 
 
   convertview.settag(holder); 
  } else { 
   holder = (viewholder) convertview.gettag(); 
  } 
 
  holder.idtext.settext(position + ""); 
  string str = strlist.get(position); 
  holder.strtext.settext(str); 
 
  return convertview; 
 } 
 
 private static class viewholder { 
  private textview idtext; 
 
  private textview strtext; 
 } 
 
 public void updataview(int posi, listview listview) { 
  int visiblefirstposi = listview.getfirstvisibleposition(); 
  int visiblelastposi = listview.getlastvisibleposition(); 
  if (posi >= visiblefirstposi && posi <= visiblelastposi) { 
   view view = listview.getchildat(posi - visiblefirstposi); 
   viewholder holder = (viewholder) view.gettag(); 
 
   string txt = holder.strtext.gettext().tostring(); 
   txt = txt + "++;"; 
   holder.strtext.settext(txt); 
   strlist.set(posi, txt); 
  } else { 
   string txt = strlist.get(posi); 
   txt = txt + "++;"; 
   strlist.set(posi, txt); 
  } 
 } 
} 

在activity中,调用updateview()方法,刷新数据。

public class mainactivity extends activity { 
 private mainactivity mcontext; 
 
 private edittext idedit; 
 
 private textview textview; 
 
 private list<string> strlist = new arraylist<string>(); 
 private listview listview; 
 private testlistadapter listadapter; 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
 
  mcontext = this; 
 
  for (int i = 0; i < 100; i++) { 
   strlist.add("test data"); 
  } 
 
  idedit = (edittext) findviewbyid(r.id.edittext_id); 
  textview = (textview) findviewbyid(r.id.textview_modify); 
 
  listview = (listview) findviewbyid(r.id.listview); 
  listadapter = new testlistadapter(mcontext, strlist); 
  listview.setadapter(listadapter); 
 
  //动态刷新 
  textview.setonclicklistener(new onclicklistener() { 
 
   @override 
   public void onclick(view v) { 
    // todo auto-generated method stub 
    string idstr = idedit.gettext().tostring(); 
    int idint = integer.parseint(idstr); 
    listadapter.updataview(idint, listview);//动态修改 
   } 
  }); 
 } 
} 

给出布局文件:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#ffffff" 
 android:orientation="vertical" > 
 
 <linearlayout 
  android:layout_width="match_parent" 
  android:layout_height="40dp" 
  android:orientation="horizontal" > 
 
  <edittext 
   android:id="@+id/edittext_id" 
   android:layout_width="200dp" 
   android:layout_height="wrap_content" 
   android:hint="put modify id" /> 
 
  <textview 
   android:id="@+id/textview_modify" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="动态修改" 
   android:textcolor="#123456" /> 
 </linearlayout> 
 
 <listview 
  android:id="@+id/listview" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" > 
 </listview> 
 
</linearlayout> 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。