我已经检查了在互联网上找到的所有内容。我不知道为什么RecyclerView没有显示任何项目。虽然没有显示任何错误。 有我的适配器类
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class bookAdapter extends
RecyclerView.Adapter<bookAdapter.bookViewHolder> {
ArrayList<Book> arrayList;
TextView bookNameTV;
TextView authorNameTV;
public bookAdapter(ArrayList<Book> arrayList){
this.arrayList = arrayList;
}
@Override
public bookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view =
LayoutInflater.from(context).inflate(R.layout.book_list_item,parent,false);
return new bookViewHolder(view);
}
@Override
public void onBindViewHolder(bookViewHolder holder, int position) {
Book book = arrayList.get(position);
//holder.bind(book);
bookNameTV.setText(book.getBook());
authorNameTV.setText(book.getAuthor());
}
@Override
public int getItemCount() {
return 0;
}
public class bookViewHolder extends RecyclerView.ViewHolder{
public bookViewHolder(View itemView) {
super(itemView);
bookNameTV = (TextView) itemView.findViewById(R.id.book_name);
authorNameTV = (TextView) itemView.findViewById(R.id.author_name);
}
public void bind(Book book){
bookNameTV.setText(Book.book);
authorNameTV.setText(Book.author);
}
}
}
这是Book类,具有要显示的信息:
public class Book {
public static String book;
public static String author;
Book(String book, String author){
this.book = book;
this.author = author;
}
public String getBook() {
return book;
}
public void setBook(String book) {
this.book = book;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
这是Main Activity,正在初始化Book和Adapter类: 公共类MainActivity扩展了AppCompatActivity {
ArrayList<Book> arrayList = new ArrayList<Book>();
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rvContacts);
recyclerView.setHasFixedSize(true);//also checked by removing this line
recyclerView.setLayoutManager(new
LinearLayoutManager(getApplicationContext()));
arrayList.add(new Book("Power","Burhan"));
arrayList.add(new Book("influence","Burhan Sabir"));
bookAdapter book_Adapter = new bookAdapter(arrayList);
book_Adapter.notifyDataSetChanged();
recyclerView.setAdapter(book_Adapter);
}
}}
我认为此“ build.gradle”文件可能存在一些问题。但这没有给出任何错误。
您也可以查看布局文件。 这个main.xml文件:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvContacts"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
book_list_item.xml文件为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/book_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textSize="10sp"
/>
</LinearLayout>
谢谢..