2016-10-14 81 views
0

我有一個expandableList,在子元素中我添加了一些EditText,複選框和微調。這些被創建並正確顯示。一旦勾選了複選框,就會顯示附加的EditText和Spinner,然後用戶可以添加文本並從微調器中選擇值。我想爲每個子元素檢索這些附加視圖(edittext,Spinner,CheckBox)的值。在保存點擊時,我想遍歷所有元素並在這些editText,Spinner和checkBox中檢索值。目前我正在使用下面的代碼,但是在點擊保存後,它總是返回與最後一個子元素關聯的值。請讓我知道是否需要更多信息關於這個問題。 ExpandableList ScreenShotAndroid:如何遍歷一個expandableList的視圖

MainActivity-

public class MainActivity extends AppCompatActivity { 
ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<String>> listDataChild; 

Button save; 
Button cancel; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

save = (Button) findViewById(R.id.save); 
// get the listview 
expListView = (ExpandableListView) findViewById(R.id.lvExp); 

// preparing list data 
prepareListData(); 

listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild); 

// setting list adapter 
expListView.setAdapter(listAdapter); 

expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 

save.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
     System.out.println("Save clicked"); 
     for(int i = 0; i < listAdapter.getGroupCount();i++) { 

      for (int k = 0; k < listAdapter.getChildrenCount(i); k++) { 

       String temp = listAdapter.recollect(i,k); 
       System.out.println("For i="+i+" and k="+k+" string value is="+temp); 
      } 
     } 
    } 
}); 



} 



/* 
* Preparing the list data 
*/ 
private void prepareListData() { 
listDataHeader = new ArrayList<String>(); 
listDataChild = new HashMap<String, List<String>>(); 
// Adding child data 
listDataHeader.add("Nokia"); 
listDataHeader.add("Apple"); 
listDataHeader.add("Samsung"); 

// Adding child data 
List<String> nokia = new ArrayList<String>(); 
nokia.add("Nokia1"); 
nokia.add("Nokia2"); 


List<String> apple = new ArrayList<String>(); 
apple.add("Apple1"); 
apple.add("Apple2"); 


List<String> samsung = new ArrayList<String>(); 
samsung.add("Samsung1"); 
samsung.add("Samsung2"); 

listDataChild.put(listDataHeader.get(0), nokia); // Header, Child data 
listDataChild.put(listDataHeader.get(1), apple); 
listDataChild.put(listDataHeader.get(2), samsung); 
} 
} 

MainLayout-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="dhritiapps.temp.MainActivity"> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="60dp" 
android:id="@+id/lin" 
android:orientation="horizontal"> 
<Button 
android:layout_width="0dp" 
android:layout_height="50dp" 
android:layout_weight="1" 
android:id="@+id/can" 
android:text="Cancel"/> 
<Button 
android:layout_width="0dp" 
android:layout_height="50dp" 
android:layout_weight="1" 
android:id="@+id/save" 
android:text="Save"/> 
</LinearLayout> 
<ExpandableListView 
android:layout_marginTop="10dp" 
android:id="@+id/lvExp" 
android:layout_below="@+id/lin" 
android:layout_height="match_parent" 
android:layout_width="match_parent"/> 
</RelativeLayout> 

ExpandableListAdapter-

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<String>> _listDataChild; 
// private final HashMap<String, String, String> mCheckedItems; 
String cat, item, quty, units, notes; 
EditText qty; 
EditText note; 
Spinner unit; 
ArrayList <String> ss=new ArrayList<>(); 

public ExpandableListAdapter(Context context, List<String> listDataHeader, 
HashMap<String, List<String>> listChildData) { 
this._context = context; 
this._listDataHeader = listDataHeader; 
this._listDataChild = listChildData; 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
.get(childPosititon); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
return childPosition; 
} 

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

final String childText = (String) getChild(groupPosition, childPosition); 

if (convertView == null) { 
LayoutInflater infalInflater = (LayoutInflater) this._context 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
convertView = infalInflater.inflate(R.layout.list_item, null); 
} 

TextView txtListChild = (TextView) convertView 
.findViewById(R.id.lblListItem); 

