英文:
RecycleView not showing any data
问题
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />
text_for_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">
    <TextView
        android:id="@+id/my_text_view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_1"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark" />
    <TextView
        android:id="@+id/my_text_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_2"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/my_text_view3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_3"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>
MainActivity.java
package com.example.android.myapprecyclerview7222020;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private MyAdapter myAdapter;
    private List<Names> namesList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.my_recycler_view);
        myAdapter = new MyAdapter(namesList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(myAdapter);
        prepareNames();
    }
    private void prepareNames() {
        Names names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
    }
}
MyAdapter.java
package com.example.android.myapprecyclerview7222020;
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.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Names> namesList;
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name1, name2, name3;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name1 = itemView.findViewById(R.id.my_text_view1);
            name2 = itemView.findViewById(R.id.my_text_view2);
            name3 = itemView.findViewById(R.id.my_text_view3);
        }
    }
    public MyAdapter(List<Names> namesList) {
        this.namesList = namesList;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_for_recycler, viewGroup, false);
        return new MyViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Names names = namesList.get(i);
        myViewHolder.name1.setText(names.getFirstName());
        myViewHolder.name2.setText(names.getSecondName());
        myViewHolder.name3.setText(names.getThirdName());
    }
    @Override
    public int getItemCount() {
        return namesList.size();
    }
}
Names.java
package com.example.android.myapprecyclerview7222020;
import androidx.annotation.NonNull;
public class Names {
    private String firstName, secondName, thirdName;
    public Names(String firstName, String secondName, String thirdName) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.thirdName = thirdName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(@NonNull final String firstName) {
        this.firstName = firstName;
    }
    public String getSecondName() {
        return secondName;
    }
    public void setSecondName(@NonNull final String secondName) {
        this.secondName = secondName;
    }
    public String getThirdName() {
        return thirdName;
    }
    public void setThirdName(@NonNull final String thirdName) {
        this.thirdName = thirdName;
    }
}
英文:
I am new to programming and trying to write a simple RecyclerView in android Studio to learn it. But when I run this code nothing comes up on the screen.
Please do help me with suggestions, as it will help me learn this clearly.
Thanks in advance.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="@+id/my_ecycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</android.support.v7.widget.RecyclerView>
text_for_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">
    <TextView
        android:id="@+id/my_text_view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_1"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark"/>
    <TextView
        android:id="@+id/my_text_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_2"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/my_text_view3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_text_view_3"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>
MainActivity.Java
package com.example.android.myapprecyclerview7222020;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
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 {
    private MyAdapter myAdapter;
    private List<Names> namesList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_ecycler_view);
        myAdapter = new MyAdapter(namesList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(myAdapter);
        properNames();
    }
    private void properNames() {
        Names names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
        names1 = new Names("Mohamed", "Sabry", "Eltabie");
        namesList.add(names1);
    }
}
MyAdapter.Java
package com.example.android.myapprecyclerview7222020;
import android.support.annotation.NonNull;
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.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Names> namesList;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name1, name2, name3;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name1 = (TextView) itemView.findViewById(R.id.my_text_view1);
            name2 = (TextView) itemView.findViewById(R.id.my_text_view2);
            name3 = (TextView) itemView.findViewById(R.id.my_text_view3);
        }
    }
    public MyAdapter(List<Names> namesList) {
        this.namesList = namesList;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_for_recycler, viewGroup, false);
        return new MyViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Names names = namesList.get(i);
        myViewHolder.name1.setText(names.getFirstName());
        myViewHolder.name2.setText(names.getSecondName());
        myViewHolder.name3.setText(names.getThirdName());
    }
    @Override
    public int getItemCount() {
        return namesList.size();
    }
}
Names.Java
package com.example.android.myapprecyclerview7222020;
import android.support.annotation.NonNull;
public class Names {
    private String firstName, secondName, thirdName;
    public Names(String firstName, String secondName, String thirdName) {
        firstName = this.firstName;
        secondName = this.secondName;
        thirdName = this.thirdName;
    }
    public String getFirstName(){
        return firstName;
    }
    public void setFirstName(@NonNull final String firstName) {
        this.firstName = firstName;
    }
    public String getSecondName() {
        return secondName;
    }
    public void setSecondName(@NonNull final String secondName) {
        this.secondName = secondName;
    }
    public String getThirdName() {
        return thirdName;
    }
    public void setThirdName(@NonNull final String thirdName) {
        this.thirdName = thirdName;
    }
}
答案1
得分: 0
将 properNames() 移动到 myAdapter = new MyAdapter(namesList); 之前。
或者,在 properNames() 结束时调用 myAdapter.notifyDataSetChanged()。
最后,将 Names 的构造函数更新如下:
public Names(String firstName, String secondName, String thirdName) {
    this.firstName = firstName;
    this.secondName = secondName;
    this.thirdName = thirdName;
}
英文:
Move properNames() before myAdapter = new MyAdapter(namesList);
Or,call myAdapter.notifyDataSetChanged() in the end of properNames().
And lastly, update the constructor of Names as following:
public Names(String firstName, String secondName, String thirdName) {
    this.firstName = firstName;
    this.secondName = secondName;
    this.thirdName = thirdName;
}
答案2
得分: 0
你在设置适配器之后填充了数据源。有两种方法可以做到这一点。
- 在设置适配器之前填充数据源。
- 在填充数据源后调用myAdapter.notifyDataSetChanged()。
英文:
You populated your data source after you set the adapter. There are 2 ways to do it.
- Populate the data source before setting the adapter
- call myAdapter.notifyDataSetChanged()after the population of the data source
专注分享java语言的经验与见解,让所有开发者获益!



评论