这是我第一次使用回收视图,我添加了适配器类,并且所有代码都没有错误,但没有任何显示。我根据这篇帖子Simple Android RecyclerView example中的第一个答案做了所有事情,但仍然有一些地方我做错了,我也不知道在哪里寻找。在运行日志中,我发现了此错误
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList
但是我尝试使用在线查找的内容修复它,但仍然无法正常工作
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();
}
}
我在做什么错?这也是BookListViewAdapter类的代码
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) {
}
}
}
答案 0 :(得分:0)
您需要在onBindViewHolder中设置setText
@Override
public void onBindViewHolder(@NonNull BookListViewAdapter.ViewHolder holder, int position) {
holder. myTextView.setText()//the text you want
}
答案 1 :(得分:0)
我认为问题是您忘记添加ArrayList。尝试将其与您的RecyclerView设置代码一起添加。
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);