RecyclerView没有显示任何数据。

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

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;android.support.v7.widget.RecyclerView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/my_ecycler_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;/android.support.v7.widget.RecyclerView&gt;

text_for_recycler.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;8dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/my_text_view1&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/my_text_view_1&quot;
android:textSize=&quot;24sp&quot;
android:textStyle=&quot;bold&quot;
android:textColor=&quot;@color/colorPrimaryDark&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/my_text_view2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/my_text_view_2&quot;
android:textSize=&quot;24sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/my_text_view3&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/my_text_view_3&quot;
android:textSize=&quot;24sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;/LinearLayout&gt;

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&lt;Names&gt; namesList = new ArrayList&lt;&gt;();
@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(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
namesList.add(names1);
names1 = new Names(&quot;Mohamed&quot;, &quot;Sabry&quot;, &quot;Eltabie&quot;);
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&lt;MyAdapter.MyViewHolder&gt; {
private List&lt;Names&gt; 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&lt;Names&gt; 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

你在设置适配器之后填充了数据源。有两种方法可以做到这一点。

  1. 在设置适配器之前填充数据源。
  2. 在填充数据源后调用myAdapter.notifyDataSetChanged()
英文:

You populated your data source after you set the adapter. There are 2 ways to do it.

  1. Populate the data source before setting the adapter
  2. call myAdapter.notifyDataSetChanged() after the population of the data source

huangapple
  • 本文由 发表于 2020年7月24日 21:35:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63074664.html
匿名

发表评论

匿名网友

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

确定