final LinearLayout mm = (LinearLayout) convertView.findViewById(R.id.entries); 
qty = (EditText) convertView.findViewById(R.id.qty); 
note = (EditText) convertView.findViewById(R.id.note); 
unit = (Spinner) convertView.findViewById(R.id.unit); 
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb); 

final int grp = groupPosition; 

cb.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 

final CheckBox cb = (CheckBox) v; 

if (cb.isChecked()) { 
mm.setVisibility(View.VISIBLE); 
} else { 
mm.setVisibility(View.GONE); 

} 
System.out.println(ss); 
} 
}); 

txtListChild.setText(childText); 

return convertView; 
} 

public String recollect(int grp, int ch) { 
cat = getGroup(grp).toString(); 
item = getChild(grp, ch).toString(); 
quty = qty.getText().toString(); 
units = unit.getSelectedItem().toString(); 
notes = note.getText().toString(); 

System.out.println("cat="+cat+"; item="+item+"; qty="+quty+"; unit="+units+"; note="+notes); 
return cat+";"+item+";"+quty+";"+units+";"+notes+"_"; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
.size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
return this._listDataHeader.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
View convertView, ViewGroup parent) { 
String headerTitle = (String) getGroup(groupPosition); 
if (convertView == null) { 
LayoutInflater infalInflater = (LayoutInflater) this._context 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
convertView = infalInflater.inflate(R.layout.list_group, null); 
} 
TextView lblListHeader = (TextView) convertView 
.findViewById(R.id.lblListHeader); 

lblListHeader.setTypeface(null, Typeface.BOLD); 
lblListHeader.setText(headerTitle); 

return convertView; 
} 

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

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


} 

list_Group.xml-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="8dp" 
android:background="#000000">  

<TextView 
android:id="@+id/lblListHeader" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="2dp" 
android:textSize="17dp" 
android:textColor="#f9f93d" /> 

</LinearLayout> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="55dip" 
android:orientation="vertical" 
android:background="#ef8d8d"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:paddingLeft="5dp" 
android:layout_weight="1" 
> 
<TextView 
android:id="@+id/lblListItem" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="17dip" 
android:paddingTop="5dp" 
android:paddingBottom="5dp" 
android:textColor="#ff0000" 
android:layout_weight="5"/> 
<CheckBox 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/cb" 
android:checked="false" 
android:layout_weight="1"/> 
</LinearLayout> 


<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="40dp" 
android:id="@+id/entries" 
android:orientation="horizontal" 
android:paddingLeft="8dp" 
android:visibility="gone" 
android:paddingBottom="2dp" 
android:layout_weight="1"> 
<EditText 
android:id="@+id/qty" 
android:layout_width="0dp" 
android:layout_height="30dp" 
android:hint="Qunatity" 
android:layout_weight="1" 
android:background="#12f212" 
android:layout_margin="3dp" 
android:textColor="#700a55" 
android:inputType="numberDecimal" 
android:maxLength="8" 
android:textSize="12dip"/> 
<Spinner 
android:id="@+id/unit" 
android:layout_width="0dp" 
android:layout_height="30dp" 
android:layout_weight="1" 
android:entries="@array/units" 
android:background="#12f212" 
android:layout_margin="3dp" 
android:textColor="#700a55" 
/> 
<EditText 
android:id="@+id/note" 
android:layout_width="0dp" 
android:layout_height="30dp" 
android:hint="Note" 
android:layout_weight="4" 
android:background="#12f212" 
android:layout_margin="3dp" 
android:textColor="#700a55" 
android:textSize="12dip" 
android:maxLength="50"/> 
</LinearLayout> 

</LinearLayout> 

點擊保存後,這是我getting-

System.out: For i=0 and k=0 string value is=Nokia;Nokia1;2;C;Note for Samsung2_ 
System.out: For i=0 and k=1 string value is=Nokia;Nokia2;2;C;Note for Samsung2_ 
System.out: For i=1 and k=0 string value is=Apple;Apple1;2;C;Note for Samsung2_ 
System.out: For i=1 and k=1 string value is=Apple;Apple2;2;C;Note for Samsung2_ 
System.out: For i=2 and k=0 string value is=Samsung;Samsung1;2;C;Note for Samsung2_ 
System.out: For i=2 and k=1 string value is=Samsung;Samsung2;2;C;Note for Samsung2_ 

