最大的價值我想排序包含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();
}
});
}
}
我得到以下排序順序:
請注意,我有一個單一的長項在每一個ArrayList的項目(稱爲 「timeLong」)。
謝謝你在前進, 加埃塔諾
你的問題是,你有一個'的Hashmap <字符串,對象>',而不是類 – EpicPandaForce