私の展開可能リストビューでは、それぞれの子ビューにEditTextがあり、各テキスト更新時にモデルを更新する必要があるため、各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;
}
}
ですこれはSubamountのEditTextのためTextWatcherせずに私のコンソール出力です:
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
注意と私のコンソール出力です:どのEditTextのデータもUIから変更していません。
TextWatchersを実装する方法が間違っているのですか、またはアダプタに問題がありますか? TextWatcherを対応するグループ&の子の位置についてのみ更新することは明らかですが、それらは混同しています。
もう1つの観測では、私はconvertViewが 'getChildView'内のすべてのchildViewsのビューを返すと思います。 TextChangedListenerをconvertviewで表示するように設定しているので、問題を引き起こす可能性がありますか?