我写过这段代码
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;
}
}
使用此代码我想在“A-Z”,“Z-A”,“距离”和“ “斑点数”但不知何故这不起作用。当我按下按钮来排序例如“A-Z”我的片段重新加载但它没有排序。
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
refreshFragment()将分离并附加将重新创建视图,这意味着您的数据将保持相同
使用
notifyDataSetChanged() or adapter.notifyDataSetChanged()
而不是
//Refreshes the fragment
refreshFragment();