Recycler View正常工作,没有任何错误

问题描述 投票:0回答:1

我一直在尝试使用回收站视图在textviews列表中显示一些数据。但是我的代码不起作用。 logcat中没有错误消息。我过去曾经使用过回收站视图,但不知道问题出在哪里。

package com.example.diffutil;

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;
import java.util.List;

public class Adaptershow extends RecyclerView.Adapter<Adaptershow.MyViewHolder> {
ArrayList<Student> mStudents;

public Adaptershow(ArrayList<Student> mStudents) {
    this.mStudents = mStudents;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent,             false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.show.setText(mStudents.get(position).getName());
}

@Override
public int getItemCount() {
    return mStudents.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
    TextView show;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        show = itemView.findViewById(R.id.show);
    }
}
}

diffutil是项目的名称。

下面的MainActivity代码:-

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Student> students;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.rv);
    students = new ArrayList<>();
    students.add(new Student(1,"a","q"));
    students.add(new Student(1,"b","w"));
    students.add(new Student(1,"c","e"));
    students.add(new Student(1,"d","r"));
    students.add(new Student(1,"e","t"));
    Adaptershow adapter = new Adaptershow(students);
    recyclerView.setAdapter(adapter);
}
}

下面的XML代码:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"/>

</RelativeLayout>

项目XML代码:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/show"
    android:text="hello"/>

</RelativeLayout>
java android android-recyclerview recycler-adapter
1个回答
0
投票

我看不到您的recyclerview的LinearLayoutManager

..........
..........
Adaptershow adapter = new Adaptershow(students);
//here
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
© www.soinside.com 2019 - 2024. All rights reserved.