简体   繁体   English

如何在适配器Android中使用Roboguice依赖项注入

[英]How can I use Roboguice dependency injection in adapter android

What i have done: I am aware of using dependency injection of Roboguice in activity like below 我做了什么:我知道在如下所示的活动中使用Roboguice依赖注入

 @InjectView(R.id.listView) ListView listView;

Question: 题:

How to use this in a 如何在

CODE

From activity I am calling adapter like below:: 从活动中,我正在调用适配器,如下所示:

AdptOrderListHome tickets = new AdptOrderListHome(getActivity(), result.getProducts());
listView.setAdapter(tickets);

AdptOrderListHome.java AdptOrderListHome.java

public class AdptOrderListHome extends BaseAdapter {

    ArrayList<InventoryProductItems> mProducts;

    private Context mContext = null;

    public AdptOrderListHome(Context context, ArrayList<InventoryProductItems> products) {
        super();
        mContext = context;
        mProducts = products;
        Log.d("",mProducts.size()+"");
        Log.d("",mProducts.size()+"");
    }

    public int getCount() {
        return mProducts.size();
    }

    public InventoryProductItems getItem(int position) {
        return mProducts.get(position);
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = convertView;

        final ViewHolder vHolder;
        if (convertView == null) {


            LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layout.inflate(R.layout.row_order_list_home, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }

        //Set the tag
        vHolder.txtProductNameId.setTag(mProducts.get(position).getProduct().getId());

        vHolder.txtProductNameId.setText(mProducts.get(position).getProduct().getName());
        vHolder.txtInStockId.setText(mProducts.get(position).getCurrent_Product_item_Count()+"");
        vHolder.txtRbProductsId.setText(mProducts.get(position).getRB_Product_item_Count()+"");



        return view;
    }

    class ViewHolder {
        private TextView txtProductNameId, txtInStockId, txtRbProductsId;
        private LinearLayout root;

        public ViewHolder(View base) {
            txtProductNameId = (TextView) base.findViewById(R.id.txtProductNameId);
            txtInStockId = (TextView) base.findViewById(R.id.txtInStockId);
            txtRbProductsId = (TextView) base.findViewById(R.id.txtRbProductsId);
            root = (LinearLayout) base.findViewById(R.id.root);
        }
    }

}

EDIT 编辑

  class ViewHolder {

        @InjectView(R.id.txtProductNameId) private TextView txtProductNameId;
        @InjectView(R.id.txtInStockId) private TextView txtInStockId;
        @InjectView(R.id.txtRbProductsId) private TextView txtRbProductsId;

        public ViewHolder(View base) {
            RoboGuice.getInjector(base.getContext()).injectViewMembers(mContext);
        }
    }

error: 错误:

  1. mContext giving me error in line mContext在行中给我错误

    RoboGuice.getInjector(base.getContext()).injectViewMembers(mContext); RoboGuice.getInjector(base.getContext())。injectViewMembers(mContext);

    Cannot resolve method inject view members

快照

You can look at the source of RoboActivity to see how it is done. 您可以查看RoboActivity的来源以了解如何完成。

Basically, you need to call RoboInjector.injectViewMembers() in the constructor of your viewholder. 基本上,您需要在RoboInjector.injectViewMembers()的构造函数中调用RoboInjector.injectViewMembers() Something like this: 像这样:

class ViewHolder {
    @InjectView(R.id.txtView)
    private TextView txtView;
    @InjectView(R.id.imgView)
    private ImageView imgView;

    public ViewHolder(View root) {
        RoboGuice.getInjector(root.getContext()).injectViewMembers(this);
    }
}

Note that this will only View Injection . 请注意,这将仅查看注入 If you want to do Dependency Injection (with @Inject fields), you should use RoboInjector.injectMembersWithoutViews() . 如果要进行依赖注入 (使用@Inject字段),则应使用RoboInjector.injectMembersWithoutViews()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM