2016-08-02 2 views
2

ベースrecyclerviewを作成しましたが、他のクラスはベースクラスを拡張しています。私はbutterknifeを実装しました。しかし、他のクラスがベースクラスを拡張している場合は、ビューをnullにしています。以下はアダプタークラスでバターナイフが動作しない

私の基底クラスは以下

public abstract class BaseAdapter<T> 
     extends RecyclerView.Adapter<BaseAdapter.MyViewHolder> { 

    private List<T> mObjects; 
    private Context context; 

    public BaseAdapter(final List<T> objects, Context context) { 
     this.mObjects = objects; 
     this.context = context; 
    } 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(getActivityLayout(), parent, false); 
     ButterKnife.bind(this,parent); 
     return new MyViewHolder(itemView); 
    } 
    /** 
    * Adds the specified object at the end of the array. 
    * 
    * @param object The object to add at the end of the array. 
    */ 
    public void add(final T object) { 
     mObjects.add(object); 
     notifyItemInserted(getItemCount() - 1); 
    } 

    /** 
    * Remove all elements from the list. 
    */ 
    public void clear() { 
     final int size = getItemCount(); 
     mObjects.clear(); 
     notifyItemRangeRemoved(0, size); 
    } 

    @Override 
    public int getItemCount() { 
     return mObjects.size(); 
    } 

    public T getItem(final int position) { 
     return mObjects.get(position); 
    } 

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

    /** 
    * Returns the position of the specified item in the array. 
    * 
    * @param item The item to retrieve the position of. 
    * @return The position of the specified item. 
    */ 
    public int getPosition(final T item) { 
     return mObjects.indexOf(item); 
    } 

    /** 
    * Inserts the specified object at the specified index in the array. 
    * 
    * @param object The object to insert into the array. 
    * @param index The index at which the object must be inserted. 
    */ 
    public void insert(final T object, int index) { 
     mObjects.add(index, object); 
     notifyItemInserted(index); 

    } 

    /** 
    * Removes the specified object from the array. 
    * 
    * @param object The object to remove. 
    */ 
    public void remove(T object) { 
     final int position = getPosition(object); 
     mObjects.remove(object); 
     notifyItemRemoved(position); 
    } 

    /** 
    * Sorts the content of this adapter using the specified comparator. 
    * 
    * @param comparator The comparator used to sort the objects contained in this adapter. 
    */ 
    public void sort(Comparator<? super T> comparator) { 
     Collections.sort(mObjects, comparator); 
     notifyItemRangeChanged(0, getItemCount()); 
    } 
    public class MyViewHolder extends RecyclerView.ViewHolder { 

     public MyViewHolder(View view) { 
      super(view); 
     } 
    } 
    protected abstract int getActivityLayout(); 
} 

ある

public class SubRecyclerAdapter extends BaseAdapter { 

     private Context context; 
     private List<LocationList> getLocationLists; 
     @Nullable 
     @BindView(R.id.branchimage) 
     ImageView branchimage; 
     public SubRecyclerAdapter(List<LocationList> getLocationLists,Context context) { 
      super(getLocationLists,context); 
      this.context=context; 
      this.getLocationLists=getLocationLists; 
     } 

     @Override 
     protected int getActivityLayout() { 
      return R.layout.locationlist; 
     } 

     @Override 
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
      Picasso.with(context) .load(getLocationLists.get(position).getBranch_image()) .placeholder(R.drawable.dummy_image) 
        .error(R.drawable.dummy_image) .into(branchimage); 
//branchimage is null here 
     } 

私のサブクラスであるButterknifeバージョン: - あなたのアダプタでButterKnifeを使用したい場合に必要

compile 'com.jakewharton:butterknife:8.2.1' 
apt 'com.jakewharton:butterknife-compiler:8.2.1' 

答えて

2

あなたのビューホルダーとonCreateViewHolderでこのようにする:

//View Holder for performance optimizations of the recycler view display 
    static class AddProductsViewHolder extends RecyclerView.ViewHolder{ 

     @InjectView(R.id.product_image) 
     ImageView productImage; 
     @InjectView(R.id.product_name) 
     TextView productName; 
     @InjectView(R.id.product_brand) 
     TextView productBrand; 

     public final View mView; 

     public AddProductsViewHolder(View itemView) { 
      super(itemView); 
      mView = itemView; 
      ButterKnife.inject(this, itemView); 
     } 
    } 

@Override 
    public AddProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View productView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_products, parent, false); 
     return new AddProductsViewHolder(productView); 
    } 

使用方法:以下

@Override 
    public void onBindViewHolder(AddProductsViewHolder holder, int position) { 

     try{ 
      AddProductInfo addProductInfo = addProductInfoList.get(position); 

      if(!addProductInfo.getProductImage().isEmpty()){ 
      holder.productName.setText(addProductInfo.getProductName()); 
      holder.productBrand.setText(addProductInfo.getProductBrand()); 


     } 
     catch (Exception e){ 

     } 
    } 

コメントあなたは疑問を持っている場合。

はbutterknifeの最新バージョンを使用すると、ちょうど@Bind
これは私の古いコードであると@InjectViewを交換する必要があるが、私はそれがあなたの目的を果たすと思います。

+0

BaseAdapterClassにButterKnifeを挿入したいのですが、それを行う方法はあります。コメントありがとうございました – Soham

+0

なぜあなたはビューホルダーにこれを追加できません:ButterKnife.bind(this、parent);あなたが具体的なクラスでそれらを使用したい場合は、あなた自身の抽象メソッドを作成し、onBindViewHolder()の内側とonBindViewHolder内で行いたい抽象メソッドを呼び出すと、具体的なクラスこのメソッドを実装している人は、自分の操作を世話するでしょう。 – Manikanta

+0

あなたは私のコードをチェックしましたか?あなたがコメントで言ったように私はそれをやろうとしていました。私のコードを修正して間違いを訂正してください。 – Soham

関連する問題