作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Android 编程比较陌生,并且创建了一个包含 TextViews
和 CheckBox
的 ListView
。我已设法阻止在滚动列表时随机检查复选框。
但是,如果选中/取消选中该复选框,我想更新每行中的 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/
我正在制作一个具有录制按钮和切换按钮的应用程序。录音按钮开始录音,切换按钮声明是否必须按住录音按钮才能录音或仅按一次。这就是我试图实现它的方式: touchToRecord.setOnCheckedC
我对 Android 编程比较陌生,并且创建了一个包含 TextViews 和 CheckBox 的 ListView。我已设法阻止在滚动列表时随机检查复选框。 但是,如果选中/取消选中该复选框,我想
1)我在 Recycler View Adapter 中使用 Switch 按钮。 2)问题是“上下文”,我想在选中或取消选中切换按钮时显示 toast 。 3) 我应该在 Toast 中使用哪个 C
我是一名优秀的程序员,十分优秀!