2017-04-13 52 views
0

我写这段代码自定义比较器不工作

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; 
    } 
} 

有了这个代码,我想排序“AZ”,“ZA”,“距离” &“斑点数”我的名单,但不知何故,这是行不通的。当我按下按钮排序例如“A-Z”我的片段重新加载,但它不排序。

任何人都可以帮我解决这个问题吗?

+0

http://stackoverflow.com/问题/ 23370225/how-to-sort-groups-and-children-expandable-listview-in-android/40546048#40546048 –

+0

@MaharithAdityaSS不起作用因为我使用自定义数组列表 –

+0

您的refreshFragment将分离并附加将重新创建视图,这意味着您的数据将保持不变 –

回答

0

refreshFragment()将分离和附加将重新观点,即意味着你的数据将保持相同

使用

notifyDataSetChanged() or adapter.notifyDataSetChanged()

代替

//Refreshes the fragment 
refreshFragment();