gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:18:13 26 4
gpt4 key购买 nike

我对 Android 编程比较陌生,并且创建了一个包含 TextViewsCheckBoxListView。我已设法阻止在滚动列表时随机检查复选框。

但是,如果选中/取消选中该复选框,我想更新每行中的 TextView,但我无法让我的 OnCheckChangeListener 工作。任何帮助,将不胜感激。这是我的代码(可能有点困惑,因为我已经使用过它了):

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;

}
}

最佳答案

设法通过采取不同的方法来解决这个问题。

关于java - OnCheckChangeListener 在自定义适配器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62347951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com