Karampal Yadav Karampal Yadav - 4 months ago 197x
Android Question

how sort recyclerview by data

I am trying to sort my recyclerview by Title name, date and tag but it giving me

following error:-

06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: Shutting down VM
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x5cd4ab20)
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs I/Process: Sending signal. PID: 18488 SIG: 9
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: procName from cmdline: com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs E/AndroidRuntime: in writeCrashedAppName, pkgName :com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: file written successfully with content: com.droidslife.sarkarijobs StringBuffer : ;com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.droidslife.sarkarijobs, PID: 18488
at com.droidslife.sarkarijobs.Formdir.FormMainFragment.onClick(FormMainFragment.java:142)
at android.view.View.performClick(View.java:4499)
at android.view.View$PerformClick.run(View.java:18571)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
06-04 08:56:35.620 19212-19212/com.droidslife.sarkarijobs I/dalvikvm: Could not find method android.content.Context.getSystemService, referenced from method com.droidslife.sarkarijobs.app.MyApplication.access$super
06-04 08:56:35.620 19212-19212/com.droidslife.sarkarijobs W/dalvikvm: VFY: unable to resolve virtual method 586: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
06-04 08:56:35.630 19212-19212/com.droidslife.sarkarijobs D/dalvikvm: VFY: replacing opcode 0x6f at 0x0035
06-04 08:56:35.630 19212-19212/com.droidslife.sarkarijobs I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method com.droidslife.sarkarijobs.app.MyApplication.access$super

this is my main Fragment:-

import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

import com.droidslife.sarkarijobs.R;
import com.droidslife.sarkarijobs.extras.SortListener;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

import java.util.ArrayList;
import java.util.List;

