Synchronization method for ListView data refresh in android

  • 2020-06-19 11:42:35
  • OfStack

This article illustrates the synchronization method for ListView data refresh in android. Share to everybody for everybody reference. Specific implementation methods are as follows:


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");// =>  Take the data  
  mExpandableAdapter = new ExpandableAdapter(this, Main.this); 
  mElv.setAdapter(mExpandableAdapter);   
  //  Asynchronously compare server grouping with local grouping  
  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: 
     //  To deal with UI Update and so on  
     updateUI(); 
     break; 
    } 
   }; 
  };  
 } 
 /** 
  *  Send message updates UI 
  */ 
 private void sendMessageToUpdateUI() { 
  Message msg = new Message(); 
  msg.what = STATUS_CHANGE; 
  mHandler.sendMessage(msg);
  //  to Handler Send a message , update UI 
 } 
 private void updateUI() { 
  //  Detailed updates  
  mExpandableAdapter.notifyDataSetChanged();
  //  update ExpandableListView 
 } 
 /** 
  *  Asynchronous refresh grouped 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("======== After data update , The refresh 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; 
  } 
 } 
}

The code is just the extracted part, so it shouldn't matter much.

There is data sharing in the above collection mGroupArray, and there are two types of error reporting found in the test for multiple times:

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

The first problem, the data synchronization problem, I have not solved.
Second, do not change the adapter Adapter content in the background thread; it must be handled in the UI thread
I used handler to extract the mainline assignment from the data update in UpdateGroupHandler


Message.obj = groupList;

Well, after many tests, I found that both problems were solved, but I still didn't understand handler well enough.

Post the changed code snippet:


@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");
 // =>  Take the data  
 mExpandableAdapter = new ExpandableAdapter(this, Main.this); 
 mElv.setAdapter(mExpandableAdapter);   
 //  Asynchronously compare server grouping with local grouping  
 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: 
    //  To deal with UI Update and so on  
    updateUI(msg.obj); 
    break; 
   } 
  }; 
 };  
} 
/** 
*  Send message updates UI 
*/ 
private void sendMessageToUpdateUI(ArrayList<GroupInfo> groupList) { 
 Message msg = new Message(); 
 msg.what = STATUS_CHANGE; 
 msg.obj = groupList; 
 mHandler.sendMessage(msg);
 //  to Handler Send a message , update UI 
} 
 @SuppressWarnings("unchecked") 
private void updateUI(Object obj) { 
 //  Detailed updates  
 mGroupArray = (ArrayList<GroupInfo>) obj; 
 mExpandableAdapter.notifyDataSetChanged();
 //  update ExpandableListView 
} 
/** 
 *  Asynchronous refresh grouped 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("======== After data update , The refresh listview========="); 
  sendMessageToUpdateUI(groupList); 
 } 
}

I hope this article has been helpful for your Android programming.


Related articles: