在我的可扩展列表视图中,我在每个子视图中都有EditText,我必须在每次文本更新时更新模型,所以我尝试将textwatchers添加到每个EditText。但是当与TextWatchers一起使用时,我的模型在组和组中输入的值不正确。孩子的职位。
这是我的适配器代码
package dularish.splitspends;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import java.util.List;
/**
* Created by pc on 11/3/2017.
*/
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public List<CardViewModel> models;
public Context ctx;
public ExpandableListView listviewref;
public ExpandableListAdapter(List<CardViewModel> models, Context ctx, ExpandableListView expandableListView) {
this.models = models;
this.ctx = ctx;
this.listviewref = expandableListView;
}
@Override
public int getGroupCount() {
return models.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return models.get(groupPosition).getInnerModelsData().size();
}
@Override
public Object getGroup(int groupPosition) {
return models.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return models.get(groupPosition).getInnerModelsData().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String Name = models.get(groupPosition).getName();
String Amount = models.get(groupPosition).getAmount().toString();
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_cardview,null);
}
EditText namebox = (EditText) convertView.findViewById(R.id.namebox);
EditText amountbox = (EditText) convertView.findViewById(R.id.amountbox);
ImageButton dropdownbutton = (ImageButton) convertView.findViewById(R.id.dropdownbutton);
namebox.setText(Name);
amountbox.setText(Amount);
//listviewref.expandGroup(groupPosition);//This comment should remind you that on every refresh this method would be executed
dropdownbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listviewref.isGroupExpanded(groupPosition)){
System.out.println("Group Collapsed");
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(ctx, R.anim.imagebutton_rotate_back);
hyperspaceJumpAnimation.setFillAfter(true);
v.startAnimation(hyperspaceJumpAnimation);
listviewref.collapseGroup(groupPosition);
}
else{
System.out.println("Group expanded");
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(ctx, R.anim.imagebutton_rotate);
hyperspaceJumpAnimation.setFillAfter(true);
v.startAnimation(hyperspaceJumpAnimation);
listviewref.expandGroup(groupPosition);
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String SubAmount = models.get(groupPosition).getInnerModelsData().get(childPosition).getInnerAmount().toString();
String Purpose = models.get(groupPosition).getInnerModelsData().get(childPosition).getPurpose();
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_cardview,null);
}
EditText subamountbox = (EditText) convertView.findViewById(R.id.subamountbox);
EditText purposebox = (EditText) convertView.findViewById(R.id.purposebox);
ImageButton addchildbutton = (ImageButton) convertView.findViewById(R.id.inneraddbutton);
ImageButton deletechildbutton = (ImageButton) convertView.findViewById(R.id.innerdeletebutton);
subamountbox.setText(SubAmount);
purposebox.setText(Purpose);
System.out.println("Child set for GroupPosition - " + groupPosition + " ChildPosition - " + childPosition + " Amt " + SubAmount + " Purpose " + Purpose);
addchildbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("AddChild button clicked");
models.get(groupPosition).addInnerModelsData(new InnerCardViewModel(60,"DefaultAddChild"));
notifyDataSetChanged();
}
});
subamountbox.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) {
System.out.println("TextChanged for GroupPosition - " + groupPosition + " ChildPosition - " + childPosition + " Amt " + s.toString());
models.get(groupPosition).getInnerModelsData().get(childPosition).setInnerAmount(Integer.parseInt(String.valueOf(s)));
//notifyDataSetChanged();
}
});/*
purposebox.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) {
models.get(groupPosition).getInnerModelsData().get(childPosition).setPurpose(s.toString());
}
});
*/
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的控制台输出没有TextWatcher的Subamount EditText:
11-04 08:19:52.138 30714-30714/dularish.splitspends I/System.out: Group expanded
11-04 08:19:52.154 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 1500 Purpose Default
11-04 08:19:52.157 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:19:52.166 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 1500 Purpose Default
11-04 08:19:52.174 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:19:55.548 30714-30714/dularish.splitspends I/System.out: AddChild button clicked
11-04 08:19:55.561 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 1500 Purpose Default
11-04 08:19:55.563 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:19:55.565 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 2 Amt 60 Purpose DefaultAddChild
11-04 08:19:55.572 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 1500 Purpose Default
11-04 08:19:55.574 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:19:55.575 30714-30714/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 2 Amt 60 Purpose DefaultAddChild
这是我的TextWatchers控制台输出
11-04 08:16:17.860 27184-27184/dularish.splitspends I/System.out: Group expanded
11-04 08:16:17.885 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 1500 Purpose Default
11-04 08:16:17.888 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 50
11-04 08:16:17.889 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:16:17.897 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 50
11-04 08:16:17.897 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 1 Amt 50
11-04 08:16:17.897 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 50 Purpose Default
11-04 08:16:17.906 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:16:37.699 27184-27184/dularish.splitspends I/System.out: AddChild button clicked
11-04 08:16:37.717 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 50 Purpose Default
11-04 08:16:37.719 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 50
11-04 08:16:37.720 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 50 Purpose Trial2
11-04 08:16:37.722 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 60
11-04 08:16:37.722 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 1 Amt 60
11-04 08:16:37.722 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 2 Amt 60 Purpose DefaultAddChild
11-04 08:16:37.731 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 60
11-04 08:16:37.731 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 1 Amt 60
11-04 08:16:37.731 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 60
11-04 08:16:37.732 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 0 Amt 60 Purpose Default
11-04 08:16:37.733 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 1 Amt 60
11-04 08:16:37.734 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 1 Amt 60 Purpose Trial2
11-04 08:16:37.735 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 0 Amt 60
11-04 08:16:37.735 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 1 Amt 60
11-04 08:16:37.735 27184-27184/dularish.splitspends I/System.out: TextChanged for GroupPosition - 0 ChildPosition - 2 Amt 60
11-04 08:16:37.736 27184-27184/dularish.splitspends I/System.out: Child set for GroupPosition - 0 ChildPosition - 2 Amt 60 Purpose DefaultAddChild
注意:我没有更改UI中任何EditText的数据。
我实现TextWatchers的方法是错误的,还是适配器有问题?虽然我明确提到只为相应的组&amp;更新TextWatcher。孩子的位置,他们正在混淆。
还有一个观察结果,我认为'getChildView'中的convertView会返回所有childViews的视图。由于我将设置TextChangedListener以在转换视图中查看,是否会导致问题?
答案 0 :(得分:0)
将一个布尔类型的数组列表绑定到您的数据默认设置所有布尔值为false当用户选择视图时将位置设置为true,这样您就可以检测到位置然后根据其位置工作。
此外,您可以使用XML集编辑文本标记中的标记(如-1)以及绑定活动中的数据 添加布尔值,当用户点击或选择位置获取位置时,如下所示:
Integer Pos = Integer.parseInt(holder.edittext.getTag().toString());
并根据所选位置工作,然后除选定之外的所有其他视图将不起作用。
另请查看此示例:
Select checkbox is duplicated when scrolling on an ExpandableListView