雖然它應該是像這個 -

System.out: For i=0 and k=0 string value is=Nokia;Nokia1;123;A;Note for Note for Nokia1 
System.out: For i=0 and k=1 string value is=Nokia;Nokia2;;; 
System.out: For i=1 and k=0 string value is=Apple;Apple1;;; 
System.out: For i=1 and k=1 string value is=Apple;Apple2;;; 
System.out: For i=2 and k=0 string value is=Samsung;Samsung1;;; 
System.out: For i=2 and k=1 string value is=Samsung;Samsung2;2;C;Note for Samsung2_ 
+0

分享@ array/units或不管它是什麼?如你分享 –

+0

無法獲取您的視圖 一個 ç

+0

陣列單元具有僅3個值作爲A,B C. <字符串數組名= 「單位」> –

回答

1

添加新的Class CustomChilds

package stack.buy.com.stackdemos; 

/** 
* Created by Desktop - Ganesh on 10/14/2016. 
*/ 
public class CustomChilds { 
String Quntity; 
String Unit; 
String Note; 
String GroupName; 

public String getQuntity() { 
    return Quntity; 
} 

public void setQuntity(String quntity) { 
    Quntity = quntity; 
} 

public String getUnit() { 
    return Unit; 
} 

public void setUnit(String unit) { 
    Unit = unit; 
} 

public String getNote() { 
    return Note; 
} 

public void setNote(String note) { 
    Note = note; 
} 

public String getGroupName() { 
    return GroupName; 
} 

public void setGroupName(String groupName) { 
    GroupName = groupName; 
} 

public CustomChilds(String quntity, String unit, String note, String groupName) { 
    Quntity = quntity; 
    Unit = unit; 
    Note = note; 
    GroupName = groupName; 
} 

public CustomChilds() { 
} 
} 

適配器:

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<CustomChilds>> _listDataChild; 
// private final HashMap<String, String, String> mCheckedItems; 
String cat, item, quty, units, notes; 

ArrayList<String> ss = new ArrayList<>(); 

public ExpandableListAdapter(Context context, List<String> listDataHeader, 
          HashMap<String, List<CustomChilds>> listChildData) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
} 
    @Override 
    public CustomChilds getChild(int groupPosition, int childPosititon){ 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId (int groupPosition, int childPosition){ 
     return childPosition; 
    } 

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

     final CustomChilds childText =getChild(groupPosition, childPosition); 
     final ViewHolder holder; 

     View row = convertView; 
     if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.list_item, parent, false); 
      holder = new ViewHolder(); 
      holder.mm = (LinearLayout) row.findViewById(R.id.entries); 
      holder.qty = (EditText) row.findViewById(R.id.qty1); 
      holder.note = (EditText) row.findViewById(R.id.note); 
      holder.unit = (Spinner) row.findViewById(R.id.unit); 
      holder.cb = (CheckBox) row.findViewById(R.id.cb); 
      holder.txtListChild = (TextView) row 
        .findViewById(R.id.lblListItem); 
      row.setTag(holder); 
     } 

     else { 
      // view already exists, get the holder instance from the view 
      holder = (ViewHolder) row.getTag(); 
     } 

     List<CustomChilds> temp = _listDataChild.get(this._listDataHeader.get(groupPosition)); 
     temp.set(childPosition,new CustomChilds(holder.qty.getText().toString(),holder.unit.getSelectedItem().toString(),holder.note.getText().toString(),childText.GroupName)); 

     final int grp = groupPosition; 

     holder.cb.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 

       final CheckBox cb = (CheckBox) v; 

       if (cb.isChecked()) { 
        holder.mm.setVisibility(View.VISIBLE); 
       } else { 
        holder.mm.setVisibility(View.GONE); 

       } 
       System.out.println(ss); 
      } 
     }); 

     holder.txtListChild.setText(childText.getGroupName()); 

     return row; 
    } 



@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this._listDataHeader.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 
    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 

    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

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

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



