我尝试在Android中使用 RecyclerView 创建列表。但是,我不明白为什么RecyclerView 无法正确加载数据。我试图将我的代码与在线提供的示例进行比较,但我无法分辨示例代码和代码之间的区别。
任何人都可以使用我的代码帮助指出遗漏或错误吗?
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="daozui.assignment3_task3.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
contact_arrangement.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/contactIcon_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/man"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/contactName_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="30sp"
android:text="Name"
android:layout_toEndOf="@id/contactIcon_ID"
android:layout_marginTop="25dp"
android:layout_marginStart="25dp"/>
<TextView
android:id="@+id/contactRelationship_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFBEB9B9"
android:textSize="20sp"
android:text="Relationship"
android:layout_alignStart="@id/contactName_ID"
android:layout_below="@id/contactName_ID"/>
</RelativeLayout>
MainActivity.Java
package daozui.assignment3_task3;
import android.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView theRecyclerView;
List<Contact> contactList;
ContactAdapter theAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createList();
theRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
theRecyclerView.setLayoutManager(new LinearLayoutManager(this));
theAdapter = new ContactAdapter(contactList);
theRecyclerView.setAdapter(theAdapter);
}
private void createList() {
contactList = new ArrayList<Contact>();
contactList.add(new Contact("Alex", "male", "Friends", "0123456789"));
contactList.add(new Contact("Mona", "female", "Friends", "9876543210"));
}
}
ContactAdapter.Java
package daozui.assignment3_task3;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.theViewHolder> {
private List<Contact> contacList;
public ContactAdapter(List<Contact> ContactList) {
this.contacList = ContactList;
}
@Override
public ContactAdapter.theViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_arrangement, parent, false);
return new theViewHolder(itemView);
}
@Override
public void onBindViewHolder(theViewHolder holder, int position) {
Contact contact = contacList.get(position);
holder.contactName.setText(contact.getName());
holder.contactRelationship.setText(contact.getRelationship());
if (contact.getGender().equals("male")) {
holder.contactIcon.setImageResource(R.drawable.man);
} else {
holder.contactIcon.setImageResource(R.drawable.woman);
}
}
@Override
public int getItemCount() {
return 0;
}
public static class theViewHolder extends RecyclerView.ViewHolder {
public TextView contactName, contactRelationship;
public ImageView contactIcon;
public theViewHolder(View itemView) {
super(itemView);
contactIcon = (ImageView) itemView.findViewById(R.id.contactIcon_ID);
contactName = (TextView) itemView.findViewById(R.id.contactName_ID);
contactRelationship = (TextView) itemView.findViewById(R.id.contactRelationship_ID);
// itemView.setOnClickListener(this);
}
// @Override
// public void onClick(View view)
// {
// int pos= getAdapterPosition();
// Toast.makeText(itemView.getContext(),contacList.get(pos).getContactNumber(),Toast.LENGTH_LONG);
// }
}
}
Contact.Java
package daozui.assignment3_task3;
public class Contact {
private String name;
private String gender;
private String relationship;
private String contactNumber;
public Contact(String Name, String Gender, String Relationship, String ContactNumber) {
this.name = Name;
this.gender = Gender;
this.relationship = Relationship;
this.contactNumber = ContactNumber;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getRelationship() {
return relationship;
}
public String getContactNumber() {
return contactNumber;
}
}
答案 0 :(得分:4)
问题在于:
@Override
public int getItemCount() {
return 0;
}
您应该返回列表的大小:
@Override
public int getItemCount() {
return contactList.size();
}
答案 1 :(得分:1)
您应该在此处返回您的收藏品的大小:
@Override
public void getItemCount(){
return contactList.size();
}
getItemCount()返回适配器所拥有的数据集中的项目总数。
getItemCount()返回的int值是RecyclerView要从您的集合中查找要绑定的数据的次数。
答案 2 :(得分:0)
通知您的适配器数据已更新。!
notifyDataSetChanged();
@Override
public int getItemCount() {
return contactList.size();
}
答案 3 :(得分:0)
如果列表为空,使用这个简单的三元来避免空指针异常;
@Override
public int getItemCount() {
return contactList == null ? 0 : contactList.size();
}