ListView with Button,在OnClick中使用ViewHolder

时间:2016-04-06 21:47:26

标签: android listview android-viewholder

我正在尝试创建与文本配对的按钮列表。只要单击列表中的按钮,我想在关联的文本视图中增加整数值。我的计划是使用以下代码,但我在viewHolder内使用OnClickListener时出错,因为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;
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您应该在if-else blocks -

之外的视图中添加侦听器或设置值
    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));
}

答案 1 :(得分:0)

更改此代码,

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