2016-04-13 10 views
10

子供の中にedittextがあります。次に、edittextの中にテキストを入力しましたが、他のexpandablelistviewヘッダーをクリックするたびに、入力したテキストは消えます。ExpandableListView内のEditTextコンテンツは、次のExpandableListViewヘッダーをクリックすると消えます。

私のManifest.xmlと他の多くの可能な修正の中で既にandroid:windowSoftInputMode="adjustPan"を使用しましたが、うまくいきませんでした。

これは私のアダプタです:

public class ExpendableAdapter extends BaseExpandableListAdapter { 
    private Context _context; 
    private List<String> _listDataHeader; 
    private HashMap<String, List<ExpandItemModel>> _listDataChild; 

public ExpendableAdapter(Context context, List<String> listDataHeader, 
          HashMap<String, List<ExpandItemModel>> listChildData) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .get(childPosititon); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(final int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

    final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 

    final EditText etTotal = (EditText)convertView 
      .findViewById(R.id.et_total); 

    etTotal.setText(childText.getTotal); 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return this._listDataHeader.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 



    TextView tvHeader = (TextView) convertView 
      .findViewById(R.id.tv_header); 
    tvHeader.setTypeface(null, Typeface.BOLD); 
    tvHeader.setText(headerTitle); 

    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 


@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 
} 

そして、これはレイアウトです:

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/rl_expen_but" 
     android:layout_below="@+id/rl_ats_draft2" 
     android:layout_above="@+id/selanjutnya" 
     > 



     <ExpandableListView 
      android:background="@color/cardview_light_background" 
      android:id="@+id/lvExp" 
      android:descendantFocusability="beforeDescendants" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:animateLayoutChanges="true"/> 

     <View 
      android:layout_below="@+id/lvExp" 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#bebebe"/> 

    </RelativeLayout> 

は、上記のコードで私がやった何か問題はありますか?ありがとうございました。

EDIT:

これは私のコードは、chandil03によって示唆されているように私はそれを編集した後にどのように見えるかです。ヘッダーを展開すると、expandablelistviewが更新されなくなりました。しかし、不思議なことに、私がgroupposition 0と子の位置0でedittextに値を入力すると、別のgouppositionとchildpositionの中のいくつかの他のedittextに、前のedittextで入力した同じ正確な値が表示されます。それとも、見知らぬ人でも、前の編集時の値が時折消え去ってしまいます。

答えて

1

あなたがすることによって、このデータを取得することができ

http://bignerdranch.github.io/expandable-recycler-view/

その後AddTextChangedListenerを使用することによって、私は私が入力した値を挿入します。その後、ViewHolderChildで挿入された値を確認します。例:

public void bind(final MyModel model) { 
      tvName.setText(model.getName()); 
      tvPrice.setText(model.getPrice()); 
      tvNote.setText(model.getNote()); 

      for(int i=0;i<listProduct.size();i++){ 
       if(listProduct.get(i).getIdProduct().equals(model.getIdProduct())){ 
        et.setText(listProduct.get(i).getQuantity()); 
        break; 
       } 
      } 

      et.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) { 
        if(m!=null){ 
         listProduct.remove(m); 
        } 

        if (!s.toString().equals("")) { 

         m = new MyModel(); 
         m.setName(model.getName()); 
         m.setNote(model.getNote()); 
         m.setIdProduct(model.getIdProduct()); 
         m.setPrice(model.getPrice()); 
         m.setQuantity(s.toString()); 
         m.setSubtotal(Long.parseLong(s.toString()) * Long.parseLong(model.getPrice()) + ""); 
         m.setStock(model.getStock()); 
         listProduct.add(m); 
        } 
       } 
      }); 
     } 
3

別のグループリストを更新すると、いつでも、テキストがないEditTextの別のインスタンスが表示されます。テキストを入力したEditTextは、別のgroupViewの子になります。

ViewHolderクラスを作成し、ビューを保持してタグとして設定することをお勧めします。getView()convertViewが既にnullであるかどうかを確認してください。他の部分とgetTagをconvertViewから追加し、それに応じて値を設定してください。例えば

:私はあなたのコードを変更した

@Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.my_layout, parent, false); 
      ViewHolder holder = new ViewHolder(); // View holder to save views. 
      holder.addView(v.findViewById(R.id.myView)); 
      v.setTag(holder); 
     } 
     else{ 
     ViewHolder holder = (ViewHolder) v.getTag(); // get tag and set values 
     // Do whatever you need to with the group view 
     } 
     return v; 
    } 

EDIT 1

。コメントもチェックしてください。

@Override 
    public View getChildView(final int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 

     final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition); 
     ViewHolder holder; 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.editText.setText(childText.getTotal); // Here whatever you will type in edittext will be overwritten by the value of 'childText.getTotal'. So after you are done writing in edit text make sore you change that in "_listDataChild" list. 

     return convertView; 
    } 

    /**This is a class that holds view, Here i m not talking about support.v7.widget.RecyclerView.ViewHolder class. Though concept is more or less same.*/ 
    class ViewHolder{ 
     EditText editText; 

     public ViewHolder(View v) 
     { 
      editText = (EditText) v.findViewById(R.id.et_total); 
     } 
    } 

EDIT 2

getChildView()方法で次のコードを追加します。

holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() 
    { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) 
     { 
      if(!hasFocus) 
       childText.getTotal = holder.editText.getText().toString(); 
// In java variable contains reference of Object so when you change childText object, it will be reflected in same HashMap you are getting data from. 

     } 
    }); 
+0

私はそれを試みましたが、結果は実際にはexpandablelistviewは更新されませんでした。しかし、不思議なことに、私がgroupposition 0と子の位置0でedittextに値を入力すると、別のgouppositionとchildpositionの中のいくつかの他のedittextに、前のedittextで入力した同じ正確な値が表示されます。それとも、見知らぬ人でも、前の編集時の値が時折消え去ってしまいます。なぜそれが起こったのですか? –

+0

ListViewは、ビューを循環/使用してメモリを効率的にします。したがって、setTagとgetTagを使用してviewHolderを使用してビューを処理していない場合。この問題に直面するでしょう。あなたは、この問題を抱えている理由の1つ以上の子にedittextの同じ参照を設定しています。調べることができるように、あなたの修正されたコードであなたの質問を編集してください。 – chandil03

+0

質問を編集しました。今、viewHolder、settag()、およびgettag()を使用しなければならないアダプター、グループビュー、または子ビューのどれを要求したいのですか?私は彼らに両方を使用させましたが、何も変わりません。上記のコードでholder.addviewについて教えてください。私はそれを正しく使うかどうかわかりません。 –

1

EditTextは、ExpandableListViewの別のヘッダーをスクロールまたはクリックするたびにリサイクルされます。

あなたの最善の策は、ユーザーが入力したテキストをモデルに保存することです e。G:

etTotal.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
       childText.setTotal(etTotal.getText().toString().trim()); 
      } 
     } 
    }); 
+0

はリサイクルされたモデルではなく、その価値も失われていますか? –

1

まず、このようなのEditTextからデータを格納するためのダミーモデルクラスを作成します。

public class DummyModel { 
    String edt_field; 
    // 
    //And any other data you might want to store 
    // 
    public DummyModel(String field) 
    { 
     this.edt_field=field; 
    } 
} 

その後、あなたのデータを保存するために、静的な配列リストを作成します。

public class DummyStatic { 
public static ArrayList<DummyModel> arl_static=new ArrayList(); 
} 

その後にあなたのアダプタクラスは次のようにgetChildView()メソッドを変更します:

@Override 
public View getChildView(final int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

    final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 

    final EditText etTotal = (EditText)convertView 
      .findViewById(R.id.et_total); 
    etTotal.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) { 
      DummyStatic.arl_static.add(childPosition,new DummyModel(charSequence.toString())); 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 

    etTotal.setText(childText.getTotal); 

    return convertView; 
} 

このコードは、編集テキストに文字を入力すると、arraylistのDummyModelオブジェクトに格納されます。このライブラリを使用してこれを処理するために、私はついにでき

String value = DummyStatic.arl_static.get(position).edt_field; 
関連する問題