Values of Counter Changes After Scrolling Expendablelistview

Values of counter changes after scrolling ExpendableListView

Try this adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

class ViewHolder {
TextView childText;
TextView counterText;
Button addItemButton;
Button deleteItemButton;
}

class ChildItem{
String name;
int quantity;
ChildItem(String name, int quantity){
this.name = name;
this.quantity = quantity;
}
}

class Pos{
int group;
int child;
Pos(int group, int child){
this.group = group;
this.child = child;
}
}

private Context context;
private List<String> listDataHeader;
//private HashMap<String, List<String>> listHashMap;
private HashMap<String, List<ChildItem>> listChildMap;

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
listChildMap = new HashMap<>();
for(int i=0; i<getGroupCount(); i++){
List<ChildItem> listTemp = new ArrayList<>();
for(int j=0; j<listHashMap.get(listDataHeader.get(i)).size(); j++){
listTemp.add(new ChildItem(listHashMap.get(listDataHeader.get(i)).get(j), 0));
}
listChildMap.put(listDataHeader.get(i), listTemp);
}
}

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

@Override
public int getChildrenCount(int groupPosition) {
return listChildMap.get(listDataHeader.get(groupPosition)).size();
}

@Override
public String getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}

@Override
public ChildItem getChild(int groupPosition, int childPosition) {
return listChildMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = getGroup(groupPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
TextView listHeader = (TextView) convertView.findViewById(R.id.list_header);
listHeader.setTypeface(null, Typeface.BOLD);
listHeader.setText(headerTitle);
return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
TextView textListChild = (TextView) convertView.findViewById(R.id.list_item_header);
TextView itemsCounter = (TextView) convertView.findViewById(R.id.items_counter);
Button addItemButton = (Button) convertView.findViewById(R.id.plus_btn);
viewHolder = new ViewHolder();
viewHolder.childText = textListChild;
viewHolder.counterText = itemsCounter;
viewHolder.addItemButton = addItemButton;
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ChildItem child = getChild(groupPosition, childPosition);
viewHolder.childText.setText(child.name);
viewHolder.counterText.setText("" + child.quantity);
viewHolder.addItemButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Pos pos = (Pos)v.getTag();
ChildItem selectedItem = getChild(pos.group, pos.child);
selectedItem.quantity = selectedItem.quantity + 1;
notifyDataSetChanged();

PutOrderDrinks.addOrder(selectedItem.name);
}
});
viewHolder.addItemButton.setTag(new Pos(groupPosition, childPosition));
convertView.setTag(viewHolder);
return convertView;
}

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

When scrolling expandable list view textview values change

hello i heaved use this adapter for expandable list view
use this:

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 List<Home_property> data;
int[] images_tag;

public ExpandableListAdapter(Context context, List<Home_property> category,
int[] image) {
this._context = context;
images_tag = image;
data = category;

}

@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(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

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);
txtListChild.setTypeface(Fontfamily.getFont(_context));
txtListChild
.setText(data.get(groupPosition).subcats.get(childPosition).subcatnam);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).subcats.size();

}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
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);
ImageView header = (ImageView) convertView.findViewById(R.id.header);
lblListHeader.setTypeface(Fontfamily.getFont(_context));
try {
lblListHeader.setText(data.get(groupPosition).catnam);
} catch (IndexOutOfBoundsException e) {

}

header.setBackgroundResource(images_tag[groupPosition]);
return convertView;
}

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

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

Expandable ListView Child return wrong view after scroll down

You have this result because of android recycling mechanism.

To solve this, just change this code:

if(groupPosition==0){
container.setVisibility(View.VISIBLE);
txtListChild.setVisibility(View.GONE);
txtUser.setText(opini.getName());
txtOpini.setText(opini.getContent());
}else{
txtListChild.setText(opini.getText());
txtListChild.setTypeface(null, Typeface.BOLD);
}

to this:

if(groupPosition==0){
container.setVisibility(View.VISIBLE);
txtListChild.setVisibility(View.GONE);
txtUser.setText(opini.getName());
txtOpini.setText(opini.getContent());
}else{
container.setVisibility(View.GONE); // changes here
txtListChild.setVisibility(View.VISIBLE);// changes here
txtListChild.setText(opini.getText());
txtListChild.setTypeface(null, Typeface.BOLD);
}

Android Expandable listview on scroll it changes selected checkbox state

Maybe its too late for this answer, but I think that this is simpler solution for this. Just try create new view every time (remove checking if view == null before that).

Instead of:

View view = convertView;
ViewHolder holder = new ViewHolder();
if(view == null){
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.home_activity_lv_sync_adapter, null);
holder.ckupdate = (CheckBox) view.findViewById(R.id.ck_update);
view.setTag(holder);
} else {
holder = (RecyclerView.ViewHolder) view.getTag();
}

Try with this:

View view;
ViewHolder holder = new ViewHolder();
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =infalInflater.inflate(R.layout.home_activity_lv_sync_adapter,null);
holder.ckupdate =(CheckBox)view.findViewById(R.id.ck_update);
view.setTag(holder);

Also, there is no need for using holder for this parent view. You can use convertView instead.

ExpandableListView: Get input values from childs

Add a new class SelectedDrink like this:

public class SelectedDrink {
String content;
int qty;
}

Then try this adapter code:

