【问题标题】:How can I use Roboguice dependency injection in adapter android如何在适配器 android 中使用 Roboguice 依赖注入
【发布时间】:2015-06-24 08:51:24
【问题描述】:

我做了什么: 我知道在下面的活动中使用 Roboguice 的依赖注入

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

问题:

如何在a中使用它

代码

从活动中我调用适配器如下::

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

    Cannot resolve method inject view members

【问题讨论】:

  • 正如我在评论中解释的那样,如果你想在 ViewHolder 中注入视图,你应该使用RoboGuice.getInjector(base.getContext()).injectViewMembers(this);。不知道为什么你会得到这个错误,该方法肯定存在
  • @nicopico ...请检查快照....除了我发布的内容之外,我还需要做其他事情吗?比如将一些东西传递给与roboguice相关的适配器
  • 你能用injectMembers代替injectViewMembers吗?
  • 试过了……应用程序崩溃了……我想我们还需要对适配器做更多的事情

标签: java android roboguice


【解决方案1】:

你可以看看RoboActivity的源码看看是怎么做的。

基本上,您需要在 viewholder 的构造函数中调用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字段),你应该使用RoboInjector.injectMembersWithoutViews()。

【讨论】:

  • 您使用的是哪个版本的 RoboGuice?此外,传递给injectViewMembers 的参数应该是包含@InjectView 注释的参数(在本例中为ViewHolder 实例)
  • 我正在使用org.roboguice:roboguice:3.0-alpha-2 ....我已扩展RoboActionBarActivity ...并且其中的片段已扩展RoboFragment ...适配器已打开片段
猜你喜欢
  • 2011-09-08
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多