// somewhere else in your class definition 
static class ViewHolder { 
    EditText qty; 
    EditText note; 
    Spinner unit; 
    LinearLayout mm; 
    CheckBox cb; 
    TextView txtListChild; 
} 
} 

MainActivity

public class MainActivity extends AppCompatActivity { 

ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<CustomChilds>> listDataChild; 

Button save; 
Button cancel; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    save = (Button) findViewById(R.id.save); 


    listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild); 



    expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 

    save.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      System.out.println("Save clicked"); 
      for(int i = 0; i < listAdapter.getGroupCount();i++) { 
       for (int k = 0; k < listAdapter.getChildrenCount(i); k++) { 


        CustomChilds child = listAdapter.getChild(i, k); 
        String unit=child.getUnit(); 
        String note=child.getNote(); 
        String qut=child.getQuntity(); 

        System.out.println("For i="+i+" and k="+k+" string value is="+child.getNote()); 
       } 
      } 
     } 
    }); 



} 



private void prepareListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, List<CustomChilds>>(); 
    listDataHeader.add("Nokia"); 
    listDataHeader.add("Apple"); 
    listDataHeader.add("Samsung"); 

    List<CustomChilds> nokia = new ArrayList<CustomChilds>(); 
    nokia.add(new CustomChilds("","","","Nokia1")); 
    nokia.add(new CustomChilds("","","","Nokia2")); 


    List<CustomChilds> apple = new ArrayList<CustomChilds>(); 
    apple.add(new CustomChilds("","","","Apple1")); 
    apple.add(new CustomChilds("","","","Apple2")); 



    List<CustomChilds> samsung = new ArrayList<CustomChilds>(); 
    samsung.add(new CustomChilds("","","","Samsung1")); 
    samsung.add(new CustomChilds("","","","Samsung2")); 

    listDataChild.put(listDataHeader.get(0), nokia); // Header, Child data 
    listDataChild.put(listDataHeader.get(1), apple); 
    listDataChild.put(listDataHeader.get(2), samsung); 
} 
} 

獲取單位,數量和注意的onClick

save.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
     System.out.println("Save clicked"); 
     for(int i = 0; i < listAdapter.getGroupCount();i++) { 
      for (int k = 0; k < listAdapter.getChildrenCount(i); k++) { 


       CustomChilds child = listAdapter.getChild(i, k); 
       String unit=child.getUnit(); 
       String note=child.getNote(); 
       String qut=child.getQuntity(); 

       System.out.println("For i="+i+" and k="+k+" string value is="+child.getNote()); 
      } 
     } 
    } 
}); 
+0

這是最終的代碼.. –

+0

非常感謝@Ganesh Pokale。它的工作現在 –

0
您使用

相同的編輯文本,所以它只會給你最後的值,而不是在編輯文本和onTextChanged()方法中添加一個textchange監聽器,編輯這樣的列表中的文本: 還要添加一個帶有位置的標籤並使用標籤來檢索列表中的值。

myEditText1.addTextChangedListener(new GenericTextWatcher(myEditText1));

myEditText1。setTag( 「theFirstEditTextAtPos:」 +位);

private class GenericTextWatcher implements TextWatcher{ 

     private View view; 
     private GenericTextWatcher(View view) { 
      this.view = view; 
     } 

     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

     public void afterTextChanged(Editable editable) { 

      String text = editable.toString(); 
      //save the value for the given tag : 
      MyResultAdapter.this.textValues.put(view.getTag(), editable.toString()); 
     } 
    } 

    //you can implement a method like this one for each EditText with the list position as parameter : 
    public String getValueFromFirstEditText(int position){ 
      //here you need to recreate the id for the first editText 
     String result = textValues.get("theFirstEditTextAtPos:"+position); 
     if(result ==null) 
       result = "default value"; 

     return result; 
    } 


} 
0

試試這個

save.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      System.out.println("Save clicked"); 
      for(int i = 0; i < listAdapter.getGroupCount();i++) { 
       String group = listAdapter.getGroup(i).toString(); 
       for (int k = 0; k < listAdapter.getChildrenCount(i); k++) { 


        String child = listAdapter.getChild(i, k).toString();; 

        System.out.println("For i="+i+" and k="+k+" string value is="+child); 
       } 
      } 
     } 
    }); 

