2017-02-20 78 views
0

最大的價值我想排序包含HashMap排序的ArrayList <HashMap的<字符串,對象>>從頂部

的ArrayList

的ArrayList <的HashMap <字符串,對象>>

在下面的類

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

我得到以下排序順序:

Screenshot

請注意,我有一個單一的長項在每一個ArrayList的項目(稱爲 「timeLong」)。

謝謝你在前進, 加埃塔諾

+1

你的問題是,你有一個'的Hashmap <字符串,對象>',而不是類 – EpicPandaForce

回答

1

,我建議你創建一個包含4個屬性的類:

String title; 
String body; 
String time; 
long timeLong; 

可以說類的名稱是實例。獲取數據時,創建此類的實例並設置此實例的屬性,而不是有4個字符串值。

改變你的ArrayList這個ArrayList<Instance> myList = ArrayList();

創建一個類

public class SortingClass implements Comparator<Instance> 
{ 
    public int compare(Instance left, Instance right){ 
      return left.timeLong < right.timeLong ? 1 : -1; 
} 

而對於排序

Collections.sort(myList,new SortingClass()) 
+0

我不太確定我的理解。你能爲我寫一段關於ArrayList的代碼嗎? –

+0

我試過了,但SimpleAdapter不接受ArrayList ...非常感謝您 –

+0

您可以實現自己的適配器,從ArrayAdapter 擴展它。然後,你可以填充它並將它傳遞給你的列表視圖。 – anzaidemirzoi

相關問題