Reputation: 39
I'm trying to sort a RecyclerView by Date, but I tried too many things and I don't know what to try now. The problem is in the line adapter.notifyDataSetChanged(); because if I don't put doesn't show me the error but doesn't update the recyclerview too
This is what I have now, and shows me that error.
Unable to start activity ComponentInfo{st.stc/sharetaxi.sharetaxicabdriver.PendingTrajectRecyclerView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference'
First of all I put all the trajects in the method GetAllTrajects() and inside Collections.sort I parse to date the TextView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pending_traject_recycler_view);
listPendingTraject=new ArrayList<>();
recyclerPendingTraject = (RecyclerView)findViewById(R.id.pendingTrajectRecyclerView);
recyclerPendingTraject.setLayoutManager(new LinearLayoutManager(this));
GetAllTrajects();
Collections.sort(listPendingTraject, new Comparator<PendingTrajectPOJO>() {
@Override
public int compare(PendingTrajectPOJO pendingTrajectPOJO, PendingTrajectPOJO t1) {
String Date = pendingTrajectPOJO.getDate();
String Date1 = t1.getDate();
Date date=null,date1=null;
SimpleDateFormat formatDate = new SimpleDateFormat("dd/MMM/yyyy");
try {
date = formatDate.parse(Date);
date1 = formatDate.parse(Date1);
} catch (ParseException e) {
e.printStackTrace();
}
return date.before(date1) ? 1 : 0;
}
});
adapter.notifyDataSetChanged();
}
Inside method GetAllTrajects when I put all the information I call the method showPendingTraject()
private void showPendingTraject() {
listPendingTraject.add(new PendingTrajectPOJO(nameUsers,coordenatesOriginBundle,Origin,Destination,DataIHora,coordenatesDestiBundle,contadorCordenades));
Log.d("ID",""+nomUser);
adapter = new AdapterPendingTraject(listPendingTraject);
recyclerPendingTraject.setAdapter(adapter);
}
Upvotes: 2
Views: 5233
Reputation: 857
A simple solution to it would be while storing/entering the data in your PendingTrajectPOJO store date in milliseconds which will be of type long instead of String, and then you can easily compare the long values, and sort it.
Suppose your date is 28/05/2018, then you can get it in milliseconds using the following method:
String givenDateString = "28/05/2018";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date mDate = sdf.parse(givenDateString);
long timeInMilliseconds = mDate.getTime();
//store this timeInMilliseconds in your POJO object
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
and using timeInMilliseconds sort it, just don't forget to convert timeInMilliseconds into a date while displaying in RecyclerView.
Collections.sort(listPendingTraject, new Comparator<PendingTrajectPOJO>() {
@Override
public int compare(PendingTrajectPOJO pendingTrajectPOJO, PendingTrajectPOJO t1) {
long Date = pendingTrajectPOJO.getDate();
long Date1 = t1.getDate();
return Date.compareTo(Date1);
}
});
Upvotes: 1
Reputation: 273
I think below mentioned line is creating a problem.
String Date =pendingTrajectPOJO.getDate();
Rename Date to something else and see if it works
Upvotes: 0