Adapter传递数据到Activity(接口回调)

Adapter代码
构造方法:

public ContactListAdapter(Context context, List list,
                              QuickAlphabeticBar alpha, HashMap isSelected) {
        this.ctx = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.alphaIndexer = new HashMap();
        this.sections = new String[list.size()];
        this.isSelected = isSelected;

        for (int i = 0; i < list.size(); i++) {
            String name = getAlpha(list.get(i).getSortKey());
            if (!alphaIndexer.containsKey(name)) {
                alphaIndexer.put(name, i);
            }
        }
        mOnClickListener = (OnClickListener) context;
        }

在Adaper中定义接口:

public interface OnClickListener {
        public void setSelectedNum(int num);
    }

Adapter调用接口

mOnClickListener.setSelectedNum(getSelectSize());

Activity代码
在Activty中实现接口

public class ContactListActivity extends Activity implements ContactListAdapter.OnClickListener{
}

//接口回调,显示已选择的人数
    @Override
    public void setSelectedNum(int num) {
        haschoose.setText("已经选择" + num + "");
    }

你可能感兴趣的:(Android基础)