Java 如何在android Studio中对Recylcerview中的名称应用字母排序

Java 如何在android Studio中对Recylcerview中的名称应用字母排序,java,android,firebase,android-studio,Java,Android,Firebase,Android Studio,我有一个学生跟踪应用程序,它使用firebase作为后端。我现在想做的是,单击Order students按钮后,能够按姓名对学生进行排序。现在我有一个方法,可以使用按钮上附带的比较器对学生的姓名进行排序。我验证了它实际上是在排序完成后通过附加println语句进行排序的。现在,我试图找出如何将此逻辑应用于显示学生图像和姓名的RecyclerView。我将在下面发布相关代码。谢谢大家 所以流看起来是这样的 编辑 我已经在下面发布了RecyclerAdapter类的代码 activity\u vi

我有一个学生跟踪应用程序,它使用
firebase
作为后端。我现在想做的是,单击
Order students
按钮后,能够按姓名对学生进行排序。现在我有一个方法,可以使用按钮上附带的
比较器对学生的姓名进行排序。我验证了它实际上是在排序完成后通过附加println语句进行排序的。现在,我试图找出如何将此逻辑应用于显示学生图像和姓名的RecyclerView。我将在下面发布相关代码。谢谢大家

所以流看起来是这样的

编辑 我已经在下面发布了RecyclerAdapter类的代码

activity\u view\u students.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=".ViewStudents">

    <Button
        android:id="@+id/addStudentButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="Add a Student"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

    </androidx.recyclerview.widget.RecyclerView>

    <Button
        android:id="@+id/orderStudents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="orderStudents"
        android:text="Order Students"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecycleAdapter.class

public class ViewStudents extends AppCompatActivity {


    RecyclerView recyclerView;
    Button addStudent;


    private DatabaseReference myRef;

    public ArrayList<Students> students;
    private RecyclerAdapter recyclerAdapter;
    private Button orderStudents;

    private EditText mEditTextAge;
    private EditText mEditTextAssignment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_students);

        recyclerView = findViewById(R.id.recyclerView);
        addStudent = findViewById(R.id.addStudentButton);
        mEditTextAge = findViewById(R.id.EditTextAge);
        mEditTextAssignment = findViewById(R.id.EditTextAssignment);
        orderStudents = findViewById(R.id.orderStudents);

        addStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ViewStudents.this, AddStudent.class));
            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setHasFixedSize(true);

        myRef = FirebaseDatabase.getInstance().getReference();

        students = new ArrayList<>();

        ClearAll();

        GetDataFromFirebase();



    }
   // This method will make a call to firebase and get names and 
   // images of the students in the db
    private void GetDataFromFirebase() {
        Query query = myRef.child("student");

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ClearAll();
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Students student = new Students();
                    if (snapshot.child("url").getValue() == null) {
                        student.setImageUrl(snapshot.child("imageUrl").getValue().toString());
                    }
                    else {
                        student.setImageUrl(snapshot.child("url").getValue().toString());

                    }
//                    student.setAge(mEditTextAge.getText().toString());
//                    student.setAssignment(mEditTextAssignment.getText().toString().trim());
                    student.setName(snapshot.child("name").getValue().toString());
                    students.add(student);
                }
                recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
                recyclerView.setAdapter(recyclerAdapter);
                recyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
    // will clear any pre-existing data 
    private void ClearAll() {
        if (students != null) {
            students.clear();

            if(recyclerAdapter != null) {
                recyclerAdapter.notifyDataSetChanged();
            }
        }
        students = new ArrayList<>();
    }

   // method to order students by name
    public void orderStudents(View view) {
        Intent orderIntent = new Intent(this, ViewStudents.class);
        startActivity(orderIntent);
        orderStudents.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.sort(students, new Comparator<Students>() {
                    @Override
                    public int compare(Students o1, Students o2) {
                        return o1.name.compareTo(o2.name);
                    }
                });
            }
        });

        for(int i = 0; i < students.size(); i++){
            System.out.println("Students ==> " + students.get(i).getName());
        }

    }
package com.example.studenttracker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private OnItemClickListener mListener;
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
    private static final String Tag = "RecyclerView";
    private Context mContext;
    private ArrayList<Students> studentsArrayList;

    public RecyclerAdapter(Context mContext, ArrayList<Students> studentsArrayList) {
        this.mContext = mContext;
        this.studentsArrayList = studentsArrayList;
    }

    @NonNull
    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.student_item,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //TextView
        holder.textView.setText(studentsArrayList.get(position).getName());

        Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        
                    }
                }
            });
        }
    }
}
package com.example.studenttracker;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.ImageView;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.bumptech.glide.glide;
导入java.util.ArrayList;
公共类RecyclerAdapter扩展了RecyclerView.Adapter{
私人监听者;
公共接口侦听器{
无效单击(内部位置);
}
公共void setOnItemClickListener(OnItemClickListener侦听器){
mListener=监听器;
}
私有静态最终字符串Tag=“RecyclerView”;
私有上下文;
私立ArrayList学生名单;
公共回收器适配器(上下文mContext、ArrayList studentsArrayList){
this.mContext=mContext;
this.studentsArrayList=studentsArrayList;
}
@非空
@凌驾
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
View=LayoutInflater.from(parent.getContext())
.充气(右布局、学生项、家长、假);
返回新的ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
//文本视图
holder.textView.setText(studentsArrayList.get(position.getName());
Glide.with(mContext).load(studentsArrayList.get(position.getImageUrl())到(holder.imageView)中;
}
@凌驾
public int getItemCount(){
return studentsArrayList.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
图像视图图像视图;
文本视图文本视图;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
imageView=itemView.findViewById(R.id.imageView);
textView=itemView.findViewById(R.id.textView);
itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mListener!=null){
int position=getAdapterPosition();
}
}
});
}
}
}

将您的RecyclerAdapter更改为扩展
ListAdapter
,然后您可以让您的
orderStudents
单击方法修改适配器中的列表,例如:

// method to order students by name
public void orderStudents(View view) {
    Intent orderIntent = new Intent(this, ViewStudents.class);
    startActivity(orderIntent);
    orderStudents.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            List<Students> l = recyclerAdapter.getCurrentList();
            Collections.sort(l, new Comparator<Students>() {
                @Override
                public int compare(Students o1, Students o2) {
                    return o1.name.compareTo(o2.name);
                }
            });
            recyclerAdapter.submitList( l );
        }
    });
//按姓名对学生排序的方法
公共学生(视图){
Intent orderIntent=新的Intent(这个,viewstustudents.class);
startActivity(orderIntent);
orderStudents.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
List l=recyclerAdapter.getCurrentList();
Collections.sort(l,新的Comparator(){
@凌驾
公共整数比较(学生o1,学生o2){
返回o1.name.compareTo(o2.name);
}
});
回收器适配器提交列表(l);
}
});

将您的RecyclerAdapter更改为扩展
ListAdapter
,然后您可以让您的
orderStudents
单击方法修改适配器中的列表,例如:

// method to order students by name
public void orderStudents(View view) {
    Intent orderIntent = new Intent(this, ViewStudents.class);
    startActivity(orderIntent);
    orderStudents.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            List<Students> l = recyclerAdapter.getCurrentList();
            Collections.sort(l, new Comparator<Students>() {
                @Override
                public int compare(Students o1, Students o2) {
                    return o1.name.compareTo(o2.name);
                }
            });
            recyclerAdapter.submitList( l );
        }
    });
//按姓名对学生排序的方法
公共学生(视图){
Intent orderIntent=新的Intent(这个,viewstustudents.class);
startActivity(orderIntent);
orderStudents.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
List l=recyclerAdapter.getCurrentList();
Collections.sort(l,新的Comparator(){
@凌驾
公共整数比较(学生o1,学生o2){
返回o1.name.compareTo(o2.name);
}
});
回收器适配器提交列表(l);
}
});

Post your
RecyclerAdapter
code,您可能可以在那里的某个地方对列表进行排序,但不知道您是如何构建的,我无法建议在哪里。我已经发布了
RecyclerAdapter
Post your
RecyclerAdapter
代码的代码,您可能可以在那里的某个地方对列表进行排序,但不知道您是如何创建的我已经发布了
RecylcAdapter
的代码,我想点击按钮
Order Students
,然后按名称排序。不是在它打电话从firebase获取数据时。啊,好的,更改了我的答案。扩展
ListAdapter
将允许您获取并设置列表适配器正在使用。非常感谢@Ralgha你救了我一天!我想单击按钮
为学生排序
,然后按名称排序。不是在它打电话从firebase获取数据时。啊,好的,更改了我的答案。扩展
列表适配器
将允许你获取和设置适配器正在使用的列表。Tha谢谢你@Ra