2014-11-19 5 views
0

Я построю ExpandableListViewAdapter, который расширяет BaseExpandableListAdapter. дети каждой группы состоят из EditText. Кроме того, у меня есть ArrayList<String>, который содержит значения для детей.Расширяемый список Просмотр и прослушивание изменений текста

Проблема в том, что когда я хочу прослушать текстовые изменения, текст «после» изменился с использованием addTextChangedListener(watcher), мои данные не меняются, он возвращается к тому, что было.

Вот мой код:

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); 

    } 

} 

ответ

0

это нормально :) Я нашел решение ... Я просто должен был обновить mIdMap HashMap, прежде чем он будет вызван в getGroupId ... вот если кто-код нуждается в нем

@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); 
    } 

} 
Смежные вопросы