listview 动态的添加或删除item

MainActivity


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.threepapa.virtualmachine.R;
import com.threepapa.virtualmachine.adapter.EditListAdapter;
import com.threepapa.virtualmachine.bean.SongBean;
import com.threepapa.virtualmachine.utils.DebugLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by lzy on 2017/3/2.
 */

public class MainActivity extends BaseActivity {
    @BindView(R.id.btn_high)
    Button btnHigh;
    @BindView(R.id.listview)
    ListView listview;
    private String[] mData = {"设备名称", "设备名称", "设备名称", "设备名称", "设备名称", "设备名称"};
    private EditListAdapter mAdapter;

    @Override
    protected int setContentByXML() {
        return R.layout.activity_main;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        mAdapter = new EditListAdapter();
        listview.setAdapter(mAdapter);
        mAdapter.setData(list());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_high})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_high:
                DebugLog.e("增加设备");
                mAdapter.addItem();
                break;

        }
    }

    public List<SongBean> list() {
        List<SongBean> mList = new ArrayList<>();
        for (int i = 0; i < mData.length; i++) {
            SongBean bean = new SongBean();
            bean.setName(mData[i]);
            mList.add(bean);
        }
        return mList;
    }

    @OnItemClick(R.id.listview)
    void onItemClick(int position) {
        DebugLog.e("点击了" + position);

    }


}

adapter

package com.threepapa.virtualmachine.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.threepapa.virtualmachine.R;
import com.threepapa.virtualmachine.bean.SongBean;
import com.threepapa.virtualmachine.utils.DebugLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2017/3/2.
 */

public class EditListAdapter extends BaseAdapter {

    private List<SongBean> mData;
    private int index = -1;

    public void setData(List<SongBean> data) {
        mData = data;
        this.notifyDataSetChanged();
    }

    public void addItem() {
        final SongBean songBean = new SongBean();
        songBean.setName("添加测试");
        songBean.setMac("AABBCCDDEEFF");
        mData.add(0, songBean);
        this.notifyDataSetChanged();
    }

    public void deletItem(int pos) {
        DebugLog.e(pos + "删除的position");
        mData.remove(pos);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listview_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        holder.tvMac.setText(mData.get(i).getName());
        EditText etMac = holder.etMac;

        //使listview中的edittext获取焦点
        etMac.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                // 在TOUCH的UP事件中,要保存当前的行下标,因为弹出软键盘后,整个画面会被重画
                // 在getView方法的最后,要根据index和当前的行下标手动为EditText设置焦点
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = i;
                }
                return false;
            }
        });

        etMac.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) {
                DebugLog.e(i + " onTextChanged-----" + s.toString());
                //如果该edittext有默认内容,还要在if那里进行过滤
                if (index >= 0 && s.length() > 0 && index == i) {
                    mData.get(index).setMac(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                DebugLog.e(" mac " + s.toString());

            }
        });

        etMac.setText(mData.get(i).getMac());
        etMac.clearFocus();

        if (index != -1 && index == i) {
            // 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
            etMac.requestFocus();
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DebugLog.e(i+"??????????????"+holder.tvMac.getText());
            }
        });
        holder.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugLog.e(" 删除设备 position = " + i);
                deletItem(i);
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_mac)
        TextView tvMac;
        @BindView(R.id.btn_del)
        Button btnDel;
        @BindView(R.id.et_mac)
        EditText etMac;
        @BindView(R.id.rl_item)
        RelativeLayout rlItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}

bean

package com.threepapa.virtualmachine.bean;

/**
 * Created by lzy on 2017/3/2.
 */

public class SongBean {
    private String name;

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    private String mac;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

开发过程中遇到 listview item中包含 edittext 点击焦点丢失,参考另外一篇解决方法

参考解决方法:http://www.jianshu.com/p/001b5836f5dc

推荐阅读更多精彩内容