安卓 : How iterate through views of a expandableList

标签 android expandablelistview expandablelistadapter

我有一个 expandableList,在子元素中我添加了一些 EditText、复选框和微调器。这些已正确创建和显示。选中复选框后会显示其他 EditText 和微调器,然后用户可以添加文本并从微调器中选择值。我想为每个子元素检索这些附加 View (edittext、Spinner、CheckBox)的值。单击保存时,我想遍历所有元素并检索这些 editText、Spinner 和 checkBox 中的值。目前我正在使用下面的代码,但是在点击保存之后,它总是返回与最后一个子元素关联的值。如果需要有关此问题的更多信息,请告诉我。 ExpandableList ScreenShot

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>

点击保存后,这就是我得到的-

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_

最佳答案

添加新类 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;
}
 }

主要 Activity

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

点击获取单位、数量和备注

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

关于安卓 : How iterate through views of a expandableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039959/

相关文章:

android - Android 上的选项卡

android - 应用程序无法使用 Android ICS 连接到网络

android - 自定义 View 未使用 ExpandableListView 上的 OnGroupClickListener 进行更新

java - 您可以将可扩展列表放入可扩展列表中吗

android - 如何将 Qualcoomm Vuforia 与基于位置的标记结合使用

Android ExpandableListView 和 onChildClick 不工作

java - Android ExpandableListView 填充了两次

android - 如何保持 ExpandableListView 打开?

android - 将选中的项目 move 到可展开的 ListView 中

android - 导入gradle项目,android报错