这是我第一次使用循环视图,我添加了adaptor类,代码中没有错误,但什么也没有显示出来。 我按照第一个答案做了所有的事情,在这篇文章中简单的Android RecyclerView示例,但仍然有一些事情我做错了,我不知道去哪里找它。 在运行日志中,我发现了这个错误
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) {
}
}
}
您需要在onBindViewHolder中设置Text
@Override
public void onBindViewHolder(@NonNull BookListViewAdapter.ViewHolder holder, int position) {
holder. myTextView.setText()//the text you want
}
我想问题是你忘了添加你的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);