2016-04-13 16 views
10

expandablelistview ürün çocuğunun içinde bir edittext var. Sonra, bu edittext içinde bir metin girdim, ancak diğer expandablelistview başlığını tıklattığımda, girdiğim metin kaybolur.İçindekiler ExpandableListView içinde EditText içeriği bir sonraki ExpandableListView üstbilgisini tıkladığınızda kaybolur

Manifest.xml sayfamda ve diğer birçok düzeltmelerimde zaten android:windowSoftInputMode="adjustPan" kullanıyorum, ancak çalışmadı.

bu benim adaptör geçerli:

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; 
} 
} 

Ve bu düzen: Ben yukarıdaki kodlarla yaptığı yanlış bir şey

<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> 

var mı? Teşekkür ederim.

DÜZENLEME:

kodum chandil03 önerdiği gibi ben bunu düzenlenebilir sonra nasıl göründüğünü budur. Expandablelistview, üstbilgileri genişletdiğimde artık yenilenmiyor. Ama garip bir şekilde, grup pozisyonunda 0 ve çocuk pozisyonunda 0 edittext yazdığımda, o zaman başka bir edittext farklı ayrışma ve çocuk pozisyonunda daha önceki edittext'de yazdığım aynı kesin değerler ortaya çıkıyor. Hatta daha da kötüsü, önceki edittext'deki değerler bazen ortadan kalktı.

static class ViewHolder { 
    protected View et; 

    public void addView(View et){ 
     this.et = et; 
    } 
} 

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

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

     ViewHolder viewHolder; 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.addView(convertView 
       .findViewById(R.id.et_total)); 
      convertView.setTag(viewHolder); 
     }else{ 
      viewHolder = (ViewHolder)convertView.getTag(); 
     } 

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

     etTotal.setText(childText.getTotal); 

     return convertView; 
    } 

cevap

3

Başka bir grup listesini genişletirseniz, yenilenir ve metin içermeyen başka bir EditText örneği alırsınız. Metne girdiğiniz EditText, başka bir groupView çocuksu olur.

Bu nedenle, bir ViewHolder sınıfı oluşturmanızı ve görünümünüzü burada tutmanızı ve bir etiket olarak ayarlamanızı ve getView() numaralı telefonun daha önce tamamladığınız convertView boş olup olmadığını kontrol etmenizi öneririz. Sadece convertView'dan parça ve getTag ekleyin ve değerleri buna göre ayarlayın. Örneğin

:

@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; 
    } 

DÜZENLEME 1

Ben senin kodunu değiştirmiş. Yorumlara da göz atın. getChildView() yönteminde kod aşağıdaki

@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); 
     } 
    } 

DÜZENLEME 2

Ekle.

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

Bunu denedim ve sonuç gerçekten de expandablelistgörün yenilemedi. Ama garip bir şekilde, grup pozisyonunda 0 ve çocuk pozisyonunda 0 edittext yazdığımda, o zaman başka bir edittext farklı ayrışma ve çocuk pozisyonunda daha önceki edittext'de yazdığım aynı kesin değerler ortaya çıkıyor. Hatta daha da kötüsü, önceki edittext'deki değerler bazen ortadan kalktı. Bu neden oldu? –

+0

ListView, belleği verimli hale getirmek için görünümlerini çevirir/kullanır. Yani eğer setTag ve getTag kullanarak viewHolder kullanarak görünümleri kullanmıyorsanız. Bu problemle karşılaşacaksın. Birden fazla çocuğa edittext ile aynı referansı yapıyorsunuz, bu yüzden bu soruna sahip oluyorsunuz. Sorguyu düzenlediğiniz kodla düzenleyin, böylece bakabilirim. – chandil03

+0

Sorumu düzenledim. Şimdi viewHolder, settag() ve gettag() 'ı kullanmak zorunda olan adaptör, groupview veya childview'de hangisini sormak istiyorum? İkisini de kullandım ama hiçbir şey değişmedi. Ayrıca yukarıdaki kodunuzda holder.addview hakkında bilgi verebilir misiniz? Bunu doğru kullanıp kullanmadığımı bilmiyorum. –

1

EditText aşağı kaydırmanız veya ExpandableListView başka başlığını tıklayın her zaman geri dönüştürülmektedir.

En iyi bahsiniz, kullanıcının modelinize girdiği metni kaydetmek olacaktır. e.g:

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

modelin kendisi geri dönüştürülmüş ve değerleri de yitirilmiş değil mi? –

1

İlk böyle EDITTEXT verilerinizi depolamak için bir kukla modeli sınıf oluşturmak:

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

Sonra verileri depolamak için statik bir arraylist oluşturun:

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

Sonra içinde bağdaştırıcı sınıfınız aşağıdaki gibi getChildView() yönteminizi değiştirir:

@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; 
} 

Bu kodun yapacağı şey, edittext karakterlerini yazarken arraylist içindeki DummyModel nesnesinde saklanmalarıdır.

Sen tarafından bu veri alabilirsiniz:

String value = DummyStatic.arl_static.get(position).edt_field; 
İlgili konular