滚动 ExpendableListView 后计数器的值发生变化 [英] Values of counter changes after scrolling ExpendableListView

查看:30
本文介绍了滚动 ExpendableListView 后计数器的值发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ExpandableListView 项,在列表项上我有 TextView 和两个按钮,用于在单击时增加或减少 TextView 中的值.每次我尝试滚动列表时都会出现问题.如果我增加一项然后滚动列表,这些值会混合(因为 ListView 不断回收其视图),我不知道如何修复它.

I have an ExpandableListView of items and on list item I have TextView with two buttons to increment or decrements the value in TextView on clicks. The problem occurs every time I try to scroll the list. If I increment one item and then scroll the list the values get mixed (becouse the ListView keeps recycling its views) and I don't know how to fix it.

我在这里找到了很多解决方案,所以是的,这可能是重复的,但我无法用我找到的任何方法解决我的问题.

I have tried many soulutions I have found here, so yes this may be a duplicate, but I wasn't able to solve my problem with any method I have found.

我的 ExpandableListAdapter.java

My ExpandableListAdapter.java

    import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;


public class ExpandableListAdapter extends BaseExpandableListAdapter {

public static class ViewHolder {
    TextView childText;
    TextView counterText;
    Button addItemButton;
    Button deleteItemButton;

    int quantity = 0;

}


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

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
}

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

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

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return listHashMap.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 = (String) 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) {

    final 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;

        convertView.setTag(viewHolder);


    } else {
        viewHolder = (ViewHolder) convertView.getTag();

    }

    final String childText = (String) getChild(groupPosition, childPosition);
    viewHolder.childText.setText(childText);

    viewHolder.addItemButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            //int itemCount = Integer.parseInt((String)viewHolder.counterText.getText());
            //itemCount++;

            viewHolder.quantity++;
            viewHolder.counterText.setText( Integer.toString(viewHolder.quantity));

            PutOrderDrinks.addOrder(childText);

        }
    });


    return convertView;
}

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

推荐答案

试试这个适配器:

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

这篇关于滚动 ExpendableListView 后计数器的值发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