java - 如何在 android 适配器中使用 Roboguice 依赖注入(inject)

标签 java android roboguice

我做了什么: 我知道在如下 Activity 中使用 Roboguice 的依赖注入(inject)

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

问题:

如何在中使用它

代码

从 Activity 中我调用适配器,如下所示::

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

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);
        }
    }

}

编辑

  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);
        }
    }
<小时/>

错误:

  1. mContext 给我错误提示

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

    无法解析方法注入(inject) View 成员

snapshot

最佳答案

可以查看RoboActivity的来源看看它是如何完成的。

基本上,您需要在 View 持有者的构造函数中调用RoboInjector.injectViewMembers()。像这样的事情:

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);
    }
}

请注意,这只会查看注入(inject)。 如果您想进行依赖注入(inject)(使用@Inject字段),您应该使用RoboInjector.injectMembersWithoutViews()

关于java - 如何在 android 适配器中使用 Roboguice 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31021978/

相关文章:

Java OpenGL(JOGL) 对象数组和 FloatBuffer

Android:如何将一个应用程序从同一个应用程序转移到另一部 Android 手机

java - 扩展 RoboActivity 会出现 ClassNotFoundException

android - 解决 Logcat 警告消息

android - 带有 roboguice 抛出异常的简单 android 应用程序

java - 如何在 java 中比较两个 arraylist<Contacts>

java - 如何比较黑莓上的2时间?

c# - 查找可以用给定字母集拼写的单词集

android - Android 中的非对称relativelayout 行为

java - 带 TextView 的 RecyclerView 适配器