0

This is the first time I'm going for a recycled view, I added the adaptor class and all, no errors in the code but nothing shows up. I did everything according to the first answer in this post Simple Android RecyclerView example but still there is something that I'm doing wrong and I don't know where to look for it. In the Run log I found this error

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList

but I tried fixing it with what I found online but still it didn't work

    package com.mircea.bookapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class BookActivity extends AppCompatActivity implements BookListViewAdapter.ItemClickListener {


    BookListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);


        //Hardcoded books
        List<Book> books = new ArrayList<>();
        books.add(new Book(" In Search of Lost Time", "Marcel Proust", 1913, "modernist"));
        books.add(new Book(" In Search of Lost Time", "Marcel Proust", 1913, "modernist"));
        books.add(new Book(" In Search of Lost Time", "Marcel Proust", 1913, "modernist"));
        books.add(new Book(" In Search of Lost Time", "Marcel Proust", 1913, "modernist"));
        books.add(new Book(" In Search of Lost Time", "Marcel Proust", 1913, "modernist"));

        // set up the RecyclerView
        RecyclerView recyclerView = findViewById(R.id.booklist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BookListViewAdapter(this, books);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();

    }
}

What am I doing wrong? Also here is the code for the BookListViewAdapter class

package com.mircea.bookapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class BookListViewAdapter extends RecyclerView.Adapter<BookListViewAdapter.ViewHolder> {

    private List<Book> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    // data is passed into the constructor
    BookListViewAdapter(Context context, List<Book> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }


    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookListViewAdapter.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
    // convenience method for getting data at click position
    Book getItem(int id) {
        return mData.get(id);
    }

    // allows clicks events to be caught
    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.bookTitle);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

        }
    }


}
dzstr
  • 73
  • 6
  • Have you debugged to check if any of your `Adapter` methods are being called? Are you sure that you're returning a non-zero value from your `Adapter`'s `getItemCount()` method? Are you certain that the `RecyclerView` is visible on-screen? – Mike M. Jun 13 '20 at 13:28
  • `code` @Override public int getItemCount() { return mData.size(); } `code` this is the code I used for getItemCount() as for the RecyclerView i'm not sure on how to check that – dzstr Jun 13 '20 at 13:37
  • Can you post the code for BookListViewAdapter – Ayush Bansal Jun 13 '20 at 13:40
  • I updated the question with the code from BookListViewAdapter – dzstr Jun 13 '20 at 13:42
  • You forgot to do anything in `onBindViewHolder()`. You'll want to set the text on your `ViewHolder`'s `TextView` there. – Mike M. Jun 13 '20 at 13:44

2 Answers2

0

You need to setText in onBindViewHolder

@Override
    public void onBindViewHolder(@NonNull BookListViewAdapter.ViewHolder holder, int position) {
        holder. myTextView.setText()//the text you want 
    }
Ayush Bansal
  • 702
  • 1
  • 7
  • 17
0

I think the problem is that you forgot to add your ArrayList. Try adding it with your RecyclerView setup code.

   RecyclerView recyclerView = findViewById(R.id.booklist);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Add ArrayList here
    adapter = new BookListViewAdapter(this, books);
    adapter.setClickListener(this);
    recyclerView.setAdapter(adapter);
JMB
  • 313
  • 2
  • 9