Java 自定义比较器不';行不通
我已经写了这个代码Java 自定义比较器不';行不通,java,android,Java,Android,我已经写了这个代码 public class RecycleFrame extends Fragment { GPSTracker gps; ArrayList<Double> dLatitude = new ArrayList<>(); ArrayList<Double> dLongitude = new ArrayList<>(); ArrayList<Float> distance = new Ar
public class RecycleFrame extends Fragment {
GPSTracker gps;
ArrayList<Double> dLatitude = new ArrayList<>();
ArrayList<Double> dLongitude = new ArrayList<>();
ArrayList<Float> distance = new ArrayList<>();
ArrayList<Data> dataList = new ArrayList<>();
Timer timer;
public RecycleFrame() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_view, menu);
super.onCreateOptionsMenu(menu,inflater);
}
public void refreshFragment(){
dataList.clear();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.detach(this).attach(this).commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Integer id = item.getItemId();
if(id == R.id.action_A_Z){
//Sorts the garages from A to Z
Collections.sort(dataList, new Comparator<Data>() {
@Override
public int compare(Data name1, Data name2) {
return name1.getNames().compareTo(name2.getNames());
}
});
//Refreshes the fragment
refreshFragment();
return true;
}
else if(id == R.id.action_Z_A){
//Sorts the garages from Z to A
Collections.sort(dataList, new Comparator<Data>() {
@Override
public int compare(Data name1, Data name2) {
return name2.getNames().compareTo(name1.getNames());
}
});
//Refreshes the fragment
refreshFragment();
return true;
}
else if (id == R.id.short_distance){
Collections.sort(dataList, new Comparator<Data>() {
@Override
public int compare(Data distance1, Data distance2) {
return distance1.getDistance().compareTo(distance2.getDistance());
}
});
refreshFragment();
return true;
}
else if(id == R.id.places){
//Sorts the garages from Z to A
Collections.sort(dataList, new Comparator<Data>() {
@Override
public int compare(Data place1, Data place2) {
return place2.getSpots().compareTo(place1.getSpots());
}
});
//Refreshes the fragment
refreshFragment();
return true;
}
else if(id == R.id.refresh){
refreshFragment();
}
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
gps = new GPSTracker(getActivity());
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recycle, container, false);
final RecyclerView VRecyclerView = (RecyclerView) view.findViewById(R.id.rv_recycler_view);
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
String url= "http://test.dontstealmywag.ga/api/parkgarage_all.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
try{
JSONObject o = new JSONObject(response);
JSONArray values=o.getJSONArray("");
if(dataList.size() == 0) {
for (int i = 0; i < values.length(); i++) {
JSONObject jsonObject = values.getJSONObject(i);
// new location object
Location myLocation = new Location("");
//gets data from current location
myLocation.setLatitude(gps.getLatitude());
myLocation.setLongitude(gps.getLongitude());
// creates list of lat long data
dLatitude.add(jsonObject.getDouble("langitude"));
dLongitude.add(jsonObject.getDouble("longitude"));
// creates new location object
Location parkingGarage = new Location("");
//gets lat long from garage
parkingGarage.setLatitude(dLatitude.get(i));
parkingGarage.setLongitude(dLongitude.get(i));
//converts distance to KM
distance.add(myLocation.distanceTo(parkingGarage)/1000);
//creates custom object with all required data
dataList.add(new Data(jsonObject.getString("parkgarage_name"), jsonObject.getString("charging_capacity"), jsonObject.getDouble("langitude"),
jsonObject.getDouble("longitude"), jsonObject.getString("parkgarage_code"), distance.get(i), jsonObject.getInt("car_capacity")));
}
}
} catch (JSONException ex){}
VRecyclerView.setHasFixedSize(true);
RecycleAdapter adapter = new RecycleAdapter(dataList);
VRecyclerView.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
VRecyclerView.setLayoutManager(llm);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
rq.add(stringRequest);
return view;
}
}
public类RecycleFrame扩展片段{
全球定位系统;
ArrayList dLatitude=新的ArrayList();
ArrayList Dlongitence=新的ArrayList();
ArrayList距离=新的ArrayList();
ArrayList dataList=新的ArrayList();
定时器;
公共回收名称(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建选项菜单(菜单菜单,菜单充气机){
充气机。充气(右菜单。碎片视图,菜单);
super.onCreateOptions菜单(菜单,充气机);
}
公共无效刷新片段(){
dataList.clear();
FragmentTransaction=getFragmentManager().beginTransaction();
transaction.detach(this.attach)(this.commit();
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
整数id=item.getItemId();
if(id==R.id.action_A_Z){
//将车库从A到Z排序
Collections.sort(dataList,newcomparator(){
@凌驾
公共整数比较(数据名1,数据名2){
返回name1.getNames().compareTo(name2.getNames());
}
});
//刷新片段
刷新片段();
返回true;
}
else if(id==R.id.action_Z_A){
//将车库从Z排序到A
Collections.sort(dataList,newcomparator(){
@凌驾
公共整数比较(数据名1,数据名2){
返回name2.getNames().compareTo(name1.getNames());
}
});
//刷新片段
刷新片段();
返回true;
}
else if(id==R.id.short_distance){
Collections.sort(dataList,newcomparator(){
@凌驾
公共整数比较(数据距离1,数据距离2){
返回distance1.getDistance().compareTo(distance2.getDistance());
}
});
刷新片段();
返回true;
}
else if(id==R.id.places){
//将车库从Z排序到A
Collections.sort(dataList,newcomparator(){
@凌驾
公共整数比较(数据位置1,数据位置2){
返回place2.getSpots().compareTo(place1.getSpots());
}
});
//刷新片段
刷新片段();
返回true;
}
else if(id==R.id.refresh){
刷新片段();
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
gps=新的GP斯特拉克(getActivity());
设置选项菜单(真);
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u回收,容器,错误);
最终RecyclerView VRecyclerView=(RecyclerView)视图.findViewById(R.id.rv\u recycler\u视图);
RequestQueue rq=Volley.newRequestQueue(getActivity().getApplicationContext());
字符串url=”http://test.dontstealmywag.ga/api/parkgarage_all.php";
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//对回应做点什么
试一试{
JSONObject o=新的JSONObject(响应);
JSONArray值=o.getJSONArray(“”);
如果(dataList.size()==0){
对于(int i=0;i//Refreshes the fragment
refreshFragment();