英文:
RecyclerView isn't displaying any items
问题
My recyclerView isn't displaying any items. I'm not getting any errors though so I'm not sure what the problem is. Here is the code.
MainActivity.java
package com.example.recyclerviewexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView contactsRecView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsRecView = findViewById(R.id.contactsRecView);
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Tony Soprano", "tsoprano@gmail.com", "https://i.pinimg.com/originals/13/a9/41/13a9416d23220f57191bbdb9a0eab3aa.jpg"));
contacts.add(new Contact("Silvio Dante", "sdante@gmail.com", "https://i1.wp.com/bamfstyle.com/wp-content/uploads/2019/11/sop603silv-main1.jpg?ssl=1"));
contacts.add(new Contact("Paulie Gualtieri", "pgualtieri@gmail.com", "http://img2.wikia.nocookie.net/__cb20110422182403/sopranos/images/c/c5/Paulie1.jpg"));
contacts.add(new Contact("Salvatore Bonpensiero", "sbonpensiero@gmail.com", "https://static1.srcdn.com/wordpress/wp-content/uploads/2021/09/Big-Pussy-in-The-Sopranos.jpg"));
contacts.add(new Contact("Christopher Moltisanti", "cmoltisanti@gmail.com", "https://s-media-cache-ak0.pinimg.com/originals/9b/bd/6d/9bbd6d2c6cd5c285f6d537e82fcbea80.jpg"));
ContactsRecViewAdapter adapter = new ContactsRecViewAdapter();
adapter.setContacts(contacts);
contactsRecView.setAdapter(adapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
contactsRecView.setLayoutManager(linearLayoutManager);
}
}
ContactsRecViewAdapter.java
package com.example.recyclerviewexample;
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.ArrayList;
public class ContactsRecViewAdapter extends RecyclerView.Adapter<ContactsRecViewAdapter.ViewHolder> {
private ArrayList<Contact> contacts = new ArrayList<>();
public ContactsRecViewAdapter() {
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.txtName.setText(contacts.get(position).getName());
}
@Override
public int getItemCount() {
return contacts.size();
}
public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
}
}
}
Contact.java
package com.example.recyclerviewexample;
public class Contact {
private String name;
private String email;
private String imageUrl;
public Contact(String name, String email, String imageUrl) {
this.name = name;
this.email = email;
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public String toString() {
return "Contact{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", imageUrl='" + imageUrl + '\'' +
'}';
}
}
I am using the codeacademy tutorial on android studio and followed the section of recyclerView exactly to get this but it didn't display any items in the app.
英文:
My recyclerView isn't displaying any items. I'm not getting any errors though so I'm not sure what the problem is. Here is the code.
MainActivity.java
package com.example.recyclerviewexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView contactsRecView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsRecView = findViewById(R.id.contactsRecView);
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("Tony Soprano", "tsoprano@gmail.com", "https://i.pinimg.com/originals/13/a9/41/13a9416d23220f57191bbdb9a0eab3aa.jpg"));
contacts.add(new Contact("Silvio Dante", "sdante@gmail.com", "https://i1.wp.com/bamfstyle.com/wp-content/uploads/2019/11/sop603silv-main1.jpg?ssl=1"));
contacts.add(new Contact("Paulie Gualtieri", "pgualtieri@gmail.com", "http://img2.wikia.nocookie.net/__cb20110422182403/sopranos/images/c/c5/Paulie1.jpg"));
contacts.add(new Contact("Salvatore Bonpensiero", "sbonpensiero@gmail.com", "https://static1.srcdn.com/wordpress/wp-content/uploads/2021/09/Big-Pussy-in-The-Sopranos.jpg"));
contacts.add(new Contact("Christopher Moltisanti", "cmoltisanti@gmail.com", "https://s-media-cache-ak0.pinimg.com/originals/9b/bd/6d/9bbd6d2c6cd5c285f6d537e82fcbea80.jpg"));
ContactsRecViewAdapter adapter = new ContactsRecViewAdapter();
adapter.setContacts(contacts);
contactsRecView.setAdapter(adapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
contactsRecView.setLayoutManager(linearLayoutManager);
}
}
ContactsRecViewAdapter.java
package com.example.recyclerviewexample;
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.ArrayList;
public class ContactsRecViewAdapter extends RecyclerView.Adapter<ContactsRecViewAdapter.ViewHolder>{
private ArrayList<Contact> contacts = new ArrayList<>();
public ContactsRecViewAdapter() {
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.txtName.setText(contacts.get(position).getName());
}
@Override
public int getItemCount() {
return 0;
}
public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
}
}
}
Contact.java
package com.example.recyclerviewexample;
public class Contact {
private String name;
private String email;
private String imageUrl;
public Contact(String name, String email, String imageUrl) {
this.name = name;
this.email = email;
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public String toString() {
return "Contact{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", imageUrl='" + imageUrl + '\'' +
'}';
}
}
I am using the codeacademy tutorial on android studio and followed the section of recyclerView exactly to get this but it didn't display any items in the app.
答案1
得分: 1
在你的 ContactsRecViewAdapter
中,你将 getItemCount
返回为 0
,这告诉了适配器需要渲染多少个项目。
@Override
public int getItemCount() {
return contacts.size();
}
英文:
In your ContactsRecViewAdapter
you are returning getItemCount
as 0
, which tells recycler how many items to render
@Override
public int getItemCount() {
return contacts.size();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论