android中ListView数据刷新时的同步方法

android中ListView数据刷新时的同步方法,第1张

概述本文实例讲述了android中ListView数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下:

本文实例讲述了androID中ListVIEw数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下:

public class Main extends BaseActivity {  private static final String TAG = "tag";  private static final int STATUS_CHANGE = 0;  ExpandableListVIEw mElv;  ArrayList<GroupInfo> mGroupArray;  @OverrIDe  public voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.main);   mElv = (ExpandableListVIEw) findVIEwByID(R.ID.contact_List);   mStatus = (TextVIEw) findVIEwByID(R.ID.setStatus);   mGroupArray = getIntent().getParcelableArrayListExtra("groupArray");// => 取数据   mExpandableAdapter = new ExpandableAdapter(this,Main.this);   mElv.setAdapter(mExpandableAdapter);     // 异步对比服务器分组和本地分组   HandlerThread handlerThread = new HandlerThread("handler_thread");   handlerThread.start();   UpdateGroupHandler myHandler = new UpdateGroupHandler(     handlerThread.getLooper());   Message message = myHandler.obtainMessage();   message.sendToTarget();   mHandler = new Handler() {    public voID handleMessage(Message msg) {     switch (msg.what) {     case STATUS_CHANGE:      // 处理UI更新等 *** 作      updateUI();      break;     }    };   };   }  /**   * 发送消息更新UI   */  private voID sendMessagetoUpdateUI() {   Message msg = new Message();   msg.what = STATUS_CHANGE;   mHandler.sendMessage(msg);  // 向Handler发送消息,更新UI  }  private voID updateUI() {   // 详细的更新   mExpandableAdapter.notifyDataSetChanged();  // 更新ExpandableListVIEw  }  /**   * 异步刷新分组的handler   *   * @author administrator   *   */  class UpdateGroupHandler extends Handler {   public UpdateGroupHandler() {   }   public UpdateGroupHandler(Looper looper) {    super(looper);   }   @OverrIDe   public voID handleMessage(Message msg) {    ContactsManagerDbAdapter dbAdapter = new ContactsManagerDbAdapter(      Main.this);    dbAdapter.open();    // =>doSomeThing...    mGroupArray = groupList;    System.out.println("========数据更新后,刷新ListvIEw=========");    sendMessagetoUpdateUI();   }  }  private class ExpandableAdapter extends Baseexpandablelistadapter {   Activity activity;   LayoutInflater layoutInflater;   public ExpandableAdapter(Activity a,Context context) {    activity = a;    layoutInflater = (LayoutInflater) context      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);   }   public Object getChild(int groupposition,int childposition) {    return mGroupArray.get(groupposition).getChildList()      .get(childposition);   }   public long getChildID(int groupposition,int childposition) {    return childposition;   }   public int getChildrenCount(int groupposition) {    return mGroupArray.get(groupposition).getChildList().size();   }   public VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,VIEw convertVIEw,VIEwGroup parent) {    // .....    return vi;   }   public Object getGroup(int groupposition) {    return mGroupArray.get(groupposition);   }   public int getGroupCount() {    return mGroupArray.size();   }   public long getGroupID(int groupposition) {    return groupposition;   }   public VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEwGroup parent) {    GroupInfo groupInfo = mGroupArray.get(groupposition);    String string = groupInfo.getname();    convertVIEw = (VIEw) layoutInflater.inflate(R.layout.group_layout,null);    final TextVIEw textVIEw = (TextVIEw) convertVIEw      .findVIEwByID(R.ID.groupname);    if (textVIEw != null) {     textVIEw.setText(string);    }    return convertVIEw;   }   public boolean hasStableIDs() {    return true;   }   public boolean isChildSelectable(int groupposition,int childposition) {    return true;   }  } }

代码只是提取的部分,应该没多大影响.

上面集合mGroupArray存在数据共享,测试多次发现报错有两种:

=>1.java.lang.indexoutofboundsexception: InvalID location 3,size is 3
=>2.The content of the adapter has changed but ListVIEw dID not receive a notification. Make sure the content of your adapter is not modifIEd from a background thread,but only from the UI thread.

第一个问题,数据同步问题,我弄了下没解决.
第二个问题,改变适配器Adapter内容时不要在后台线程中,必须在UI线程中处理
我将上面子线程UpdateGroupHandler里的数据更新利用handler提取到了主线程赋值

Message.obj = groupList;

额,改好后测试多次,发现这两问题都解决了,发现还是对handler理解的不够.

发下更改的代码段:

@OverrIDe public voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.main);  mElv = (ExpandableListVIEw) findVIEwByID(R.ID.contact_List);  mStatus = (TextVIEw) findVIEwByID(R.ID.setStatus);  mGroupArray = getIntent().getParcelableArrayListExtra("groupArray"); // => 取数据  mExpandableAdapter = new ExpandableAdapter(this,Main.this);  mElv.setAdapter(mExpandableAdapter);    // 异步对比服务器分组和本地分组  HandlerThread handlerThread = new HandlerThread("handler_thread");  handlerThread.start();  UpdateGroupHandler myHandler = new UpdateGroupHandler(    handlerThread.getLooper());  Message message = myHandler.obtainMessage();  message.sendToTarget();  mHandler = new Handler() {   public voID handleMessage(Message msg) {    switch (msg.what) {    case STATUS_CHANGE:     // 处理UI更新等 *** 作     updateUI(msg.obj);     break;    }   };  };  } /** * 发送消息更新UI */ private voID sendMessagetoUpdateUI(ArrayList<GroupInfo> groupList) {  Message msg = new Message();  msg.what = STATUS_CHANGE;  msg.obj = groupList;  mHandler.sendMessage(msg); // 向Handler发送消息,更新UI }  @SuppressWarnings("unchecked") private voID updateUI(Object obj) {  // 详细的更新  mGroupArray = (ArrayList<GroupInfo>) obj;  mExpandableAdapter.notifyDataSetChanged(); // 更新ExpandableListVIEw } /**  * 异步刷新分组的handler  *  * @author administrator  *  */ class UpdateGroupHandler extends Handler {  public UpdateGroupHandler() {  }  public UpdateGroupHandler(Looper looper) {   super(looper);  }  @OverrIDe  public voID handleMessage(Message msg) {   ContactsManagerDbAdapter dbAdapter = new ContactsManagerDbAdapter(     Main.this);   dbAdapter.open();   // =>doSomeThing...   System.out.println("========数据更新后,刷新ListvIEw=========");   sendMessagetoUpdateUI(groupList);  } }

希望本文所述对大家的AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的android中ListView数据刷新时的同步方法全部内容,希望文章能够帮你解决android中ListView数据刷新时的同步方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存