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

时间:2017-05-14 02:47:35

标签: android sorting firebase location firebase-authentication

我要从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);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您可以使用float distanceTo方法然后使用 Collection.sort在结果上得到最近的距离。