public class ExpandableListAdapterDrinks extends BaseExpandableListAdapter {

private Context context;
private List<Drink> drinksList;

private Button btReset, btOk;
private List<Integer> groupSum;
private List<List<Integer>> qty;
private int totalSum = 0;

class ViewHolder {
TextView childText,counterText, childUnitPrice, childFinalPrice;
Button btn_plus,btn_minus;
}

class Pos{
int group;
int child;
Pos(int group, int child){
this.group = group;
this.child = child;
}
}

public ExpandableListAdapterDrinks(Context context, List<Drink> drinksList,
Button btReset, Button btOk) {
this.context = context;
this.drinksList= drinksList;
this.btReset = btReset;
this.btOk = btOk;

groupSum = new ArrayList<>();
qty = new ArrayList<>();
for(int i=0; i<drinksList.size(); i++) {
groupSum.add(0);
List<Integer> orderedQty = new ArrayList<>();
for(int j=0; j<drinksList.get(i).getContent().size(); j++) orderedQty.add(0);
qty.add(orderedQty);
}
}

private void resetGroupSum(int groupPosition) {
totalSum -= groupSum.get(groupPosition);
groupSum.set(groupPosition, 0);
resetGroupChildrenQty(groupPosition);
}

private void resetGroupChildrenQty(int groupPosition) {
for(int i=0; i<qty.get(groupPosition).size(); i++) qty.get(groupPosition).set(i, 0);
}

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

@Override
public int getChildrenCount(int groupPosition) {
return drinksList.get(groupPosition).getContent().size();
}

@Override
public Drink getGroup(int groupPosition) {
return drinksList.get(groupPosition);
}

@Override
public String getChild(int groupPosition, int childPosition) {
return drinksList.get(groupPosition).getContent().get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
String headerTitle = drinksList.get(groupPosition).getTitle();

/** HEADER TEXT HERE */
if(view==null) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup,null);
}
LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
TextView lblListHeaderPrice = (TextView)view.findViewById(R.id.lblListHeader_Price);
Button lblListHeaderButton = 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))};
GradientDrawable gd = new GradientDrawable(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;
}

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

/** Drinks List */
ViewHolder viewHolder;

String childDrink = getChild(groupPosition, childPosition);
int childPrice = getGroup(groupPosition).getPricelist().get(childPosition);

if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_drinks_listitem, null);

viewHolder = new ViewHolder();
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(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pos pos = (Pos) v.getTag();
int orderedQty = 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pos pos = (Pos) v.getTag();
int orderedQty = 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(".", ",") + "€");
int orderedQty = 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(new Pos(groupPosition, childPosition));
viewHolder.btn_plus.setTag(new Pos(groupPosition, childPosition));
view.setTag(viewHolder);
return view;
}

@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}

public void resetTotalSum() {
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 = new ArrayList<>();
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) {
SelectedDrink selectedDrink = new SelectedDrink();
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:

public class MainActivity extends AppCompatActivity {
final private static int NUM_OF_GROUP = 5;
List<Drink> drinksList;
ExpandableListView listView;
ExpandableListAdapterDrinks expandableListAdapterDrinks;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btRest = findViewById(R.id.btReset);
Button btOk = findViewById(R.id.btOK);
listView = findViewById(R.id.elvDrinks);

initDataList();
expandableListAdapterDrinks =
new ExpandableListAdapterDrinks(getApplicationContext(), drinksList, btRest, btOk);
listView.setAdapter(expandableListAdapterDrinks);
listView.expandGroup(0);

btRest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
expandableListAdapterDrinks.resetTotalSum();
for(int i=0; i<drinksList.size(); i++) listView.collapseGroup(i);
listView.expandGroup(0);
}
});
btOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = "Nothing Selected";
Button button = (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();
}
});
}

private void initDataList(){
drinksList = new ArrayList<>();
List<String> content;
List<Integer> pricelist;
List<String> bg;
for(int i=1; i<=NUM_OF_GROUP; i++) {
content = new ArrayList<>();
pricelist = new ArrayList<>();
bg = new ArrayList<>();
for(int j = 1; j<(NUM_OF_GROUP + new Random().nextInt(5)); j++){
content.add("Drink " + i + "-" + j);
pricelist.add(new Random().nextInt(1000));
bg.add("#008577");
bg.add("#D81B60");
}
Drink drink = new Drink();
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"?>
<RelativeLayout xmlns: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">

<LinearLayout
android:id="@+id/llBtns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">

<Button
android:id="@+id/btReset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:text="Reset" />

<Button
android:id="@+id/btOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="0" />
</LinearLayout>

<ExpandableListView
android: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!

ListView changes records while scrolling

Android ListView recycles views. At the moment the problem is that your adapter always appends new data to the TextViews. Now every time a new row is shown, you are appending the data from the new row to the old data.

To solve this, you should clear the TextViews from the old data if you are using the convertView parameter.

Duplicates and re-positioning of 'children' in expandable listview on scroll

The problem is because of object reuse. the object is already created so we should set the new value. if you didn't set it will show the old values.

You did correctly in Group view, but in child view, you did wrong. Set the value all the time, then it will work fine

Try this,

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
ChildRow childRow = (ChildRow) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_list_item, null);

}

ImageView childIcon = (ImageView) convertView.findViewById(R.id.ivExp);
childIcon.setImageResource(childRow.getIcon());

final TextView childTitleText = (TextView) convertView
.findViewById(R.id.childTitleText);

final TextView childAmountText = (TextView) convertView
.findViewById(R.id.childAmountText);

childTitleText.setText(childRow.getTitle().trim());
childAmountText.setText(childRow.getAmount()+"");

// final View finalConvertView =convertView;

return convertView;
}


Related Topics



Leave a reply



Submit