我有一个
Activity
,在RecyclerView
中有一个layout
,我想用layout
在这个RecyclerView
中显示一些数据,问题是当我输入Activity
数据时不可见并且为空。数据是在名为 java class
的 "recyclerViewModel"
的帮助下收集的,这里我设置了一个带有 6 variables
的构造函数,然后我在 activity
代码中创建了一个 Arraylist
class
我用一个objects
和for
arrays
快速创建了总共3个(setDataRecycler method).
,这3个objects
每一个都有6个variables
,每个对象都意味着“一个配置文件” 。 objects
保存在 ArrayList
中,目的是在 RecyclerView
中显示它们,正如我所说的
活动java代码:
package com.example.calculoimcwithui;
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 Profiles extends AppCompatActivity {
ArrayList<recyclerViewModel> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profiles);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
data = new ArrayList<recyclerViewModel>();
setDataRecycler();
recyclerViewAdapter adapter = new recyclerViewAdapter(this,data);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void setDataRecycler () {
String [] nombres = {"Rober","Carlos","Laura"};
String [] edades = {"16","16","21"};
String [] alturas = {"178","180","162"};
String [] pesos = {"72","68","54"};
String [] afs = {"Bastante","Mucha","Bastante"};
String [] generos = {"Hombre","Hombre","Mujer"};
for (int i = 0; i<nombres.length;i++) {
data.add(new recyclerViewModel(nombres[i],edades[i],alturas[i],pesos[i],afs[i],generos[i]));
}
}
}
活动 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Profiles">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="399dp"
android:layout_height="627dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.452" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerView适配器:
package com.example.calculoimcwithui;
import android.content.Context;
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 com.example.calculoimcwithui.R;
import com.example.calculoimcwithui.recyclerViewModel;
import java.util.ArrayList;
public class recyclerViewAdapter extends RecyclerView.Adapter<recyclerViewAdapter.ViewHolder> {
Context context;
ArrayList<recyclerViewModel> data;
public recyclerViewAdapter (Context context, ArrayList<recyclerViewModel> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public recyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.recyclerviewmodel,parent,false);
return new recyclerViewAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull recyclerViewAdapter.ViewHolder holder, int position) {
holder.name.setText(data.get(position).getName());
holder.age.setText(data.get(position).getAge());
holder.height.setText(data.get(position).getAltura());
holder.weight.setText(data.get(position).getPeso());
holder.af.setText(data.get(position).getAf());
holder.gender.setText(data.get(position).getGenero());
}
@Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView age;
TextView height;
TextView weight;
TextView af;
TextView gender;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
age = itemView.findViewById(R.id.age);
height = itemView.findViewById(R.id.height);
weight = itemView.findViewById(R.id.weight);
af = itemView.findViewById(R.id.af);
gender = itemView.findViewById(R.id.gender);
}
}
}
recyclerViewModel java 代码
package com.example.calculoimcwithui;
public class recyclerViewModel {
String name;
String age;
String altura;
String peso;
String af;
String genero;
public recyclerViewModel(String name, String age, String altura, String peso, String af, String genero) {
this.name = name;
this.age = age;
this.altura = altura;
this.peso = peso;
this.af = af;
this.genero = genero;
}
public String getName() {
return name;
}
public String getAge () {
return age;
}
public String getAltura () {
return altura;
}
public String getPeso () {
return peso;
}
public String getAf () {
return af;
}
public String getGenero () {
return genero;
}
}
RecyclerView 模型卡
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="441dp"
android:layout_height="117dp"
android:layout_marginStart="1dp"
android:layout_marginTop="245dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="486dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/recCard"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="31dp"
android:text="Nombre"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.373" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Edad"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.673" />
<TextView
android:id="@+id/af"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="156dp"
android:text="Af"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.234" />
<TextView
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="140dp"
android:text="Peso"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.673" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="136dp"
android:text="Genero"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.663" />
<TextView
android:id="@+id/height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="140dp"
android:text="Altura"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.244" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
我检查了适配器类中的
getItemCount
方法实际上返回了正确的大小,还有 width
的 height
和 RecyclerView
,并且我检查了它们不在 wrap_content
中。还有项目的数量,我检查了 xml
膨胀文件是否正确,我检查了 setLayoutManager
方法,确保已写入,我不知道我还能做什么。 (该应用程序工作正常,它不会抛出任何错误,但问题是。)