web-dev-qa-db-ja.com

画像をドローアブルからリサイクラービューに設定

TextViewとImageViewを備えたCardViewを含む下部のナビゲーションフラグメントにRecyclerViewを実装したいと思います。また、新しいアクティビティを開くすべてのカードにonClickListenerがあります。しかし、私のrecyclerviewには何も表示されず、nullポイント例外が発生します Logcat 。ここで何が欠落しているかわかりません。

モデルクラス

   class Data {

public int imageId;
public String txt;

Data( int imageId, String text) {

    this.imageId = imageId;
    this.txt = text;
}
}

RecyclerView Adapter

    public class RecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
List<Data> data ;
Context context;

public RecyclerViewAdapter(Context context, ArrayList<Data>data) {
    this.context = context;
    this.data = data;
}

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        public CardView mCardView;
        public TextView mTextView;
        public ImageView imageView;

        public MyViewHolder(View v){
            super(v);

            mCardView = (CardView) v.findViewById(R.id.cardview1);
            mTextView = (TextView) v.findViewById(R.id.tv_text);
            imageView = (ImageView) v.findViewById(R.id.iv_image);

        }
    }

    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

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

        Data all = data.get(position);
        holder.mTextView.setText(all.txt);
        holder.imageView.setImageResource(all.imageId);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                final Intent intent;
                if (position == 0){
                    intent =  new Intent(context, ShowDetails.class);
                } else if (position == 1){
                    intent =  new Intent(context, AllDetails.class);
                } else {
                    intent =  new Intent(context, RecentDetails.class);
                }
                context.startActivity(intent);
            }
        });
    }

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

MainActivity

     RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private List<Data> data;

public StatusFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_status, container, false);

    recyclerView = rootView.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    ArrayList<Data> data= new ArrayList<>();

    data.add(new Data( R.drawable.bg, "Image 1"));
    data.add(new Data( R.drawable.bg, "Image 2"));
    data.add(new Data( R.drawable.bg, "Image 3"));
    data.add(new Data( R.drawable.bg, "Image 1"));
    data.add(new Data( R.drawable.bg, "Image 2"));
    data.add(new Data( R.drawable.bg, "Image 3"));
    data.add(new Data( R.drawable.bg, "Image 1"));
    data.add(new Data( R.drawable.bg, "Image 2"));
    data.add(new Data( R.drawable.bg, "Image 3"));

    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(llm);
    return  rootView;
}
}

ScreenShot

5
shivani patil

RecyclerViewAdapterコンストラクターを呼び出していません:

この行をフラグメントに追加して呼び出しますadapter = new RecyclerViewAdapter (getActivity(), data);

RecyclerViewAdapter adapter;
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
adapter = new RecyclerViewAdapter (getActivity(), data);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;
3
Abhishek kumar