Android 自定义listview,添加删除。

不严谨,不规范,个人记录使用。

public class Base extends BaseAdapter {
    private List> list;
    private LayoutInflater inflater;

    Base(Context context, List> list1) {
        this.list = list1;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        viewhode hode = new viewhode();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            hode.textView = (TextView) convertView.findViewById(R.id.item_1);
            hode.textView2 = (TextView) convertView.findViewById(R.id.item_2);
            hode.checkBox = (CheckBox) convertView.findViewById(R.id.item_3);
            convertView.setTag(hode);
        } else {
            hode = (viewhode) convertView.getTag();
        }
        String a = list.get(position).get("tj").toString();
        String b = list.get(position).get("todo").toString();
        hode.textView.setText(a);
        hode.textView2.setText(b);
        return convertView;
    }

    public final class viewhode {
        private TextView textView, textView2;
        private CheckBox checkBox;
    }

}

给Base初始时定义一个Layoutinflater和一个用于传参数的list(好像Hashmap和Map有点细小的区别?),重写一个viewhoder,定义一下需要用到的控件(这里是用到了2个Textview)
,一个checkbox(用来做删除);
在getview方法中实例化viewhode,判空view,如果为空则用inflater填充写好的xml文件,findviewbyid实例化。给view设置tag;
else复用直接获取;
保证tag之后,从list中获取数据,加载到对应的地方。返回一个view;
添加部分:

base = new Base(getActivity(), list);
                add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("tj", sp1.getSelectedItem().toString());
                map.put("todo", sp2.getSelectedItem().toString());
                list.add(map);
                base.notifyDataSetChanged();
            }
        });

更新list,然后同步调用notify;

删除;

del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int max = listView.getChildCount();
                for (int i = max - 1; i >= 0; i--) {
                    View view = listView.getChildAt(i);
                    CheckBox checkBox = (CheckBox) view
                            .findViewById(R.id.item_3);
                    if (checkBox.isChecked()) {
                        list.remove(i);
                        checkBox.setChecked(false);
                    }
                }
                base.notifyDataSetChanged();
            }
        });

获取listview的总数,
倒着,从最大-1开始,到0,i–;
for循环内
实例化对应listview子列的view;
通过view,绑定对应的checkbox,
判断是否被选中,选中则删除对应list,同时设checkbox为不选中状态,调用notify刷新;

bug:添加多了,滑动listview时,复选框的勾会乱跑(可解决,没找到)。
有一次遇到删除删的不是选中的行(那次是把base直接在activity里实现),然而不知怎么就好了(好像一开始改回来也没好)。

你可能感兴趣的:(记录)