如何在android中排序后用新数据刷新适配器?

时间:2017-06-06 08:55:47

标签: android android-fragments refresh fragmentpageradapter

我在我的应用程序中使用FragmentPagerAdapter。共有3个选项卡,在排序数据后,我无法获得新数据。

任何人都可以帮我解决这个问题吗?

这是我的代码:

public class PagerAdapter extends FragmentPagerAdapter {

    @SuppressWarnings("WeakerAccess")
    public final int tabCount;
//    private String categorySearchPath;
    private List<String> categorySearchPath;
    private String categoryTitle;
    public static String TAG="Pager";
    MyAlert myAlert=new MyAlert();
    public static SortingRequest sortRequest = new SortingRequest();



    public PagerAdapter(FragmentManager fm, int tabCount,List<String> categorySearchPath,String categoryTitle) {
        super(fm);
        this.tabCount = tabCount;
        this.categorySearchPath=categorySearchPath;
        this.categoryTitle=categoryTitle;
    }



    @Override
    public Fragment getItem(int position) {

        String searchedText="";
        int someParameter=-1;

        switch (position) {
            case 0:
                @SuppressWarnings("UnnecessaryLocalVariable")
                FragmentOnlineRetailers fr = new FragmentOnlineRetailers();
                fr.callSearchFragOnline(searchedText);

                //category
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("ListOfCategorySearchPath", (ArrayList<String>) categorySearchPath);
                bundle.putString("categoryTitle",categoryTitle);
                fr.setArguments(bundle);
                fr.categorySearchFlow(searchedText);

                return fr;

            default:
                return null;

        }



    }

    @Override
    public int getCount() {
        return tabCount;
    }


}

公共类MyAlert扩展了DialogFragment {

public static final String TAG="MyAlert";
public static String  searchedText="*";
private static Context context;
private Context mContext;
public static List<PPHits> listHitsSort=null;
private RecyclerView onlineRecycler;
final SortingRequest sortRequest = new SortingRequest();
private List<String> sort=new ArrayList<>();
private static int someParameter=-1;


public MyAlert() {

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle("Sort by");
    LayoutInflater li = getActivity().getLayoutInflater();
    @SuppressLint("InflateParams")
     final View promptsView = li.inflate(R.layout.popup_sortby, null);
    alertDialogBuilder.setView(promptsView);
    alertDialogBuilder.setCancelable(false);



    alertDialogBuilder.setSingleChoiceItems(R.array.sortList, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            Log.d(TAG, "onClick: Sort Dialogue "+which);
            doSomeStuff(which);

        }
    });




    alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
           // vLayoutSort.setClickable(true);

            dismiss();
        }
    });





    alertDialogBuilder.setPositiveButton("SORT", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id ) {
           // vLayoutSort.setClickable(true);

            FragmentOnlineRetailers fragmentOnlineRetailers=new FragmentOnlineRetailers();<---second time object is created to access CALLSORT(), how to use same object without creating new object
            fragmentOnlineRetailers.callSort(searchedText);



        }

    });



    Dialog dialog=alertDialogBuilder.create();
    return dialog;
}




    //Sorting Call this method is in  FragmentOnlineRetailers  class

    public List<PPHits> callSort(final String searchText) {



        String searchSortText="*";

        APIService api_service;

        Log.d(TAG, "callFb: " + searchSortText);

        retrofit.Retrofit retrofit = new retrofit.Retrofit
                .Builder()
                .baseUrl("http:// --------.com:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        api_service = retrofit.create(APIService.class);
        Log.d(TAG, "callSorting: Inside");

        Log.d(TAG, "callSorting: " + searchSortText);
        if (searchText != null) {

            Log.d(TAG, "callSorting: "+sortRequest);

            Call<ProductPageResponse> exampleCall = api_service.getSorted(sortRequest);
            exampleCall.enqueue(new Callback<ProductPageResponse>() {
                @Override
                public void onResponse(Response<ProductPageResponse> response, Retrofit retrofit) {

                    Log.d(TAG, "onResponse: Inside response");

                    if (response.code() == 200) {

                        Log.d(TAG, "onResponse: " + response.code());
                        Log.d(TAG, "onResponse: " + response.body());

                        ProductPageResponse ppr = response.body();
                        listHitsSort = ppr.getPpObj().getResult().getHits();


                        Log.d("Hello Moto", "callSorting: "+listHitsSort);



                        if(listHitsSort!=null) {

                            Log.d(TAG, "onResponse: Hello Sirji kya haal h");
                            Context activity = getActivity();



                            adapter = new OnlineRetailAdapter(listHitsSort,activity, R.layout.item_product);
                            onlineRecycler.setAdapter(adapter);




                        }else{
                            Log.d(TAG, "onResponse: Sorted list is NUll ");
                        }

                    }else
                    {
                        Log.d(TAG, "onResponse: product page not available");
                    }


                    Log.d(TAG, "onResponse: getting responser from server");

                }

                @Override
                public void onFailure(Throwable t) {

                    Log.d("callSorting", "onFailure:" + t.getMessage());


                }


            });

        }

        return listHitsSort;


    }

0 个答案:

没有答案