发布于 2016-03-15 06:16:00 | 84 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了android中ListView数据刷新时的同步方法,涉及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, 
    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数据刷新时的同步方法
详解Android应用中ListView列表选项栏的编写方法
关于Android中ListView嵌套GridView的问题
Android中Listview点赞功能的实现
Android中代码运行指定Apk的简单方法
Android中使用achartengine生成图表的具体方法
解析Android中使用自定义字体的实现方法
Android中WebView与Js交互的实现方法
Android中贝塞尔曲线的绘制方法示例代码
Android中new Notification创建实例的最佳方法
android中listview嵌套scrollveiw冲突解决方法
Android中DrawerLayout+ViewPager滑动冲突的解决方法
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务