Expandable List View and listening to text changes

283 Views Asked by At

I build a ExpandableListViewAdapter that extends BaseExpandableListAdapter. the children of each group are made of EditText. Aslo, I there is an ArrayList<String> that holds the values for the children.

The problem is that when I want to listen to text changes "after" text has changed using addTextChangedListener(watcher), the my data doesn't change, it goes back to what it was.

Here is my code:

public class StableArrayAdapter extends BaseExpandableListAdapter {

    final int INVALID_ID = -1;
    private Context mContext;
    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
    ArrayList<String> mLocations;
    View.OnTouchListener mTouchListener;
    //OnItemLongClickListener mItemLongClickListener;

    public StableArrayAdapter(Context context, ArrayList<String> objects, OnTouchListener mTouchListener) {
        super();
        mContext = context;
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
        mLocations = objects;
        this.mTouchListener = mTouchListener;
        //this.mItemLongClickListener = mItemLongClickListener;
    }

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

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

    @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 mIdMap.get(groupPosition);
    }

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

    @Override
    public long getGroupId(int groupPosition) {
        if (groupPosition < 0 || groupPosition >= mIdMap.size()) {
            return INVALID_ID;
        }
        String item = mLocations.get(groupPosition);
        return mIdMap.get(item);
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) mLocations.get(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_list, null);
        }

        //convertView.setOnTouchListener(mTouchListener);
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final int groupPos = groupPosition;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.text_view, null);
        }

        EditText txtListChild = (EditText) convertView
                .findViewById(R.id.textView_header);

        txtListChild.addTextChangedListener(new TextWatcher() {
            String completeText = "";
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                completeText = s.toString();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                mLocations.set(groupPos, completeText);

            }
        });

        String childText = (String) mLocations.get(groupPosition);
        txtListChild.setText(childText);
        return convertView;
    }

    public void remove(long groupId) {
        mIdMap.remove(mLocations.get((int) groupId));
        mLocations.remove((int) groupId);

    }

}
1

There are 1 best solutions below

0
On

it's ok :) I found a solution ... I just had to update my mIdMap hashmap before it gets called in getGroupId ... here is a code if anyone needs it

@Override
public long getGroupId(int groupPosition) {
    if (groupPosition < 0 || groupPosition >= mIdMap.size()) {
        return INVALID_ID;
    }
    String item = mLocations.get(groupPosition);
    if(item == null){
        updatemIdMap(mLocations);
        item = mLocations.get(groupPosition);
        Log.d("Adapter-Inside","item = " + item);
    }
    Log.d("Adapter-Outside","item = " + item);
    if(mIdMap.get(item) == null){
        updatemIdMap(mLocations);
    }
    return mIdMap.get(item);
}

private void updatemIdMap(ArrayList<String> objects) {
    for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
    }

}