public class FormMainFragment extends Fragment implements View.OnClickListener {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_form_main_items = 3;

//tag associated with the FAB menu button that sorts by name
private static final String TAG_SORT_NAME = "sortName";
//tag associated with the FAB menu button that sorts by date
private static final String TAG_SORT_DATE = "sortDate";
//tag associated with the FAB menu button that sorts by ratings
private static final String TAG_SORT_RATINGS = "sortRatings";
private FloatingActionButton mFAB;
private FloatingActionMenu mFABMenu;
private MyFormMainAdapter myFormMainAdapter;

public FormMainFragment() {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
*Inflate tab_layout and setup Views.
View formMainView = inflater.inflate(R.layout.form_main_layout, null);
tabLayout = (TabLayout) formMainView.findViewById(R.id.tabs);
viewPager = (ViewPager) formMainView.findViewById(R.id.viewpager);

*Set an Apater for the View Pager
viewPager.setAdapter(new MyFormMainAdapter(getChildFragmentManager()));

* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .

tabLayout.post(new Runnable() {
public void run() {

//define the icon for the main floating action button
ImageView iconFAB = new ImageView(getActivity());

//set the appropriate background for the main floating action button along with its icon
mFAB = new FloatingActionButton.Builder(getActivity())

//define the icons for the sub action buttons
ImageView iconSortName = new ImageView(this.getActivity());
ImageView iconSortDate = new ImageView(this.getActivity());
ImageView iconSortRatings = new ImageView(this.getActivity());

//set the background for all the sub buttons
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());

//build the sub buttons
SubActionButton buttonSortName = itemBuilder.setContentView(iconSortName).build();
SubActionButton buttonSortDate = itemBuilder.setContentView(iconSortDate).build();
SubActionButton buttonSortRatings = itemBuilder.setContentView(iconSortRatings).build();

//to determine which button was clicked, set Tags on each button


//add the sub buttons to the main floating action button
mFABMenu = new FloatingActionMenu.Builder(getActivity())

return formMainView;


public void onClick(View v) {

//call instantiate item since getItem may return null depending on whether the PagerAdapter is of type FragmentPagerAdapter or FragmentStatePagerAdapter
Fragment fragment = (Fragment) myFormMainAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());
if (fragment instanceof SortListener) {

if (v.getTag().equals(TAG_SORT_NAME)) {
//call the sort by name method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByName();
if (v.getTag().equals(TAG_SORT_DATE)) {
//call the sort by date method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByDate();
if (v.getTag().equals(TAG_SORT_RATINGS)) {
//call the sort by ratings method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByRating();


class MyFormMainAdapter extends FragmentPagerAdapter {

public MyFormMainAdapter(FragmentManager fm) {

* Return fragment with respect to Position .

public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormFragment();
case 1:
return new AdmitFragment();
case 2:
return new ResultFragment();
return null;

public int getCount() {

return int_form_main_items;


* This method returns the title of the tab according to the position.

public CharSequence getPageTitle(int position) {

switch (position) {
case 0:
return "New Jobs";
case 1:
return "Admit Cards";
case 2:
return "Results";
return null;



and this is form fragment:-

public class FormFragment extends Fragment implements FormsLoadedListener,SwipeRefreshLayout.OnRefreshListener,SortListener,SearchView.OnQueryTextListener {

//The key used to store arraylist of movie objects to and from parcelable
private static final String STATE_FORM = "state_forms";
//the arraylist containing our list of box office his
private ArrayList<FormItem> mListForms = new ArrayList<>();
//the adapter responsible for displaying our movies within a RecyclerView
private AdapterForms mAdapter;

private SwipeRefreshLayout mSwipeRefreshLayout;
//the recyclerview containing showing all our movies
private RecyclerView mRecyclerForms;
//the TextView containing error messages generated by Volley
private TextView mTextError;
private FormSorter mSorter = new FormSorter() ;

//the sorter responsible for sorting our movie results based on choice made by the user in the FAB

// private MovieSorter mSorter = new MovieSorter();

public FormFragment() {
// Required empty public constructor

* Use this factory method to create a new instance of
* this fragment using the provided parameters.
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentBoxOffice.
// TODO: Rename and change types and number of parameters
public static FormFragment newInstance(String param1, String param2) {
FormFragment fragment = new FormFragment();
Bundle args = new Bundle();
//put any extra arguments that you may want to supply to this fragment
return fragment;

public void onFormsLoaded() {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View layout = inflater.inflate(R.layout.form_layout,container,false);
mTextError = (TextView) layout.findViewById(R.id.textVolleyError);
mSwipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeFormHits);
mRecyclerForms =(RecyclerView) layout.findViewById(R.id.listFormHits);
//set the layout manager before trying to display data
mRecyclerForms.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new AdapterForms(getActivity());

if (savedInstanceState != null){

mListForms = savedInstanceState.getParcelableArrayList(STATE_FORM);
} else {

mListForms = MyApplication.getWritableDatabase().readForms(DBForms.FORMS);

if (mListForms.isEmpty()){
L.m("FormFragment: executing task from fragment");
new TaskLoadForms(this).execute();

return layout;


public void onSaveInstanceState(Bundle outState) {

outState.putParcelableArrayList(STATE_FORM, mListForms);

private void handleVolleyError(VolleyError error) {
//if any error occurs in the network operations, show the TextView that contains the error message
if (error instanceof TimeoutError || error instanceof NoConnectionError) {

} else if (error instanceof AuthFailureError) {
} else if (error instanceof ServerError) {
} else if (error instanceof NetworkError) {
} else if (error instanceof ParseError) {

/** * Called when the user chooses to sort results by name through the menu displayed inside FAB
public void onSortByName() {
L.t(getActivity(),"sorted by nme");
// mSorter.sortFormsByName(mListForms);
// mAdapter.notifyDataSetChanged();

* Called when the user chooses to sort results by date through the menu displayed inside FAB
public void onSortByDate() {
// mSorter.sortFormsByDate(mListForms);
// mAdapter.notifyDataSetChanged();

public void onSortByRating() {


* Called when the user chooses to sort results by rating through the menu displayed inside FAB

public void onSortByRating() {

* Called when the AsyncTask finishes load the list of movies from the web

public void onFormsLoaded(ArrayList<FormItem> listForms) {
L.m("FormFRagment: Onformloaded fragment");
if (mSwipeRefreshLayout.isRefreshing()){

public void onRefresh() {
new TaskLoadForms(this).execute();

i also want to add a searchView filter so that i can search recyclerView data by title if anyone any one have a tutorial please comment a link



Try initializing the adapter. It's currently null.

myFormMainAdapter = new MyFormMainAdapter(getChildFragmentManager());