android中ListView数据刷新时的同步方法
本文实例讲述了android中ListView数据刷新时的同步方法。分享给大家供大家参考。具体实现方法如下:
publicclassMainextendsBaseActivity{
privatestaticfinalStringTAG="tag";
privatestaticfinalintSTATUS_CHANGE=0;
ExpandableListViewmElv;
ArrayList<GroupInfo>mGroupArray;
@Override
publicvoidonCreate(BundlesavedInstanceState){
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=newExpandableAdapter(this,Main.this);
mElv.setAdapter(mExpandableAdapter);
//异步对比服务器分组和本地分组
HandlerThreadhandlerThread=newHandlerThread("handler_thread");
handlerThread.start();
UpdateGroupHandlermyHandler=newUpdateGroupHandler(
handlerThread.getLooper());
Messagemessage=myHandler.obtainMessage();
message.sendToTarget();
mHandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
switch(msg.what){
caseSTATUS_CHANGE:
//处理UI更新等操作
updateUI();
break;
}
};
};
}
/**
*发送消息更新UI
*/
privatevoidsendMessageToUpdateUI(){
Messagemsg=newMessage();
msg.what=STATUS_CHANGE;
mHandler.sendMessage(msg);
//向Handler发送消息,更新UI
}
privatevoidupdateUI(){
//详细的更新
mExpandableAdapter.notifyDataSetChanged();
//更新ExpandableListView
}
/**
*异步刷新分组的handler
*
*@authoradministrator
*
*/
classUpdateGroupHandlerextendsHandler{
publicUpdateGroupHandler(){
}
publicUpdateGroupHandler(Looperlooper){
super(looper);
}
@Override
publicvoidhandleMessage(Messagemsg){
ContactsManagerDbAdapterdbAdapter=newContactsManagerDbAdapter(
Main.this);
dbAdapter.open();
//=>doSomeThing...
mGroupArray=groupList;
System.out.println("========数据更新后,刷新listview=========");
sendMessageToUpdateUI();
}
}
privateclassExpandableAdapterextendsBaseExpandableListAdapter{
Activityactivity;
LayoutInflaterlayoutInflater;
publicExpandableAdapter(Activitya,Contextcontext){
activity=a;
layoutInflater=(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
publicObjectgetChild(intgroupPosition,intchildPosition){
returnmGroupArray.get(groupPosition).getChildList()
.get(childPosition);
}
publiclonggetChildId(intgroupPosition,intchildPosition){
returnchildPosition;
}
publicintgetChildrenCount(intgroupPosition){
returnmGroupArray.get(groupPosition).getChildList().size();
}
publicViewgetChildView(intgroupPosition,intchildPosition,
booleanisLastChild,ViewconvertView,ViewGroupparent){
//.....
returnvi;
}
publicObjectgetGroup(intgroupPosition){
returnmGroupArray.get(groupPosition);
}
publicintgetGroupCount(){
returnmGroupArray.size();
}
publiclonggetGroupId(intgroupPosition){
returngroupPosition;
}
publicViewgetGroupView(intgroupPosition,booleanisExpanded,
ViewconvertView,ViewGroupparent){
GroupInfogroupInfo=mGroupArray.get(groupPosition);
Stringstring=groupInfo.getName();
convertView=(View)layoutInflater.inflate(R.layout.group_layout,
null);
finalTextViewtextView=(TextView)convertView
.findViewById(R.id.groupName);
if(textView!=null){
textView.setText(string);
}
returnconvertView;
}
publicbooleanhasStableIds(){
returntrue;
}
publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
returntrue;
}
}
}
代码只是提取的部分,应该没多大影响.
上面集合mGroupArray存在数据共享,测试多次发现报错有两种:
=>1.java.lang.IndexOutOfBoundsException:Invalidlocation3,sizeis3
=>2.ThecontentoftheadapterhaschangedbutListViewdidnotreceiveanotification.Makesurethecontentofyouradapterisnotmodifiedfromabackgroundthread,butonlyfromtheUIthread.
第一个问题,数据同步问题,我弄了下没解决.
第二个问题,改变适配器Adapter内容时不要在后台线程中,必须在UI线程中处理
我将上面子线程UpdateGroupHandler里的数据更新利用handler提取到了主线程赋值
Message.obj=groupList;
额,改好后测试多次,发现这两问题都解决了,发现还是对handler理解的不够.
发下更改的代码段:
@Override
publicvoidonCreate(BundlesavedInstanceState){
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=newExpandableAdapter(this,Main.this);
mElv.setAdapter(mExpandableAdapter);
//异步对比服务器分组和本地分组
HandlerThreadhandlerThread=newHandlerThread("handler_thread");
handlerThread.start();
UpdateGroupHandlermyHandler=newUpdateGroupHandler(
handlerThread.getLooper());
Messagemessage=myHandler.obtainMessage();
message.sendToTarget();
mHandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
switch(msg.what){
caseSTATUS_CHANGE:
//处理UI更新等操作
updateUI(msg.obj);
break;
}
};
};
}
/**
*发送消息更新UI
*/
privatevoidsendMessageToUpdateUI(ArrayList<GroupInfo>groupList){
Messagemsg=newMessage();
msg.what=STATUS_CHANGE;
msg.obj=groupList;
mHandler.sendMessage(msg);
//向Handler发送消息,更新UI
}
@SuppressWarnings("unchecked")
privatevoidupdateUI(Objectobj){
//详细的更新
mGroupArray=(ArrayList<GroupInfo>)obj;
mExpandableAdapter.notifyDataSetChanged();
//更新ExpandableListView
}
/**
*异步刷新分组的handler
*
*@authoradministrator
*
*/
classUpdateGroupHandlerextendsHandler{
publicUpdateGroupHandler(){
}
publicUpdateGroupHandler(Looperlooper){
super(looper);
}
@Override
publicvoidhandleMessage(Messagemsg){
ContactsManagerDbAdapterdbAdapter=newContactsManagerDbAdapter(
Main.this);
dbAdapter.open();
//=>doSomeThing...
System.out.println("========数据更新后,刷新listview=========");
sendMessageToUpdateUI(groupList);
}
}
希望本文所述对大家的Android程序设计有所帮助。