如何按最近位置对列表视图进行排序?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何按最近位置对列表视图进行排序?相关的知识,希望对你有一定的参考价值。

我将从firebase中对我的位置列表进行排序。列表应从距离当前位置的最近位置排列。如何按最近位置对列表视图进行排序?

这是我的适配器

public class AdminImageListAdapter extends ArrayAdapter <AdminImageUpload> {
private Activity context;
private int resource;
private List<AdminImageUpload> adlistImage;

public AdminImageListAdapter(@NonNull Activity context, @LayoutRes int resource, @NonNull List<AdminImageUpload> objects) {
    super(context, resource, objects);
    this.context = context;
    this.resource = resource;
    adlistImage = objects;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View v = inflater.inflate(resource, null);
    TextView adName = (TextView) v.findViewById(adImageName);
    TextView adAddress = (TextView) v.findViewById(adImageAddress);
    ImageView img = (ImageView) v.findViewById(R.id.adimgView);

    adName.setText(adlistImage.get(position).getName());
    adAddress.setText(adlistImage.get(position).getAddress());
    Glide.with(context).load(adlistImage.get(position).getUrl()).into(img);

    return v;

}

这是我的Listview

public class AirConditioningActivity extends AppCompatActivity {

private DatabaseReference mDatabaseRef;
private List<AdminImageUpload> adimgList;
private ListView lvl;
private AdminImageListAdapter adapter;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_air_conditioning);
    adimgList = new ArrayList<>();
    lvl= (ListView) findViewById(R.id.listViewImage);
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait loading list image...");
    progressDialog.show();
    firebaseAuth = FirebaseAuth.getInstance();
    mDatabaseRef = FirebaseDatabase.getInstance().getReference(AdminKeyWorkshopsActivity.FB_DATABASE_PATH);
    Query mQuery = mDatabaseRef.orderByChild("category").equalTo("AIR-CONDITIONING");
    mQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            progressDialog.dismiss();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                AdminImageUpload image = snapshot.getValue(AdminImageUpload.class);
                adimgList.add(image);
            }
            adapter = new AdminImageListAdapter(AirConditioningActivity.this, R.layout.admin_image_item, adimgList);
            lvl.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            progressDialog.dismiss();
        }
    });

    lvl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            AdminImageUpload adimgList = (AdminImageUpload) adapterView.getAdapter().getItem(i);
            Intent intent = new Intent(AirConditioningActivity.this, AdminDetailsOfWorkshops.class);

            String name = adimgList.getName();
            String address = adimgList.getAddress();
            String contact = adimgList.getContact();
            String category = adimgList.getCategory();
            String url = adimgList.getUrl();

            intent.putExtra("name", name);
            intent.putExtra("address", address);
            intent.putExtra("contact", contact);
            intent.putExtra("category", category);
            intent.putExtra("URL", url);
            startActivity(intent);
        }
    });
}
答案

你可以使用浮动distanceTo方法,然后在结果上使用Collection.sort来获得最近的距离。

以上是关于如何按最近位置对列表视图进行排序?的主要内容,如果未能解决你的问题,请参考以下文章

如何按降序对列表视图项进行排序

如何按日期和时间对 jsonarray 数据进行排序并放入列表视图适配器

按距离地图排序位置 Android

如何以字符串格式按日期对对象列表进行排序?

如何按最新消息对聊天列表进行排序?

如何按保存顺序对表格视图中的数据进行排序?