編輯:關於Android編程
本文實例講述了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程序設計有所幫助。
消息推送方案(輪詢、長連接) 輪詢 輪詢:比較簡單的,最容易理解和實現的就是客戶端去服務器上拉信息,信息的及時性要求越高則拉信息的頻率越高。客戶端拉信息的觸發可以是一些事
什麼是AIDL以及如何使用 ①aidl是Android interface definition Language 的英文縮寫,意思Android 接口定義語言。 ②使用
實現方式 實現的方式有很多種 這裡總結最常見的幾種方式,以後再添加其他的。viewPager + RadioGroup viewPager + FragmentTabHo
最近公司做了一款OTP令牌激活的產品,由於之前激活手機令牌需要輸入很多的激活信息才能進行激活。經過一段使用後,發現易用性不是很強,考慮如果加入二維碼的的掃碼功能豈不是大大