Я хочу, чтобы отсортировать ArrayList из HashMapsСортировать ArrayList <HashMap <String, Object >> от большого значения на Top
< ArrayList HashMap < String, Object >>
в следующем классе:
public class NotificationFragment extends ListFragment {
private static final String TAG = "NotificationFragment";
private DatabaseReference mDatabaseReference;
private ProgressBar mProgressBar;
String[] from = {"TITLE","BODY","TIME"};
int[] to = {R.id.single_notification_row_title,
R.id.single_notification_row_body,
R.id.single_notification_row_time};
ArrayList<HashMap<String,Object>> data;
SimpleAdapter adapter;
public NotificationFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_notification, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Set Activity title
getActivity().setTitle(R.string.Notifications);
mDatabaseReference = FirebaseDatabase.getInstance().getReference()
.child("Notification").child("Android");
Query query = mDatabaseReference.orderByChild("title");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
getNewData((Map<String,Object>)dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), R.string.Something_went_wrong_retry, Toast.LENGTH_SHORT).show();
}
});
}
private void cancelNotification(Activity activity, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) activity.getSystemService(ns);
nMgr.cancel(notifyId);
}
private void getNewData (Map<String,Object> mapDatabase){
mProgressBar = (ProgressBar)getActivity().findViewById(R.id.progressBarNotifications);
data = new ArrayList<>();
try{
//Iterate through each notification
for(Map.Entry<String,Object> entry : mapDatabase.entrySet()){
Log.e(TAG, "ForEach entered");
Map singleNotification = (Map)entry.getValue();
//Fill the list
String title = (String) singleNotification.get("title");
String body = (String) singleNotification.get("body");
String time = (String) singleNotification.get("time");
String timeLong = (String) singleNotification.get("timeStamp");
HashMap<String,Object> map = new HashMap<>();
map.put("TITLE", title);
map.put("BODY", body);
map.put("TIME", time);
map.put("timeLong", timeLong);
data.add(map);
Log.d(TAG,"data.add(map)");
Log.d(TAG,"data.size = " + data.size());
}
for(HashMap<String, Object> myMap: data) {
for(Map.Entry<String, Object> mapEntry: myMap.entrySet()) {
String key = mapEntry.getKey();
String value = (String) mapEntry.getValue();
if(key.equals("timeLong")){
Log.d(TAG,"timeLong = " + value);
//?????????HOW TO SORT by LONG VALUE ??????????
}
}
}
//Adapter
adapter = new SimpleAdapter
(getActivity(),data,R.layout.single_notification_row ,from,to);
setListAdapter(adapter);
Log.d(TAG,"setListAdapter(adapter)");
}catch (NullPointerException e){
Toast.makeText(getActivity(), R.string.No_Notifications, Toast.LENGTH_SHORT).show();
}
if(mProgressBar.getVisibility() == View.VISIBLE){
mProgressBar.setVisibility(View.GONE);
}
cancelNotification(getActivity(), 0);
}
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "HI", Toast.LENGTH_SHORT).show();
}
});
}
}
я получаю следующий порядок сортировки:
Пожалуйста, обратите внимание, что у меня есть один один длинный элемент (так называемый "timeLong") в каждом ArrayList пункта.
Спасибо заранее, Гаэтано
Ваша проблема в том, что у вас есть 'HashMap' вместо классы –
EpicPandaForce