ListView动态删除和添加editview

需求:在运行时动态添加ListView的editview,并将输入的数据存放到相应的数组中,也可以删除某些你不需要的项。

一开始通过addTextChangedListener来监听editview上数据的变化,不过因为多个editview同时监听出现了混乱,导致了删除editview上数据错位。

之后通过 setOnFocusChangeListener来监听editview焦点的变化,完美的解决了数据错位的问题。

下面附上Adapter的代码.

public class MoreAdapter extends BaseAdapter{

    private Context context;
    private List<MoreInfo> items;

    public MoreAdapter(Context context) {
        this.context = context;
        this.items = new ArrayList<>();
    }
    public MoreAdapter(Context context, List<MoreInfo> items) {
        this.context = context;
        this.items = items;
    }

    public void setItems(List<MoreInfo> items) {
        this.items = items;
    }

    public List<MoreInfo> getItems() {
        return items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_more, null);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv_field);
            holder.et = (EditText) convertView.findViewById(R.id.et_value);
            holder.btn = (ImageButton) convertView.findViewById(R.id.btn_delete);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        MoreInfo info = items.get(position);
        holder.tv.setText(info.getField());
        if (info.getValue() == null) {
            holder.et.setText("");
        }else {
            holder.et.setText(info.getValue());
        }

        holder.et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                 // 判 断items的长度是否大于0,可以防止数组越界
                    if (items.size() > 0) {
                        items.get(position).setValue(((EditText) v).getText().toString().trim());
                    }
                    System.out.println(" " + position);
                }
            }
        });

        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                items.remove });

        return convertView;
    }

    private class ViewHolder {
      TextView tv;
      EditText et;
      ImageButton btn;
    }
}

item_more.xml的代码

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp">

    <TextView  android:id="@+id/tv_field" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginLeft="20dp" android:layout_centerVertical="true" android:text="单位" android:textColor="@color/white"/>

    <EditText  android:id="@+id/et_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textNoSuggestions" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/tv_field" android:layout_toLeftOf="@+id/btn_delete"/>

    <ImageButton  android:id="@+id/btn_delete" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/delete" android:background="@android:color/transparent" android:layout_marginRight="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:scaleType="fitCenter"/>

</RelativeLayout>

你可能感兴趣的:(ListView,EditView,数据混乱)