日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/13 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android listview 数据同步,android中ListView数据刷新时的同步方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例講述了android中ListView數(shù)據刷新時的同步方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

public class Main extends BaseActivity {

private static final String TAG = "tag";

private static final int STATUS_CHANGE = 0;

ExpandableListView mElv;

ArrayList 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");// => 取數(shù)據

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;

}

};

};

}

/**

* 發(fā)送消息更新UI

*/

private void sendMessageToUpdateUI() {

Message msg = new Message();

msg.what = STATUS_CHANGE;

mHandler.sendMessage(msg);

// 向Handler發(fā)送消息,更新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("========數(shù)據更新后,刷新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存在數(shù)據共享,測試多次發(fā)現(xiàn)報錯有兩種:

=>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.

第一個問題,數(shù)據同步問題,我弄了下沒解決.

第二個問題,改變適配器Adapter內容時不要在后臺線程中,必須在UI線程中處理

我將上面子線程UpdateGroupHandler里的數(shù)據更新利用handler提取到了主線程賦值

Message.obj = groupList;

額,改好后測試多次,發(fā)現(xiàn)這兩問題都解決了,發(fā)現(xiàn)還是對handler理解的不夠.

發(fā)下更改的代碼段:

@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");

// => 取數(shù)據

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;

}

};

};

}

/**

* 發(fā)送消息更新UI

*/

private void sendMessageToUpdateUI(ArrayList groupList) {

Message msg = new Message();

msg.what = STATUS_CHANGE;

msg.obj = groupList;

mHandler.sendMessage(msg);

// 向Handler發(fā)送消息,更新UI

}

@SuppressWarnings("unchecked")

private void updateUI(Object obj) {

// 詳細的更新

mGroupArray = (ArrayList) 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("========數(shù)據更新后,刷新listview=========");

sendMessageToUpdateUI(groupList);

}

}

希望本文所述對大家的Android程序設計有所幫助。

總結

以上是生活随笔為你收集整理的android listview 数据同步,android中ListView数据刷新时的同步方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。