带按钮的 ListView,在 OnClick 中使用 ViewHolder

ListView with Button, Using ViewHolder within OnClick

我正在尝试创建与文本配对的按钮列表。每当单击列表中的按钮时,我想增加关联文本视图中的整数值。我的计划是使用以下代码,但我在 OnClickListener 中使用 viewHolder 时遇到错误,因为 viewHolder 不是最终的,并且不能是为了操纵它onClick 内。如何修改关联的文本值?

public View getView(final int position, View convertView, ViewGroup parent){
    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
        viewHolder.button = (Button) convertView.findViewById(R.id.playerButton);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            protected String scoreToSet;
            @Override
            public void onClick(View v) {
                 viewHolder.text.setText(Integer.toString(modelList.get(position).getScore() + 1)); //Error is occurring here
            }
        });
        convertView.setTag(viewHolder);
    }
    else{
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.button.setText(modelList.get(position).getPlayer());
    }
    return convertView;
}

感谢您的帮助!

您应该在 if-else 块之外的视图中添加侦听器或设置值 -

    public View getView(final int position, View convertView, ViewGroup parent){
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
            viewHolder.button = (Button)  convertView.findViewById(R.id.playerButton);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();

        }
         viewHolder.button.setText(modelList.get(position).getPlayer());

            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                protected String scoreToSet;
                @Override
                public void onClick(View v) {
                     setValue(position);
                }
            });
        return convertView;
    }

声明setValue()-

public void setValue(int position){
    ViewHolder.text.setText(Integer.toString(modelList.get(position).getScore() + 1));
}

像这样更改代码,

public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder ;
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.playerScore);
            viewHolder.button = (Button) convertView.findViewById(R.id.playerButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.button.setText(modelList.get(position).getPlayer());
        }

        viewHolder.button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //TODO UPDATE the model here 
                int newScore = modelList.get(position).getScore() + 1;
                modelList.get(position).setScore(newScore);
                viewHolder.text.setText(Integer.toString(newScore); 
            }
        });

        return convertView;
    }