2016-04-16 7 views
0

私はExpandableListViewを持っています。グループビューを1つだけ開くことができます。選択したGroupViewから文字列を取得します。 getGroupViewメソッド(groupName - var)でstringを取得しようとしていますが、別のgroupViewからのテキストが常に表示されます。私のコードで何が間違っていますか?助けてください!ここに私のアダプターがあります:ExpandableListViewで開いたgroupViewから文字列を取得する方法は?

public class ExpandListAdapter extends BaseExpandableListAdapter {  
    private Context context; 
    private ArrayList<ExpandListGroup> groups; 
    public ArrayList<String> selectedStrings = new ArrayList<String>(); 
    public String groupName; 
    final String LOG_TAG = "myLogs"; 
    public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) { 
     this.context = context; 
     this.groups = groups; 
    } 

    public void addItem(ExpandListChild item, ExpandListGroup group) { 
     if (!groups.contains(group)) { 
      groups.add(group); 
     } 
     int index = groups.indexOf(group); 
     ArrayList<ExpandListChild> ch = groups.get(index).getItems(); 
     ch.add(item); 
     groups.get(index).setItems(ch); 
    } 
    public Object getChild(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 
     return chList.get(childPosition); 
    } 

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

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) { 
     ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition); 
     if (view == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view = infalInflater.inflate(R.layout.child_view, null); 
     } 
     // TextView tv = (TextView) view.findViewById(R.id.textChild); 
     final CheckBox cb = (CheckBox) view.findViewById(R.id.textChild); 
     cb.setText(child.getName().toString()); 
     cb.setTag(child.getTag()); 
     cb.setChecked(ExpandListChild.isSelected); 
     cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        selectedStrings.add(cb.getText().toString()); 
       }else{ 
        selectedStrings.remove(cb.getText().toString()); 
       } 
      } 
     }); 

     return view; 
    } 

    public int getChildrenCount(int groupPosition) { 
     // TODO Auto-generated method stub 
     ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 

     return chList.size(); 
    } 

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

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

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

    public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) { 
     ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition); 
     if (view == null) { 
      LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      view = inf.inflate(R.layout.group_view, null); 
     } 
     TextView tv = (TextView) view.findViewById(R.id.textGroup); 
     tv.setText(group.getName()); 

//------------------------------------------------------------------- 
//In this place, Im tryimg to get string from opened GroupView 
      groupName = group.getName(); 
//------------------------------------------------------------------- 
//------------------------------------------------------------------------- 
     // TODO Auto-generated method stub 
     selectedStrings.clear(); 

     return view; 
    } 

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

    public boolean isChildSelectable(int arg0, int arg1) { 
     // TODO Auto-generated method stub 
     return true; 
    } 
} 

答えて

0

あなたはgetGroupViewでホルダーを使うべきです。リサイクラーの所有者を追加することをお勧めします。

@Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     View v = convertView; 
     ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition); 
     if (v == null) 
     { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.my_layout, parent, false); 
      ViewHolder holder = new ViewHolder(); 
      holder.textview = v.findViewById(R.id.textGroup)); 
      holder.group = group; 
      v.setTag(holder); 
     } 

     ViewHolder holder = (ViewHolder) v.getTag(); 
     // Do whatever you need to with the group view for example: 
     // groupName = holder.group.getGroupName(); 

     return v; 
} 


public class ViewHolder 
{ 
TextView textview; 
Group group; 
    public ViewHolder() 
    { 
    } 
} 
関連する問題