RecycleView不显示任何数据

时间:2020-07-24 13:35:11

标签: java android android-studio android-layout android-recyclerview

我是编程和尝试在android Studio中编写一个简单的RecyclerView的新手。但是,当我运行此代码时,屏幕上什么都没有出现。

请帮我提一些建议,因为它将帮助我清楚地了解这一点。

谢谢。

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

2 个答案:

答案 0 :(得分:0)

properNames()之前将myAdapter = new MyAdapter(namesList);移动

或者,在myAdapter.notifyDataSetChanged()末尾致电properNames()

最后,如下更新Names的构造函数:

public Names(String firstName, String secondName, String thirdName) {
    this.firstName = firstName;
    this.secondName = secondName;
    this.thirdName = thirdName;
}

答案 1 :(得分:0)

设置适配器后,您便填充了数据源。有两种方法可以实现。

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