Expandablelistview: Get Input Values From Childs
I'm parsing a JSON into a ExpandableListView, on each child the user can select the amount of each child he wants to have due +/- Buttons. The +/- Buttons are connected to a TextVi
Solution 1:
Add a new class SelectedDrink like this:
publicclassSelectedDrink{
String content;
int qty;
}
Then try this adapter code:
publicclassExpandableListAdapterDrinksextendsBaseExpandableListAdapter {
private Context context;
private List<Drink> drinksList;
private Button btReset, btOk;
private List<Integer> groupSum;
private List<List<Integer>> qty;
privateinttotalSum=0;
classViewHolder {
TextView childText,counterText, childUnitPrice, childFinalPrice;
Button btn_plus,btn_minus;
}
classPos{
int group;
int child;
Pos(int group, int child){
this.group = group;
this.child = child;
}
}
publicExpandableListAdapterDrinks(Context context, List<Drink> drinksList,
Button btReset, Button btOk) {
this.context = context;
this.drinksList= drinksList;
this.btReset = btReset;
this.btOk = btOk;
groupSum = newArrayList<>();
qty = newArrayList<>();
for(int i=0; i<drinksList.size(); i++) {
groupSum.add(0);
List<Integer> orderedQty = newArrayList<>();
for(int j=0; j<drinksList.get(i).getContent().size(); j++) orderedQty.add(0);
qty.add(orderedQty);
}
}
privatevoidresetGroupSum(int groupPosition) {
totalSum -= groupSum.get(groupPosition);
groupSum.set(groupPosition, 0);
resetGroupChildrenQty(groupPosition);
}
privatevoidresetGroupChildrenQty(int groupPosition) {
for(int i=0; i<qty.get(groupPosition).size(); i++) qty.get(groupPosition).set(i, 0);
}
@OverridepublicintgetGroupCount() {
return drinksList.size();
}
@OverridepublicintgetChildrenCount(int groupPosition) {
return drinksList.get(groupPosition).getContent().size();
}
@Overridepublic Drink getGroup(int groupPosition) {
return drinksList.get(groupPosition);
}
@Overridepublic String getChild(int groupPosition, int childPosition) {
return drinksList.get(groupPosition).getContent().get(childPosition);
}
@OverridepubliclonggetGroupId(int groupPosition) {
return groupPosition;
}
@OverridepubliclonggetChildId(int groupPosition, int childPosition) {
return childPosition;
}
@OverridepublicbooleanhasStableIds() {
returnfalse;
}
@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
StringheaderTitle= drinksList.get(groupPosition).getTitle();
/** HEADER TEXT HERE */if(view==null) {
LayoutInflaterinflater= (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup,null);
}
LinearLayoutbgcolor= view.findViewById(R.id.lblListHeaderLayout);
TextViewlblListHeader= (TextView)view.findViewById(R.id.lblListHeader);
TextViewlblListHeaderPrice= (TextView)view.findViewById(R.id.lblListHeader_Price);
ButtonlblListHeaderButton= view.findViewById(R.id.lblListHeaderButton);
if(groupSum.get(groupPosition) > 0){
lblListHeaderPrice.setVisibility(View.VISIBLE);
lblListHeaderPrice.setText(String.format("%.02f", (float)groupSum.get(groupPosition)).replace(".", ",") + "€");
}else{
lblListHeaderPrice.setVisibility(View.GONE);
lblListHeaderButton.setVisibility(View.GONE);
}
if(!drinksList.get(groupPosition).getBg().get(0).isEmpty() || !drinksList.get(groupPosition).getBg().get(1).isEmpty() ) {
List<String> colorGradientTopBottom = drinksList.get(groupPosition).getBg();
int[] colors = {Color.parseColor(colorGradientTopBottom.get(0)),Color.parseColor(colorGradientTopBottom.get(1))};
GradientDrawablegd=newGradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(0f);
bgcolor.setBackground(gd);
} else {
//bgcolor.setBackgroundColor(ContextCompat.getColor(context, R.color.cardview_bg));
}
lblListHeader.setText(headerTitle);
return view;
}
@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
/** Drinks List */
ViewHolder viewHolder;
StringchildDrink= getChild(groupPosition, childPosition);
intchildPrice= getGroup(groupPosition).getPricelist().get(childPosition);
if (view == null) {
LayoutInflaterinflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_drinks_listitem, null);
viewHolder = newViewHolder();
viewHolder.childText = view.findViewById(R.id.lblListItemDrinks);
viewHolder.childUnitPrice = view.findViewById(R.id.lblListItemDrinksUnitPrice);
viewHolder.counterText = view.findViewById(R.id.vip_drinks_amount);
viewHolder.childFinalPrice = view.findViewById(R.id.lblListItemDrinksFinalPrice);
viewHolder.btn_plus = view.findViewById(R.id.vip_drinks_btn_plus);
viewHolder.btn_minus = view.findViewById(R.id.vip_drinks_btn_minus);
viewHolder.btn_plus.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Pospos= (Pos) v.getTag();
intorderedQty= qty.get(pos.group).get(pos.child);
orderedQty++;
qty.get((pos.group)).set(pos.child, orderedQty);
groupSum.set(pos.group, groupSum.get(pos.group) + getGroup(pos.group).getPricelist().get(pos.child));
notifyDataSetChanged();
totalSum += getGroup(pos.group).getPricelist().get(pos.child);
btOk.setText(String.format("%.02f", (float)totalSum).replace(".", ",") + "€");
btReset.setEnabled(true);
}
});
viewHolder.btn_minus.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Pospos= (Pos) v.getTag();
intorderedQty= qty.get(pos.group).get(pos.child);
if (orderedQty > 0) {
orderedQty--;
qty.get((pos.group)).set(pos.child, orderedQty);
groupSum.set(pos.group, groupSum.get(pos.group) - getGroup(pos.group).getPricelist().get(pos.child));
notifyDataSetChanged();
totalSum -= getGroup(pos.group).getPricelist().get(pos.child);
if (totalSum == 0) resetTotalSum();
else btOk.setText(String.format("%.02f", (float)totalSum).replace(".", ",") + "€");
}
}
});
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.childText.setText(childDrink);
viewHolder.childUnitPrice.setText(String.format("%.02f", (float)childPrice).replace(".", ",") + "€");
intorderedQty= qty.get(groupPosition).get(childPosition);
viewHolder.counterText.setText(String.valueOf(orderedQty));
viewHolder.childFinalPrice.setText(String.format("%.02f", (float)orderedQty*childPrice).replace(".", ",") + "€");
viewHolder.btn_minus.setTag(newPos(groupPosition, childPosition));
viewHolder.btn_plus.setTag(newPos(groupPosition, childPosition));
view.setTag(viewHolder);
return view;
}
@OverridepublicbooleanisChildSelectable(int i, int i1) {
returnfalse;
}
publicvoidresetTotalSum() {
for(int i=0; i<drinksList.size(); i++) {
resetGroupSum(i);
}
notifyDataSetChanged();
btReset.setEnabled(false);
btOk.setText(String.valueOf(0));
}
public ArrayList<SelectedDrink> getOrderList() {
ArrayList<SelectedDrink> orderList = newArrayList<>();
for(int i=0; i<drinksList.size(); i++) {
for(int j=0; j<drinksList.get(i).getContent().size(); j++) {
if(qty.get(i).get(j) > 0) {
SelectedDrinkselectedDrink=newSelectedDrink();
selectedDrink.content = getGroup(i).getContent().get(j);
selectedDrink.qty = qty.get(i).get(j);
orderList.add(selectedDrink);
}
}
}
return orderList;
}
}
I test the above with this activity:
publicclassMainActivityextendsAppCompatActivity {
finalprivatestaticintNUM_OF_GROUP=5;
List<Drink> drinksList;
ExpandableListView listView;
ExpandableListAdapterDrinks expandableListAdapterDrinks;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonbtRest= findViewById(R.id.btReset);
ButtonbtOk= findViewById(R.id.btOK);
listView = findViewById(R.id.elvDrinks);
initDataList();
expandableListAdapterDrinks =
newExpandableListAdapterDrinks(getApplicationContext(), drinksList, btRest, btOk);
listView.setAdapter(expandableListAdapterDrinks);
listView.expandGroup(0);
btRest.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
expandableListAdapterDrinks.resetTotalSum();
for(int i=0; i<drinksList.size(); i++) listView.collapseGroup(i);
listView.expandGroup(0);
}
});
btOk.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
Stringmsg="Nothing Selected";
Buttonbutton= (Button)view;
if(!button.getText().equals("0")) {
msg = "Upload!\n";
ArrayList<SelectedDrink> selectedDrinks = expandableListAdapterDrinks.getOrderList();
for(SelectedDrink selectedDrink: selectedDrinks) {
msg += selectedDrink.content + " " + selectedDrink.qty + "\n";
}
msg += "Total: " + button.getText();
}
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
privatevoidinitDataList(){
drinksList = newArrayList<>();
List<String> content;
List<Integer> pricelist;
List<String> bg;
for(int i=1; i<=NUM_OF_GROUP; i++) {
content = newArrayList<>();
pricelist = newArrayList<>();
bg = newArrayList<>();
for(intj=1; j<(NUM_OF_GROUP + newRandom().nextInt(5)); j++){
content.add("Drink " + i + "-" + j);
pricelist.add(newRandom().nextInt(1000));
bg.add("#008577");
bg.add("#D81B60");
}
Drinkdrink=newDrink();
drink.setTitle("Group " + i);
drink.setContent(content);
drink.setPricelist(pricelist);
drink.setBg(bg);
drinksList.add(drink);
}
}
}
and this layout:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:id="@+id/llBtns"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="horizontal"><Buttonandroid:id="@+id/btReset"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:enabled="false"android:text="Reset" /><Buttonandroid:id="@+id/btOK"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="0.5"android:text="0" /></LinearLayout><ExpandableListViewandroid:id="@+id/elvDrinks"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/llBtns"></ExpandableListView></RelativeLayout>
Also, your Drink class, vip_package_listgroup.xml and vip_drinks_listitem.xml are also needed. Hope that helps!
Post a Comment for "Expandablelistview: Get Input Values From Childs"