EditText 值在子 ExpandableListView 中重复
EditText value duplicate in child ExpandableListView
当我在 EditText 中写一些东西并打开其他 group/ExpandableListView 时,随机子 EditText 与我在第一个上写的值相同,空白或 0,但 0 知道我来自 onFocusChange .我希望它们都以0开头,并且在我在EditText中写东西时不要重复,如果有人能帮助我,我将不胜感激。
这是我的适配器的 getChildView
适配器
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.questions_itens, null);
holder = new ViewHolder();
holder.editText = (EditText) convertView.findViewById(R.id.EditNumber);
holder.editText.setText(editModelArrayList.get(childPosition).getEditTextValue());
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
final ViewHolder finalHolder = holder;
holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus)
{
if (!finalHolder.editText.getText().toString().equals(""))
{
editModelArrayList.get(childPosition).setEditTextValue("0");
}else {
editModelArrayList.get(childPosition).setEditTextValue("");
}
}
}
});
holder.editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(childPosition).setEditTextValue(holder.editText.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
TextView item = (TextView) convertView.findViewById(R.id.Item);
Question question = (Question) getChild(groupPosition, childPosition);
item.setText(question.getNome());
return convertView;
}
private class ViewHolder {
protected EditText editText;
}
}
我无法修复这个错误,但我知道这是因为当我设置 EditText 时我只使用子位置,所以所有其他具有相同位置的子将获得相同的值,所以我使用了两个开关之一检查组位置与子位置的其他位置。我不知道是否有更好的方法来做到这一点,因为我不熟悉可扩展和其他东西,但它对我有用。
我是这样用Switch的
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
switch (groupPosition)
{
case 0:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
break;
case 1:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
break;
case 2:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
break;
case 3:
switch (childPosition)
{
case 0:
break;
}
break;
case 4:
switch (childPosition)
{
case 0:
break;
case 1:
break;
}
break;
}
}
当我在 EditText 中写一些东西并打开其他 group/ExpandableListView 时,随机子 EditText 与我在第一个上写的值相同,空白或 0,但 0 知道我来自 onFocusChange .我希望它们都以0开头,并且在我在EditText中写东西时不要重复,如果有人能帮助我,我将不胜感激。
这是我的适配器的 getChildView
适配器
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.questions_itens, null);
holder = new ViewHolder();
holder.editText = (EditText) convertView.findViewById(R.id.EditNumber);
holder.editText.setText(editModelArrayList.get(childPosition).getEditTextValue());
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
final ViewHolder finalHolder = holder;
holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus)
{
if (!finalHolder.editText.getText().toString().equals(""))
{
editModelArrayList.get(childPosition).setEditTextValue("0");
}else {
editModelArrayList.get(childPosition).setEditTextValue("");
}
}
}
});
holder.editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(childPosition).setEditTextValue(holder.editText.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
TextView item = (TextView) convertView.findViewById(R.id.Item);
Question question = (Question) getChild(groupPosition, childPosition);
item.setText(question.getNome());
return convertView;
}
private class ViewHolder {
protected EditText editText;
}
}
我无法修复这个错误,但我知道这是因为当我设置 EditText 时我只使用子位置,所以所有其他具有相同位置的子将获得相同的值,所以我使用了两个开关之一检查组位置与子位置的其他位置。我不知道是否有更好的方法来做到这一点,因为我不熟悉可扩展和其他东西,但它对我有用。
我是这样用Switch的
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
switch (groupPosition)
{
case 0:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
break;
case 1:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
break;
case 2:
switch (childPosition)
{
case 0:
break;
case 1:
break;
case 2:
break;
}
break;
case 3:
switch (childPosition)
{
case 0:
break;
}
break;
case 4:
switch (childPosition)
{
case 0:
break;
case 1:
break;
}
break;
}
}