在循环内对 Recyclerview 适配器进行排序
Sort Recycleview adapter inside a loop
我正在制作一个聊天应用程序,显示我和每个用户之间的距离。
看起来如下:
有没有一种方法可以对这个列表进行排序,使最近的排在最前面,最远的排在最后?现在它根据添加到适配器的顺序添加。
我的适配器代码如下:
public class MapPersonAdapter extends RecyclerView.Adapter<MapPersonAdapter.MyViewHolder> {
private View mView;
private List<MapPerson> personsList;
private FirebaseAuth auth;
StorageReference storageRef;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView personName, personDistance;
public ImageView personProfile;
public ImageButton personChat;
public MyViewHolder(View view){
super(view);
personName = (TextView) view.findViewById( R.id.tvPersonName );
personDistance = (TextView) view.findViewById( R.id.tvDistance );
personProfile = (ImageView) view.findViewById( R.id.ivPersonPic );
personChat = (ImageButton) view.findViewById( R.id.imageChat );
mView = view;
}
}
public MapPersonAdapter(List<MapPerson> personsList){
this.personsList = personsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.activity_map_person,parent,false);
return new MyViewHolder( itemView );
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MapPerson mapPerson = personsList.get( position );
String ID = mapPerson.getPersonID();
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference username = db.collection( "Users" ).document( ID ).collection( "UserData" );
username.get().addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
holder.personName.setText( document.getString( "username" ) );
}
} else {
Log.d( "Error", "Error getting documents: ", task.getException() );
}
}
} );
holder.personDistance.setText( mapPerson.getPersonDistance() + " km" );
@Override
public int getItemCount(){
return personsList.size();
}
}
我问的是我是否可以根据 mapPerson.getPersonDistance()
的值进行排序。
此适配器在循环内调用:
for (String PersonID : PeopleAround) {
DocumentReference docRef = db.collection( "Users" ).document(PersonID).collection( "Location" ).document(PersonID);
docRef.get().addOnCompleteListener( new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot doc2 = task.getResult();
float distance = CalculateDistance(Lat, Lon);
if (!PersonID.equals( auth.getUid() )) {
if (distance <= Radius) {
MapPerson mapPerson = new MapPerson( PersonID, new DecimalFormat( "##.##" ).format( distance ) );
personList.add( mapPerson );
mapPersonAdapter.notifyDataSetChanged();
}
}
}
}
} );
}
在调用mapPersonAdapter.notifyDataSetChanged();
之前,根据距离排序personList,使用Comparator然后调用notifyDataSetChanged()
Collections.sort(personsList, new Comparator< MapPerson >() {
public int compare(MapPerson o1, MapPerson o2) {
// compare two instance of `Score` and return `int` as result.
return o2.getDistance().compareTo(o1.getDistance());
}
});
我正在制作一个聊天应用程序,显示我和每个用户之间的距离。
看起来如下:
有没有一种方法可以对这个列表进行排序,使最近的排在最前面,最远的排在最后?现在它根据添加到适配器的顺序添加。
我的适配器代码如下:
public class MapPersonAdapter extends RecyclerView.Adapter<MapPersonAdapter.MyViewHolder> {
private View mView;
private List<MapPerson> personsList;
private FirebaseAuth auth;
StorageReference storageRef;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView personName, personDistance;
public ImageView personProfile;
public ImageButton personChat;
public MyViewHolder(View view){
super(view);
personName = (TextView) view.findViewById( R.id.tvPersonName );
personDistance = (TextView) view.findViewById( R.id.tvDistance );
personProfile = (ImageView) view.findViewById( R.id.ivPersonPic );
personChat = (ImageButton) view.findViewById( R.id.imageChat );
mView = view;
}
}
public MapPersonAdapter(List<MapPerson> personsList){
this.personsList = personsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.activity_map_person,parent,false);
return new MyViewHolder( itemView );
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MapPerson mapPerson = personsList.get( position );
String ID = mapPerson.getPersonID();
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference username = db.collection( "Users" ).document( ID ).collection( "UserData" );
username.get().addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
holder.personName.setText( document.getString( "username" ) );
}
} else {
Log.d( "Error", "Error getting documents: ", task.getException() );
}
}
} );
holder.personDistance.setText( mapPerson.getPersonDistance() + " km" );
@Override
public int getItemCount(){
return personsList.size();
}
}
我问的是我是否可以根据 mapPerson.getPersonDistance()
的值进行排序。
此适配器在循环内调用:
for (String PersonID : PeopleAround) {
DocumentReference docRef = db.collection( "Users" ).document(PersonID).collection( "Location" ).document(PersonID);
docRef.get().addOnCompleteListener( new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot doc2 = task.getResult();
float distance = CalculateDistance(Lat, Lon);
if (!PersonID.equals( auth.getUid() )) {
if (distance <= Radius) {
MapPerson mapPerson = new MapPerson( PersonID, new DecimalFormat( "##.##" ).format( distance ) );
personList.add( mapPerson );
mapPersonAdapter.notifyDataSetChanged();
}
}
}
}
} );
}
在调用mapPersonAdapter.notifyDataSetChanged();
之前,根据距离排序personList,使用Comparator然后调用notifyDataSetChanged()
Collections.sort(personsList, new Comparator< MapPerson >() {
public int compare(MapPerson o1, MapPerson o2) {
// compare two instance of `Score` and return `int` as result.
return o2.getDistance().compareTo(o1.getDistance());
}
});