单击下一个ExpandableListView标头时,ExpandableListView内的EditText内容消失

时间:2016-04-13 09:27:16

标签: android android-edittext expandablelistview

我在edittext项目中有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>

我上面的代码有什么问题吗?谢谢。

修改

这是我按照chandil03的建议编辑后的代码。展开标题时,expandablelistview不再刷新。但奇怪的是,当我在集合0和子位置0的edittext中键入值时,在不同的goupposition和childposition中的其他一些edittext中出现与我在前一个edittext中键入的相同的确切值。甚至更奇怪的是,以前的edittext上的值有时会消失。

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

4 个答案:

答案 0 :(得分:3)

每当你展开另一个组列表时刷新,你得到另一个没有文本的EditText实例。您输入文本的EditText将成为另一个groupView的子项。

因此,我建议您创建一个ViewHolder类并将视图保留在其中并将其设置为标记,并在getView()中检查convertView是否为空。只需从convertView添加else part和getTag并相应地设置值。

例如:

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

编辑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);
        }
    }

编辑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.

        }
    });

答案 1 :(得分:1)

每次向下滚动或单击ExpandableListView中的另一个标题时,EditText都会被回收。

您最好的选择是保存用户在模型中输入的文字 e.g:

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

答案 2 :(得分:1)

首先创建一个虚拟模型类来存储来自edittext的数据,如下所示:

$conectar = mysqli_connect(HOST, USER, PASS, DATABASE);
    $listarCorreos = " SELECT userEmail 
                       FROM usuarios 
                    ";
    $resultado = mysqli_query($conectar,$listarCorreos);    

    while ( $row = mysqli_fetch_array($resultado, MYSQLI_ASSOC) ) {
      $emails[]=array($row['userEmail']);    
    }
echo json_encode($emails);

然后创建一个静态arraylist来存储您的数据:

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

然后在你的适配器类中更改你的getChildView()方法:

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

这段代码的作用是当你在edittext中输入字符时,它们将存储在arraylist中的DummyModel对象中。

您可以通过以下方式检索此数据:

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

答案 3 :(得分:1)

我终于可以通过使用这个库来处理这个问题了:

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

然后通过使用AddTextChangedListener,我插入了值I type。之后,检查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);
                    }
                }
            });
        }