RecyclerView给出错误

时间:2016-07-15 12:21:56

标签: android android-recyclerview

我有两个列表,一个是所有项目,另一个是用户选择的项目。我想更改用户选择的项目图标,但收到错误我不知道为什么。

以下是我的RecyclerView的代码: -

package adapters;

import java.util.List;

import com.qanda.R;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RecyclerView_Adapter_Categories_update
        extends
        RecyclerView.Adapter<RecyclerView_Adapter_Categories_update.MyViewHolder> {

    List<Categories> list;
    Context context;
    LayoutInflater inflater;
    List<Categories> mSelectedList;
    int pos[];
    int x = 0;

    public RecyclerView_Adapter_Categories_update(List<Categories> list,
            Context context, List<Categories> mSelectedList) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        this.mSelectedList = mSelectedList;
        pos = new int[list.size()];

        // capturepos();
        // Log.d("item Select List", mSelectedList.get(0).mCategory);
        // Log.d("item List", list.get(1).mCategory);
        // for(int i = 0 ; i < mSelectedList.size() ; i++){
        // Log.d("Inside Capture pos 1st loop", "hey");
        // for(int j =0 ; j < list.size() ; j++){
        // Log.d("Inside Capture pos 2nd loop", "hey");
        // if(mSelectedList.get(i).mCategory.equals(list.get(j).mCategory)){
        // pos[x] = j;
        // x++;
        // }
        // }
        // }
    }

    // public void capturepos(){
    //
    // }
    public List<Categories> getList() {
        return mSelectedList;
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int arg1) {
        Categories cat = list.get(arg1);
        holder.mCategory.setText(cat.mCategory);
        // holder.mIcon.setImageResource(cat.mIconId);
        for (int i = 0; i < pos.length; i++) {
            if (arg1 == pos[i]) {
                holder.mAddIcon.setImageResource(R.drawable.ic_done_black_24dp);
            }
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        View view = inflater.inflate(R.layout.cust_row_recyclerview_categories,
                arg0, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView mIcon, mAddIcon;
        TextView mCategory;
        int i = 1;

        public MyViewHolder(View itemView) {
            super(itemView);
            mCategory = (TextView) itemView.findViewById(R.id.cateogry_text);
            mAddIcon = (ImageView) itemView
                    .findViewById(R.id.category_select_icon);
            mAddIcon.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (i % 2 == 1) {
                        Animation anim = AnimationUtils.loadAnimation(context,
                                R.drawable.settrans);
                        mAddIcon.startAnimation(anim);
                        mAddIcon.setImageResource(R.drawable.ic_done_black_24dp);
                        i++;
                //      Categories cat = new Categories();
                //      cat.mCategory = mCategory.getText().toString();
                //      mSelectedList.add(cat);
                    } else {
                        Animation anim = AnimationUtils.loadAnimation(context,
                                R.drawable.settrans);
                        mAddIcon.startAnimation(anim);
                        mAddIcon.setImageResource(R.drawable.ic_add_black_24dp);
                        i++;
                //      Categories cat = new Categories();
                //      cat.mCategory = mCategory.getText().toString();
                //      mSelectedList.remove(cat);

                    }
                }
            });
        }

    }

}

我的RecyclerView仅显示完整列表,没有任何更改。我所做的是在构造函数内部评论 当我尝试在日志中打印项目时

07-15 18:03:25.173: E/AndroidRuntime(7610): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-15 18:03:25.173: E/AndroidRuntime(7610):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
07-15 18:03:25.173: E/AndroidRuntime(7610):     at java.util.ArrayList.get(ArrayList.java:308)
07-15 18:03:25.173: E/AndroidRuntime(7610):     at adapters.RecyclerView_Adapter_Categories_update.<init>(RecyclerView_Adapter_Categories_update.java:40)

1 个答案:

答案 0 :(得分:0)

您传递的mItemSelectedList为空。 因此调用

  Log.d("item Select List", mSelectedList.get(0).mCategory); //line 40

抛出异常。没有位置0.因此,尝试在日志中打印它是试图在此列表中获得不存在的位置索引

package adapters;

import java.util.List;

import com.qanda.R;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RecyclerView_Adapter_Categories_update
    extends
       RecyclerView.Adapter<RecyclerView_Adapter_Categories_update.MyViewHolder> {

List<Categories> list;
//bad idea to pass a context, can lead to memory leaks




public RecyclerView_Adapter_Categories_update(List<Categories> list        ) {
           this.list = list;
}

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

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Categories cat = list.get(position);
    holder.mCategory.setText(cat.mCategory);

    holder.mAddIcon.setOnClickListener(new View.OnClickListener(){

      public void onClick(View v){

        holder.mAddIcon.setImageResource(R.drawable.ic_done_black_24dp);
        //secondSelectedList.add(cat)
        //selectedListAdapter.notifyDataSetChanged();
        //this will change the icon in first list and add it to your second list if your adapter is set.
      }

   });


}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cust_row_recyclerview_categories,parent, false);

    return new MyViewHolder(view);
}

class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView mIcon, mAddIcon;
    TextView mCategory;


    public MyViewHolder(View itemView) {
        super(itemView);
        mCategory = (TextView) itemView.findViewById(R.id.cateogry_text);
        mAddIcon = (ImageView) itemView
                .findViewById(R.id.category_select_icon);

    }

}

}