gpt4 book ai didi

java - RecyclerView未加载数据的原因

转载 作者:行者123 更新时间:2023-12-01 16:47:43 28 4
gpt4 key购买 nike

我尝试在 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;
}
}

最佳答案

问题出在这里:

  @Override
public int getItemCount() {
return 0;
}

您应该返回列表的大小:

      @Override
public int getItemCount() {
return contactList.size();
}

关于java - RecyclerView未加载数据的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46836915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com