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