android中ListView數(shù)據(jù)刷新時(shí)的同步方法
本文實(shí)例講述了android中ListView數(shù)據(jù)刷新時(shí)的同步方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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");// => 取數(shù)據(jù) mExpandableAdapter = new ExpandableAdapter(this, Main.this); mElv.setAdapter(mExpandableAdapter); // 異步對(duì)比服務(wù)器分組和本地分組 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() { // 詳細(xì)的更新 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ù)據(jù)更新后,刷新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; } } }
代碼只是提取的部分,應(yīng)該沒(méi)多大影響.
上面集合mGroupArray存在數(shù)據(jù)共享,測(cè)試多次發(fā)現(xiàn)報(bào)錯(cuò)有兩種:
=>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.
第一個(gè)問(wèn)題,數(shù)據(jù)同步問(wèn)題,我弄了下沒(méi)解決.
第二個(gè)問(wèn)題,改變適配器Adapter內(nèi)容時(shí)不要在后臺(tái)線程中,必須在UI線程中處理
我將上面子線程UpdateGroupHandler里的數(shù)據(jù)更新利用handler提取到了主線程賦值
Message.obj = groupList;
額,改好后測(cè)試多次,發(fā)現(xiàn)這兩問(wèn)題都解決了,發(fā)現(xiàn)還是對(duì)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ù)據(jù) mExpandableAdapter = new ExpandableAdapter(this, Main.this); mElv.setAdapter(mExpandableAdapter); // 異步對(duì)比服務(wù)器分組和本地分組 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<GroupInfo> 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) { // 詳細(xì)的更新 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("========數(shù)據(jù)更新后,刷新listview========="); sendMessageToUpdateUI(groupList); } }
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android異步方法以同步方式實(shí)現(xiàn)
- Android多線程之同步鎖的使用
- Android seekbar(自定義)控制音量同步更新
- android-獲取網(wǎng)絡(luò)時(shí)間、獲取特定時(shí)區(qū)時(shí)間、時(shí)間同步的方法
- Android實(shí)現(xiàn)歌曲播放時(shí)歌詞同步顯示具體思路
- Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
- Android 5.0+ 屏幕錄制實(shí)現(xiàn)的示例代碼
- Android自適應(yīng)不同屏幕大小的全部方法
- Android6.0開(kāi)發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
- Android手機(jī)屏幕同步工具asm.jar
相關(guān)文章
Android編程之手機(jī)壁紙WallPaper設(shè)置方法示例
這篇文章主要介紹了Android編程之手機(jī)壁紙WallPaper設(shè)置方法,結(jié)合實(shí)例形式分析了Android手機(jī)壁紙WallPaper的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2017-08-08Android內(nèi)存使用情況的應(yīng)用實(shí)例
這篇文章主要介紹了Android內(nèi)存使用情況的應(yīng)用實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04解析android res 運(yùn)行錯(cuò)誤的問(wèn)題
本篇文章是對(duì)android中res運(yùn)行錯(cuò)誤的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android開(kāi)發(fā)實(shí)現(xiàn)保存圖片到手機(jī)相冊(cè)功能
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)保存圖片到手機(jī)相冊(cè)功能,涉及Android圖形及文件相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
這篇文章主要介紹了Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法,本文給出了效果圖和實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自定義倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07android中打開(kāi)相機(jī)、打開(kāi)相冊(cè)進(jìn)行圖片的獲取示例
本篇文章主要介紹了android中打開(kāi)相機(jī)、打開(kāi)相冊(cè)進(jìn)行圖片的獲取示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01