Android 控件绑定封裝

最近刚开始写android

随便记录一下,以后还会修改

绑定ListView,Spinner

先创建绑定项:

BaseItem

public class BaseItem {

    public BaseItem(Integer id,String value){
        this.setId(id);
        this.setValue(value);
    }

    private Integer id;
    private String value;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    //必须要重写,不然绑定spinner会有问题
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return value;
    }
}

然后创建绑定方法:
public class ControlHelper {
    public static <T extends AdapterView> void BindAdapterView(Context context, T control, List<BaseItem> list)
    {
        ArrayAdapter<BaseItem> adapter=new ArrayAdapter<BaseItem>(context, android.R.layout.simple_list_item_1,list);
        control.setAdapter(adapter);
    }
    public static <T extends AdapterView> void BindAdapterView(Context context, T control, List<BaseItem> list,Integer layout)
    {
        ArrayAdapter<BaseItem> adapter=new ArrayAdapter<BaseItem>(context, layout,list);
        control.setAdapter(adapter);
    }
}


调用:

List<BaseItem> list = new ArrayList<>();
//result.getData()是数据源
            for (int i = 0; i < result.getData().size(); i++) {
                Integer wid = result.getData().get(i).getPPR_ID();
                String name = result.getData().get(i).getPO_No();
                BaseItem item = new BaseItem(wid, name);
                list.add(item);
            }
	    //spinner
            ControlHelper.BindAdapterView(getBaseContext(), spInboundPO, list);
	    //listview
	    ControlHelper.BindAdapterView(getBaseContext(), lvInboundPO, list, android.R.layout.simple_list_item_1);











posted @ 2018-01-25 11:16  正怒月神  阅读(385)  评论(0编辑  收藏  举报