RecyclerView没有显示任何项目。

huangapple go评论76阅读模式
英文:

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&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();
contacts.add(new Contact(&quot;Tony Soprano&quot;, &quot;tsoprano@gmail.com&quot;, &quot;https://i.pinimg.com/originals/13/a9/41/13a9416d23220f57191bbdb9a0eab3aa.jpg&quot;));
contacts.add(new Contact(&quot;Silvio Dante&quot;, &quot;sdante@gmail.com&quot;, &quot;https://i1.wp.com/bamfstyle.com/wp-content/uploads/2019/11/sop603silv-main1.jpg?ssl=1&quot;));
contacts.add(new Contact(&quot;Paulie Gualtieri&quot;, &quot;pgualtieri@gmail.com&quot;, &quot;http://img2.wikia.nocookie.net/__cb20110422182403/sopranos/images/c/c5/Paulie1.jpg&quot;));
contacts.add(new Contact(&quot;Salvatore Bonpensiero&quot;, &quot;sbonpensiero@gmail.com&quot;, &quot;https://static1.srcdn.com/wordpress/wp-content/uploads/2021/09/Big-Pussy-in-The-Sopranos.jpg&quot;));
contacts.add(new Contact(&quot;Christopher Moltisanti&quot;, &quot;cmoltisanti@gmail.com&quot;, &quot;https://s-media-cache-ak0.pinimg.com/originals/9b/bd/6d/9bbd6d2c6cd5c285f6d537e82fcbea80.jpg&quot;));
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&lt;ContactsRecViewAdapter.ViewHolder&gt;{
private ArrayList&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();
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&lt;Contact&gt; 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 &quot;Contact{&quot; +
&quot;name=&#39;&quot; + name + &#39;\&#39;&#39; +
&quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
&quot;, imageUrl=&#39;&quot; + imageUrl + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}

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();
}

huangapple
  • 本文由 发表于 2023年5月13日 08:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定