ExpandableListView Order Changed When Click
<p>When I click the groupview the order of the expandablelist changes</p>

<p>I've found some one with similiar problem <strong><a href="http://stackoverflow.com/questions/5049613/android-listview-order-changed-when-called-notifydatasetchanged?s=ee5ed40e-26a6-4e00-b29f-76cf44c87606#new-answer">here</a></strong></p>

<p>The order seems random, i don't understand what happened.</p>

<p>Here's my code, I know it's a mess, I've rewritten it for a lot of times.</p>

<pre><code>public class ManageListAdapter extends BaseExpandableListAdapter{

public static final int SERVER_TYPE=0x000001;
public static final int HISTORY_TYPE=0x000002;

private List&lt;ServerElement&gt; mData;

private LayoutInflater mInflater;
private int viewType;


public ManageListAdapter (Context c,int type,ArrayList&lt;ServerElement&gt; data){
    mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mData=(List&lt;ServerElement&gt;) Collections.synchronizedList(new ArrayList&lt;ServerElement&gt;());
    mData=data;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return mData.get(groupPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder viewHolder;
    if(convertView==null){
        convertView=mInflater.inflate(R.layout.managelistchildelement, null);

        EditText IP=(EditText) convertView.findViewById(R.id.manageIP);
        IP.setText(mData.get(groupPosition).mIp);

        EditText Port=(EditText) convertView.findViewById(R.id.managePort);
        Port.setText(mData.get(groupPosition).mPort);

        EditText Username=(EditText) convertView.findViewById(R.id.manageUsername);
        Username.setText(mData.get(groupPosition).mUsername);

        EditText Password=(EditText) convertView.findViewById(R.id.managePWD);
        Password.setText(mData.get(groupPosition).mPassword);

        ImageButton btnUpdate=(ImageButton) convertView.findViewById(R.id.okBtn);

        viewHolder=new ViewHolder(IP,groupPosition);
        switch(this.viewType){
        case HISTORY_TYPE:
            IP.setFocusable(false);
            Port.setFocusable(false);
            Username.setFocusable(false);
            Password.setFocusable(false);
            btnUpdate.setVisibility(View.INVISIBLE);
            break;
        case SERVER_TYPE:
            btnUpdate.setFocusable(false);
            break;
        }
        convertView.setTag(viewHolder);
    }else{
        viewHolder=(ViewHolder) convertView.getTag();
    }
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return mData.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return mData.size();
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}

public void deleteItem(int groupPosition){
    if(mData.size()&lt;=0) return;
    int i=0;
    while (groupPosition+i+1&lt;mData.size()) {
        mData.set(groupPosition+i, mData.get(groupPosition+i+1));
        i++;
    }
    mData.remove(mData.size()-1);
    notifyDataSetChanged();
}

public void addItem(ServerElement element){

    mData.add(element);
    notifyDataSetChanged();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder viewHolder;
    if(convertView==null){
        convertView=mInflater.inflate(R.layout.managelistgroupelement, null); 
        TextView view=(TextView) convertView.findViewById(R.id.elementCaption);
        ImageView viewDelete=(ImageView) convertView.findViewById(R.id.deleteBtn);
        //viewDelete.setFocusable(false);
        view.setText(mData.get(groupPosition).mName);
        view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        view.setPadding(36, 0, 0, 0);
        if(groupPosition==0){
            Log.v("ManageListAdapter", "Still Can be 0");
        }
        viewHolder=new ViewHolder(view,groupPosition);
        viewDelete.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                deleteItem(getViewHolder(v).position);
            }

        });
        convertView.setTag(viewHolder);
    }else{
        viewHolder=(ViewHolder) convertView.getTag();
    }
    return convertView;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean areAllItemsEnabled()
{
    return true;
}


public static class ViewHolder{
    private TextView txtView;
    public final int position;

    public ViewHolder(TextView view,int position){
        this.txtView=view;
        this.position=position;
    }
}

public ViewHolder getViewHolder(View v)
{
    if(v.getTag() == null)
    {
        return getViewHolder((View)v.getParent());
    }
    return (ViewHolder)v.getTag();
}

private class OnDeleteClickListener implements OnClickListener{           
    private int mGroupPosition;
    OnDeleteClickListener(int groupPosition){
            mGroupPosition = groupPosition;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.v("ManageListAdapter","Deleting Group "+mGroupPosition);
        deleteItem(mGroupPosition);
    }               
}

private class OnUpdateClickListener implements OnClickListener{           
    private int mGroupPosition;
    private int mChildrenPosition;
    OnUpdateClickListener(int groupPosition,int childrenPosition){
            mGroupPosition = groupPosition;
            mChildrenPosition=childrenPosition;

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        addItem(mData.get(mGroupPosition));
    }               
}
</code></pre>

<p>}</p>

<p>and my layout</p>

<p>managelistchildelement.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
        &lt;TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:text="@string/Ip:port"&gt;&lt;/TextView&gt;
        &lt;EditText android:id="@+id/manageIP" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
            &lt;requestFocus&gt;&lt;/requestFocus&gt;
        &lt;/EditText&gt;
        &lt;TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:text="@string/:"&gt;&lt;/TextView&gt;
        &lt;EditText android:id="@+id/managePort" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/EditText&gt;
    &lt;/TableRow&gt;
    &lt;TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
        &lt;TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:text="@string/username"&gt;&lt;/TextView&gt;
        &lt;EditText android:id="@+id/manageUsername" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/EditText&gt;
    &lt;/TableRow&gt;
    &lt;TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
        &lt;TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:text="@string/password"&gt;&lt;/TextView&gt;
        &lt;EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/managePWD"&gt;&lt;/EditText&gt;
        &lt;ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ok" android:id="@+id/okBtn" android:focusable="false" android:focusableInTouchMode="false"&gt;&lt;/ImageButton&gt;
    &lt;/TableRow&gt;
&lt;/TableLayout&gt;
</code></pre>

<p>managelistgroupelement.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:orientation="horizontal"
    android:layout_width="wrap_content" android:baselineAligned="false"&gt;
    &lt;TextView android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_height="wrap_content" android:text="TextView"
        android:id="@+id/elementCaption" android:layout_weight="1"
        android:layout_width="0dp"&gt;&lt;/TextView&gt;
    &lt;ImageView android:src="@drawable/delete" android:id="@+id/deleteBtn"
        android:layout_gravity="right" android:layout_weight="1"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:focusable="false" android:focusableInTouchMode="false"&gt;&lt;/ImageView&gt;

&lt;/LinearLayout&gt;
</code></pre>
