我正在尝试创建一个listview
,每行包含checkbox
和edittext
。我已使用edittext
在checkbox
已检查事件中启用notifyDatasetChanged()
。
但是它会重置位置,这使得难以跟踪当前位置。我已经创建了一个实例变量来跟踪checkbox
已检查事件的当前位置,但是它也会重置到最后检查的项目。
checkbox
工作正常。它在选中时启用edittext
并在取消选中时禁用,也检查状态是否正常。但问题是,edittext
如果我使用listposition
,则只会获得上次检查的值;如果我使用last item
则会position
。
我的适配器如下:
public class ListViewAdapter extends ArrayAdapter<Model> implements TextWatcher{
private final List<Model> list;
private final Context context;
private int listPosition;
private ViewHolder newHolder = null;
public ListViewAdapter(Context context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
public static class ViewHolder {
public CheckBox mCheckBox;
public EditText mEditText;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(context);
convertView = inflator.inflate(R.layout.row, null);
newHolder = new ViewHolder();
newHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
newHolder.mEditText = (EditText) convertView.findViewById(R.id.txtValue);
newHolder.mCheckBox.setText(list.get(position).getCheckText());
newHolder.mEditText.setText(list.get(position).getValue());
setEdit(false);
convertView.setTag(newHolder);
convertView.setTag(R.id.checkbox, newHolder.mCheckBox);
convertView.setTag(R.id.txtValue, newHolder.mEditText);
} else {
newHolder = (ViewHolder) convertView.getTag();
}
newHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int getPosition = (int) compoundButton.getTag();
list.get(getPosition).setSelected(compoundButton.isChecked());
list.get(getPosition).setEnabled(compoundButton.isChecked());
setEdit(compoundButton.isChecked());
listPosition = getPosition;
if(!compoundButton.isChecked()){
list.get(getPosition).setValue("");
}
notifyDataSetChanged();
}
});
newHolder.mCheckBox.setTag(position);
newHolder.mEditText.setTag(position);
newHolder.mEditText.addTextChangedListener(this);
setEdit(list.get(position).isEnabled());
return convertView;
}
public void setEdit(boolean bool){
newHolder.mEditText.setEnabled(bool);
newHolder.mEditText.setFocusableInTouchMode(bool);
newHolder.mEditText.setCursorVisible(bool);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable s) {
list.get(listPosition).setValue(s.toString());
}
}
答案 0 :(得分:0)
您应该使用arraylist来存储已检查的位置,而不是存储到单个整数中。在adpater中定义temp arraylist并创建Hashmap以存储在特定edittext上输入的值
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(context);
convertView = inflator.inflate(R.layout.row, null);
newHolder = new ViewHolder();
newHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
newHolder.mEditText = (EditText) convertView.findViewById(R.id.txtValue);
newHolder.mCheckBox.setText(list.get(position).getCheckText());
newHolder.mEditText.setText(list.get(position).getValue());
setEdit(false);
convertView.setTag(newHolder);
convertView.setTag(R.id.checkbox, newHolder.mCheckBox);
convertView.setTag(R.id.txtValue, newHolder.mEditText);
} else {
newHolder = (ViewHolder) convertView.getTag();
}
newHolder.mCheckBox.setTag(position);
newHolder.mEditText.setTag(position);
//newHolder.mEditText.addTextChangedListener(this); Remove this
setEdit(list.get(position).isEnabled());
//Added Change here...Check if arraylist contains selectedposition or not?
if(selectedPosition.contains(String.valueOf(position))){
list.get(getPosition).setSelected(compoundButton.isChecked());
list.get(getPosition).setEnabled(compoundButton.isChecked());
setEdit(compoundButton.isChecked());
}else{
setEdit(false);
list.get(getPosition).setValue("");
}
//Same way compare for edittext
if(editHash.containsKey(Integer.parseInt(position))){
newHolder.mEditText.setText(editHash.get(position));
}else{
newHolder.mEditText.setText("");
}
newHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int position = (int) compoundButton.getTag();
//Simply store and check selectedPosition
if(!b)
selectedPosition.remove(String.valueOf(position));
else
selectedPosition.add(String.valueOf(position));
//And then update adapter
notifyDataSetChanged();
}
});
//Let's add Textwatcher for our Edittext
newHolder.mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int position = (int) newHolder.mEditText.getTag();
//Storing into Hashmap
editHash.put(position, s.toString());
}
});
return convertView;
}
现在按照以下内容更新你的getView()方法:
We = inv.log_10[(log_10(W_to) - A)/B]