0

What i have done: I am aware of using dependency injection of Roboguice in activity like below

 @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

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

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

    Cannot resolve method inject view members

snapshot

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • As explained in my comment, you should use `RoboGuice.getInjector(base.getContext()).injectViewMembers(this);` if you want to inject the views in your ViewHolder. Not sure why you are getting this error though, the method definitely exists – nicopico Jun 24 '15 at 09:41
  • @ nicopico ... please check the snapshot .... should i need to do anything other than that like passing some thing to adapter related to roboguice than the one i posted – Devrath Jun 24 '15 at 09:49
  • Can you try with `injectMembers` instead of `injectViewMembers` ? – nicopico Jun 24 '15 at 10:17
  • Tried it ... app crashes .... i guess we have to do some thing more to adapter also – Devrath Jun 24 '15 at 12:18

1 Answers1

1

You can look at the source of RoboActivity to see how it is done.

Basically, you need to call RoboInjector.injectViewMembers() in the constructor of your viewholder. 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().

nicopico
  • 3,606
  • 1
  • 28
  • 30
  • Which version of RoboGuice are you using? Also, the parameter passed to `injectViewMembers` should be the one holding the `@InjectView` annotations (in this case, the `ViewHolder` instance) – nicopico Jun 24 '15 at 09:31
  • I am using `org.roboguice:roboguice:3.0-alpha-2` .......... I have extended `RoboActionBarActivity` ... and the fragment in it has extended `RoboFragment` ... the adapter is on the fragment – Devrath Jun 24 '15 at 09:34