堆棧跟蹤:

10-14 16:19:38.877 20206-20206/stack.buy.com.stackdemos I/System.out: For 
i=0 and k=0 string value is=Nokia1 
10-14 16:19:41.395 20206-20206/stack.buy.com.stackdemos I/System.out: For 
i=0 and k=1 string value is=Nokia2 
10-14 16:19:45.015 20206-20206/stack.buy.com.stackdemos I/System.out: For i 
i=1 and k=0 string value is=Apple1 
10-14 16:19:47.663 20206-20206/stack.buy.com.stackdemos I/System.out: For 
i=1 and k=1 string value is=Apple2 
10-14 16:19:50.968 20206-20206/stack.buy.com.stackdemos I/System.out: For 
i=2 and k=0 string value is=Samsung1 
10-14 16:19:51.957 20206-20206/stack.buy.com.stackdemos I/System.out: For 
i=2 and k=1 string value is=Samsung2 

也爲自定義視圖ViewHolder策略是好

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private List<String> _listDataHeader; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<String>> _listDataChild; 
// private final HashMap<String, String, String> mCheckedItems; 
String cat, item, quty, units, notes; 

ArrayList<String> ss = new ArrayList<>(); 

public ExpandableListAdapter(Context context, List<String> listDataHeader, 
          HashMap<String, List<String>> listChildData) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
} 
    @Override 
    public Object getChild(int groupPosition, int childPosititon){ 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId (int groupPosition, int childPosition){ 
     return childPosition; 
    } 

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

     final String childText = (String) getChild(groupPosition, childPosition); 
     final ViewHolder holder; 

     View row = convertView; 
     if (row == null) { 
      LayoutInflater inflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.list_item, parent, false); 
      holder = new ViewHolder(); 
      holder.mm = (LinearLayout) row.findViewById(R.id.entries); 
      holder.qty = (EditText) row.findViewById(R.id.qty1); 
      holder.note = (EditText) row.findViewById(R.id.note); 
      holder.unit = (Spinner) row.findViewById(R.id.unit); 
      holder.cb = (CheckBox) row.findViewById(R.id.cb); 
      holder.txtListChild = (TextView) row 
        .findViewById(R.id.lblListItem); 
      row.setTag(holder); 
     } 

     else { 
      // view already exists, get the holder instance from the view 
      holder = (ViewHolder) row.getTag(); 
     } 



     final int grp = groupPosition; 

     holder.cb.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 

       final CheckBox cb = (CheckBox) v; 

       if (cb.isChecked()) { 
        holder.mm.setVisibility(View.VISIBLE); 
       } else { 
        holder.mm.setVisibility(View.GONE); 

       } 
       System.out.println(ss); 
      } 
     }); 

     holder.txtListChild.setText(childText); 

     return row; 
    } 

    /* public String recollect(int grp, int ch) { 
    cat = getGroup(grp).toString(); 
    item = getChild(grp, ch).toString(); 
    quty = qty.getText().toString(); 
    units = unit.getSelectedItem().toString(); 
     notes = note.getText().toString(); 

     System.out.println("cat=" + cat + "; item=" + item + "; qty=" + quty +       "; unit=" + units + "; note=" + notes); 
    return cat + ";" + item + ";" + quty + ";" + units + ";" + notes + "_"; 
}*/ 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this._listDataHeader.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 
    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 

    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

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

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



// somewhere else in your class definition 
static class ViewHolder { 
    EditText qty; 
    EditText note; 
    Spinner unit; 
    LinearLayout mm; 
    CheckBox cb; 
    TextView txtListChild; 
} 
} 

一樣,onChildClick如果你想當前視圖,然後喲ü可以像這樣訪問:

EditText note= (EditText) v.findViewById(R.id.note); 
String value = tv.getText().toString(); 
+0

感謝您的詳細回覆。可能是我沒有正確地得到它。如何得到holder.qty,holder.note,holder.unit,holder.cb的值。 holder.txtListChild的值,我能夠正確得到。 –