简体   繁体   English

OnCheckChangeListener 在自定义适配器中不起作用

[英]OnCheckChangeListener not working in Custom Adapter

I'm relatively new to Android programming and have created a ListView containing TextViews and a CheckBox .我对 Android 编程比较陌生,并创建了一个包含TextViewsCheckBoxListView I've managed to stop the check boxes from randomly checking when scrolling through the list.滚动列表时,我设法阻止复选框随机检查。

However I want to update a TextView in each row if the checkbox is selected / deselected however I cannot get my OnCheckChangeListener to work.但是,如果选中/取消选中复选框,我想在每一行中更新TextView但是我无法让我的OnCheckChangeListener工作。 Any help would be appreciated.任何帮助,将不胜感激。 Here is my code (it may be slightly messy as I've played around with it a bit):这是我的代码(它可能有点乱,因为我已经玩了一点):

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

public class CheckTestAdapter extends BaseAdapter {
    private Context mContext;
    private Context context;
    private final ArrayList<Model> listData;
    private LayoutInflater layoutInflater;
    private Model listModel;
    View rowView;
    ItemHolder holder;
    public CheckTestAdapter(Context context, ArrayList<Model> listData) {
        this.context = context;
        this.listData = listData;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        ;
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return listData.get(position).getId();
    }



    @Override
    public View getView(int position, View view, ViewGroup parent) {
        rowView = view;
        holder = null;
        if (rowView == null) {
            rowView = layoutInflater.inflate(R.layout.check_items, null);
        }
        holder = new ItemHolder();
        holder.moviesName = (TextView) rowView.findViewById(R.id.itemName);
        holder.checkTest = rowView.findViewById(R.id.itemChecked);
        holder.checkBox = (CheckBox) rowView.findViewById(R.id.chkItem);
        holder.itemID = rowView.findViewById(R.id.itemIdHolder);
        listModel = listData.get(position);
        final String test = String.valueOf(listModel.getId());
        holder.itemID.setText(test);
        holder.moviesName.setText(listModel.getItemName());
        holder.checkTest.setText(listModel.getCheckedItem());
        holder.checkBox.setChecked(listModel.itemChecked());
        // Tag is important to get position clicked checkbox
        holder.checkBox.setTag(position);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String checkVal = new String();
                boolean chChecked = holder.checkBox.isChecked();
                if (chChecked) {
                    // Run update query
                    checkVal = "Work";
                } else if (!chChecked) {
                    checkVal = "Stuff";
                }
                holder.checkTest.setText(checkVal);
            }
        });
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentPos = (int) v.getTag();
                boolean isChecked = false;
                if (listData.get(currentPos).itemChecked() == false) {
                    isChecked = true;
                }
                listData.get(currentPos).setIsChecked(isChecked);
                notifyDataSetChanged();
            }
        });
        return rowView;
    }
    static class ItemHolder {
        TextView moviesName;
        TextView checkTest;
        CheckBox checkBox;
        TextView itemID;

    }
}

Managed to work this out by taking a different approach.设法通过采取不同的方法来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM