gpt4 book ai didi

java - 按整数排序列表

转载 作者:太空狗 更新时间:2023-10-29 16:26:43 25 4
gpt4 key购买 nike

我从我的数据库中获取纬度和经度,并在 RecyclerView 适配器中计算两个人之间的距离。

我想按距离对这些项目进行排序,但我不知道该怎么做。

我的适配器:

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder>{

Context context;
List<HomeGetter> homeGetters;
HomeCallback homeCallback;

public HomeAdapter(Context context, List<HomeGetter> homeGetters, HomeCallback homeCallback) {
this.context = context;
this.homeGetters = homeGetters;
this.homeCallback = homeCallback;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_home_profil,parent,false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
calculate_age calculate_age = new calculate_age();
final HomeGetter homeGetter = homeGetters.get(position);



String[] splittedAge = homeGetter.getBirthday().split("\\.");
try {
String age = calculate_age.getAge(context,Integer.valueOf(splittedAge[0]),Integer.valueOf(splittedAge[1]),Integer.valueOf(splittedAge[2]));
String username = homeGetter.getUsername().substring(0,1).toUpperCase() + homeGetter.getUsername().substring(1);
holder.textViewUsername.setText(username + "," + age);
} catch (ParseException e) {
e.printStackTrace();
}

Picasso.with(context).load(homeGetter.getImageURL()).resize(600,700).centerCrop().into(holder.imageView);

holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
homeCallback.getPosition(position);
}
});

holder.textViewLocation.setText(homeGetter.getLocation());

if(homeGetter.getStatus().equals("0")){
holder.status.setImageDrawable(context.getResources().getDrawable(R.drawable.offline));
}else {
holder.status.setImageDrawable(context.getResources().getDrawable(R.drawable.online));
}



}

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

public class ViewHolder extends RecyclerView.ViewHolder {

ImageView imageView;
TextView textViewUsername;
CardView cardView;
TextView textViewLocation;
ImageView status;

public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageViewThumb);
textViewUsername = itemView.findViewById(R.id.textViewProfilUsername);
cardView = itemView.findViewById(R.id.cardViewProfil);
textViewLocation = itemView.findViewById(R.id.textViewLocation);
status = itemView.findViewById(R.id.imageViewOnlinestatus);
}

}
}

在这条线上,我得到以公里为单位的距离:

holder.textViewLocation.setText(homeGetter.getLocation());

例如 544

是否可以在适配器中对列表进行排序?或者我该如何排序?那么,我该如何实现呢?


Activity

pullData.getData(USERNAME, "1", new Pullcallback() {
@Override
public void getSingleData(String data) {

}

@Override
public void getMultipleData(String[] multipledatas) {
GENDER = multipledatas[5];
GENDERSEARCH = multipledatas[6];

GetStrangers getStrangers = new GetStrangers(Home.this);
Map<String,String> map = new HashMap<>();
map.put("USERNAME",USERNAME);
map.put("GENDER",GENDER);
map.put("SEARCH",GENDERSEARCH);
getStrangers.strangers(map, "7", new Pullcallback() {
@Override
public void getSingleData(String data) {

}

@Override
public void getMultipleData(final String[] multipledatas) {

// username,imagelink,position,birthday
// imageURL,username,birthday,location

pullData.getData(USERNAME, "2", new Pullcallback() {
@Override
public void getSingleData(String data) {

}

@Override
public void getMultipleData(String[] multipledatas2) {

String[] split1 = multipledatas2[1].split(",");
String[] split2 = multipledatas[2].split(",");

float lat1 = Float.valueOf(split1[0]);
float lng1 = Float.valueOf(split1[1]);

float lat2 = Float.valueOf(split2[0]);
float lng2 = Float.valueOf(split2[1]);

Collections.sort(getterList, new Comparator<HomeGetter>() {
public int compare(HomeGetter s1, HomeGetter s2) {
return Integer.compare(Integer.parseInt(s1.getLocation()), Integer.parseInt(s2.getLocation()));
}
});

homeGetter = new HomeGetter(multipledatas[1],multipledatas[0],multipledatas[3],String.valueOf(Math.round(Calculator.calculateDistance(lat1,lng1,lat2,lng2))),multipledatas[4]);

getterList.add(homeGetter);

homeAdapter.notifyDataSetChanged();

loading.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
}

@Override
public void onError(String errormessage) {

}
});
}

@Override
public void onError(String errormessage) {

}
});
}

@Override
public void onError(String errormessage) {

}
});

pullData.getData(USERNAME, "2", new Pullcallback() {
@Override
public void getSingleData(String data) {

}

@Override
public void getMultipleData(String[] multipledatas) {

if(multipledatas[7].equals("0")){

Intent intentFirststep = new Intent(Home.this,Firststep.class);
startActivity(intentFirststep);
finish();
}else {

}
}

@Override
public void onError(String errormessage) {

}
});


recyclerViewHome = findViewById(R.id.recyclerViewHome);
homeAdapter = new HomeAdapter(Home.this, getterList, new HomeCallback() {
@Override
public void getPosition(int position) {
Intent stranger = new Intent(Home.this,Stranger.class);
stranger.putExtra("USERNAME",getterList.get(position).getUsername());
startActivity(stranger);
finish();
}
});
recyclerViewHome.setHasFixedSize(true);
recyclerViewHome.setLayoutManager(new GridLayoutManager(Home.this,3));
recyclerViewHome.setItemAnimator(new DefaultItemAnimator());
recyclerViewHome.setAdapter(homeAdapter);

}

但它无法正常工作,它显示 4,14,507 和 12但它应该排序 4,12,14,507

最佳答案

在传递 ArrayList 的 Activity 中,只需在传递之前执行此操作。

Collections.sort(homeGetter, new Comparator<HomeGetter>() {
public int compare(HomeGetter s1, HomeGetter s2) {
return Integer.compare(Integer.parseInt(s1.getLocation()), Integer.parseInt(s2.getLocation()));
}
});

附言如果您已经存储为整数,请不要使用解析。乐于助人。

编辑:放在这里-

getterList.add(homeGetter);

Collections.sort(homeGetter, new Comparator<HomeGetter>() {
public int compare(HomeGetter s1, HomeGetter s2) {
return Integer.compare(Integer.parseInt(s1.getLocation()), Integer.parseInt(s2.getLocation()));
}
});

homeAdapter.notifyDataSetChanged();

关于java - 按整数排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48730557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com