SoFunction
Updated on 2025-04-07

Synchronization method when ListView data refresh in android

This article describes the synchronization method when ListView data is refreshed in Android. Share it for your reference. The specific implementation method is 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) { 
  (savedInstanceState); 
  setContentView(); 
  mElv = (ExpandableListView) findViewById(.contact_list); 
  mStatus = (TextView) findViewById(); 
  mGroupArray = getIntent().getParcelableArrayListExtra("groupArray");// => Get data  mExpandableAdapter = new ExpandableAdapter(this, ); 
  (mExpandableAdapter);   
  // Asynchronously compare server grouping and local grouping  HandlerThread handlerThread = new HandlerThread("handler_thread"); 
  (); 
  UpdateGroupHandler myHandler = new UpdateGroupHandler( 
    ()); 
  Message message = (); 
  (); 
  mHandler = new Handler() { 
   public void handleMessage(Message msg) { 
    switch () { 
    case STATUS_CHANGE: 
     // Handle UI updates and other operations     updateUI(); 
     break; 
    } 
   }; 
  };  
 } 
 /**
   * Send message to update the UI
   */ 
 private void sendMessageToUpdateUI() { 
  Message msg = new Message(); 
   = STATUS_CHANGE; 
  (msg);
  // Send a message to the Handler and update the UI } 
 private void updateUI() { 
  // Detailed update  ();
  // Update ExpandableListView } 
 /**
   * Asynchronously refresh the 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( 
     ); 
   (); 
   // =>doSomeThing... 
   mGroupArray = groupList; 
   ("========After data is updated,refreshlistview========="); 
   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 (groupPosition).getChildList() 
     .get(childPosition); 
  } 
  public long getChildId(int groupPosition, int childPosition) { 
   return childPosition; 
  } 
  public int getChildrenCount(int groupPosition) { 
   return (groupPosition).getChildList().size(); 
  } 
  public View getChildView(int groupPosition, int childPosition, 
    boolean isLastChild, View convertView, ViewGroup parent) { 
   // ..... 
   return vi; 
  } 
  public Object getGroup(int groupPosition) { 
   return (groupPosition); 
  } 
  public int getGroupCount() { 
   return (); 
  } 
  public long getGroupId(int groupPosition) { 
   return groupPosition; 
  } 
  public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parent) { 
   GroupInfo groupInfo = (groupPosition); 
   String string = (); 
   convertView = (View) (.group_layout, 
     null); 
   final TextView textView = (TextView) convertView 
     .findViewById(); 
   if (textView != null) { 
    (string); 
   } 
   return convertView; 
  } 
  public boolean hasStableIds() { 
   return true; 
  } 
  public boolean isChildSelectable(int groupPosition, int childPosition) { 
   return true; 
  } 
 } 
}

The code is just the extracted part and should not have much impact.

The above collection mGroupArray has data sharing. After testing, there are two types of errors reported:

=>: Invalid location 3, size is 3
=> 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 is the data synchronization problem, I solved it but couldn't solve it.
The second problem is that when changing the Adapter content of the adapter, it must be handled in the UI thread.
I updated the data in the child thread UpdateGroupHandler above using the handler to extract the main thread assignment

 = groupList;

Well, after the correction, I tested it many times, and found that both problems were solved, but I found that I still didn’t understand the handler enough.

Post the changed code snippet:

@Override 
public void onCreate(Bundle savedInstanceState) { 
 (savedInstanceState); 
 setContentView(); 
 mElv = (ExpandableListView) findViewById(.contact_list); 
 mStatus = (TextView) findViewById(); 
 mGroupArray = getIntent().getParcelableArrayListExtra("groupArray");
 // => Get data mExpandableAdapter = new ExpandableAdapter(this, ); 
 (mExpandableAdapter);   
 // Asynchronously compare server grouping and local grouping HandlerThread handlerThread = new HandlerThread("handler_thread"); 
 (); 
 UpdateGroupHandler myHandler = new UpdateGroupHandler( 
   ()); 
 Message message = (); 
 (); 
 mHandler = new Handler() { 
  public void handleMessage(Message msg) { 
   switch () { 
   case STATUS_CHANGE: 
    // Handle UI updates and other operations    updateUI(); 
    break; 
   } 
  }; 
 };  
} 
/**
 * Send message to update the UI
 */ 
private void sendMessageToUpdateUI(ArrayList<GroupInfo> groupList) { 
 Message msg = new Message(); 
  = STATUS_CHANGE; 
  = groupList; 
 (msg);
 // Send a message to the Handler and update the UI} 
 @SuppressWarnings("unchecked") 
private void updateUI(Object obj) { 
 // Detailed update mGroupArray = (ArrayList<GroupInfo>) obj; 
 ();
 // Update ExpandableListView} 
/**
  * Asynchronously refresh the 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( 
    ); 
  (); 
  // =>doSomeThing... 
  ("========After data is updated,refreshlistview========="); 
  sendMessageToUpdateUI(groupList); 
 } 
}

I hope this article will be helpful to everyone's Android programming design.