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

作者:jayqean 时间:2022-02-01 16:34:45 

本文实例讲述了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,
   View convertView, 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
0
投稿

猜你喜欢

  • java中初始化MediaRecorder的实现方法

    2023-11-29 03:54:52
  • Android 通过代码安装 APK的方法详解

    2022-11-12 14:37:26
  • SpringMVC中的几个模型对象

    2021-09-01 19:25:44
  • Java 8 中的 10 个特性总结及详解

    2023-07-21 00:06:51
  • Java上传文件错误java.lang.NoSuchMethodException的解决办法

    2023-11-10 13:15:43
  • Flask实现异步非阻塞请求功能实例解析

    2022-02-03 14:03:55
  • SpringMVC使用RESTful接口案例详解

    2022-08-08 23:41:52
  • Spring应用抛出NoUniqueBeanDefinitionException异常的解决方案

    2023-11-25 07:36:19
  • 使用Spring开启注解AOP的支持放置的位置

    2022-08-16 19:16:33
  • C#中多态、重载、重写区别分析

    2022-06-27 22:49:55
  • SpringBoot集成Redisson实现分布式锁的方法示例

    2021-10-30 16:59:38
  • Spring Boot 入门教程

    2023-05-26 00:14:44
  • java中怎样表示圆周率

    2022-11-30 11:20:00
  • Java Spring @Lazy延迟注入源码案例详解

    2023-06-24 05:21:07
  • 深入谈谈C#9新特性的实际运用

    2021-05-26 16:08:23
  • SpringBoot关于自定义注解实现接口幂等性方式

    2023-09-27 14:03:42
  • 在springboot中如何使用filter设置要排除的URL

    2022-11-06 00:06:01
  • Java解决计算相邻两个数的最大差值的问题

    2022-03-29 05:47:20
  • Windows编写jar启动脚本和关闭脚本的操作方法

    2021-05-28 04:36:58
  • C#使用foreach语句简单遍历数组的方法

    2022-02-07 08:40:24
  • asp之家 软件编程 m.aspxhome.com