android中ListView数据刷新时的同步

<groupinfo></groupinfo>

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; } } }

<groupinfo>代码只是提取的部分,应该没多大影响.<br></groupinfo>

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

<groupinfo>=&gt;1.java.lang.IndexOutOfBoundsException: Invalid location 3, size is 3</groupinfo>

<groupinfo>=&gt;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.</groupinfo>

<groupinfo>第一个问题,数据同步问题,我弄了下没解决.</groupinfo>

<groupinfo>第二个问题,改变适配器Adapter内容时不要在后台线程中,必须在UI线程中处理</groupinfo>

<groupinfo>我将上面子线程UpdateGroupHandler里的数据更新利用handler提取到了主线程赋值</groupinfo>

<groupinfo>Message.obj = groupList;</groupinfo>

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

<groupinfo>!发下更改的代码段:</groupinfo>

<groupinfo><textarea readonly name="code" class="java">@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");// =&gt; 取数据 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&lt;GroupInfo&gt; 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&lt;GroupInfo&gt;) 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(); // =&gt;doSomeThing... System.out.println("========数据更新后,刷新listview========="); sendMessageToUpdateUI(groupList); } }</textarea><br><br></groupinfo>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值