当前位置: 首页 > 面试题库 >

滚动ExpendableListView后计数器的值更改

晋功
2023-03-14
问题内容

我有一个ExpandableListView项目,在清单项目上有TextView,它带有两个按钮来增加或减少点击时TextView中的值。每当我尝试滚动列表时,都会出现此问题。如果我增加一项,然后滚动列表,则值会混合(因为ListView不断回收其视图),而且我不知道如何解决它。

我已经尝试了许多在这里找到的失灵,所以是的,这可能是重复的,但是我无法用发现的任何方法解决我的问题。

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


 类似资料:
  • 如何设置没有背景色的导航栏? 当在div之后向下滚动导航条时,导航条会得到一个新的背景颜色(导航条应该固定在顶部,我在引导中使用) 我尝试过一些教程,但没有成功。 这是网站:http://attafothman.olympe.in/ 我说的是上面那个黑色导航条。

  • 我正在使用Redis streams构建排队功能。我想防止坏消息阻塞队列,所以我只想在丢弃它们之前尝试N次。 我使用的模式: 使用者首先尝试从挂起列表中自动声明旧消息,如果不存在旧消息,则使用XREADGROUP从队列中拾取消息。然后它对处理过的消息进行xack。 XAUTOCLAIM的文档中提到了一个“重试计数器”,它听起来非常适合我的用例。但是我不知道如何读取消息的重试计数器。

  • 问题内容: 这个问题已经有了SQL的答案,并且我能够使用R在R中实现该解决方案。但是,我一直找不到使用来实现它的方法。 问题是要计算滚动日期范围内一列的不同值,例如(如果直接从链接的问题中引用)数据是否如下所示: 如果我们使用3天的日期范围,则结果集将类似于以下内容 这是使用R在R中创建相同数据的代码: 在这方面的任何帮助将不胜感激。谢谢! 编辑1: 这是一个玩具问题,我想将其应用于更大的数据集,

  • 我的应用程序的MainActivity从用户那里获取一些数据来计算他/她的BMI和BMR,并将这些数据用于应用程序的主要部分,即另一个名为DailyActivity的活动。我希望这样,用户第一次启动应用程序时,它会要求提供数据。但在第一次之后,每次用户启动应用程序时,都应使用之前输入的数据直接启动DailyActivity。(按下后退按钮也不应显示MainActivity)。我如何实现这一点?

  • 问题内容: 我有一组电子邮件地址和将这些电子邮件地址添加到表中的日期的数据集。电子邮件地址在不同的日期可以有多个条目。例如,如果我有下面的数据集。我希望获得上述日期和3天前之间不同电子邮件的日期和计数。 如果我们使用3的日期周期,结果集将看起来像这样 我可以使用下面的查询来获得日期范围的不同计数,但希望按天获得一个范围的计数,因此我不必手动更新数百个日期的范围。 感谢您的帮助。 问题答案: 测试用

  • 问题内容: 我正在寻找一种类似的各种功能的方法,但我希望滚动计算的窗口由一个值范围(例如,DataFrame列的值范围)定义,而不是由窗口中的行数。 例如,假设我有以下数据: 如果执行类似的操作,则会得到一个滚动总和,其中每个窗口包含5行。但是我想要的是一个滚动总和,其中每个窗口都包含的一定范围的值。也就是说,我希望能够执行类似的操作,并得到一个结果,其中第一个窗口包含所有介于1和5之间的行,然后