Don
Don

Reputation: 3

OnCreate and Onbindviewholder is not calling on recyclerview

I'm using google maps api, while I'm trying to set places name and images on RecyclerView, also using bottomsheet and viewpager, I want to fetch the data while app lunch , actually i got results showing nearest places and how to i set the name and images on recyclerview..?
actual problem is not calling the onCreateViewHolder and onBindViewHolder while calling the adapter..!! sample code is here

MainActivity.java

protected void onPostExecute(Place[] places) {
            mHMReference.clear();
            mPlaces = places;
//            List<HashMap<String, String>> onlineData = mPlaces;

            Place place;
            for (int i = 0; i < places.length; i++) {
                place = places[i];
                Log.e("Place", String.valueOf(place));
                double lat = Double.parseDouble(place.mLat);
                double lng = Double.parseDouble(place.mLng);
                Log.e("PostEx Lat" + lat, "Long" + lng);
                LatLng latLng = new LatLng(lat, lng);
                Marker m = drawMarker(latLng, BitmapDescriptorFactory.HUE_BLUE);
                        }
            int v = mHMReference.size();

            dm = new DisplayMetrics();

            for (int j = 0; j < mHMReference.size(); j++) {
                Log.e("Loop out", String.valueOf(mPlaces[j]));
                placeDeta = mPlaces[j];

                viewPager = (CustomPager) findViewById(R.id.viewpager);
                ViewPagerAdapter adapterPager = new ViewPagerAdapter(getSupportFragmentManager());
                adapterPager.addFragment(new CurrentPlace(), "Current Location");
                adapterPager.addFragment(new NearestPlace(), "Nearest Places");
                adapterPager.addFragment(new CategoryPage(), "Category");
                adapterPager.addFragment(new SuggesionsPage(), "Simler Places");
                viewPager.setAdapter(adapterPager);
                tabLayout.setupWithViewPager(viewPager);

                NearestPlace nearestPlace = new NearestPlace(placeDeta, dm);
                FragmentManager fm1 = getSupportFragmentManager();
                FragmentTransaction ft1 = fm1.beginTransaction();
                ft1.add(nearestPlace, "TAG");
                ft1.commit();
            }
        }
    }

The above code have two constructor call

Fragment.java

public class NearestPlace extends Fragment {
    Place mPlace ;
    DisplayMetrics mMetrics = null;
    private WindowManager windowManager;
    String url;
    View rootView;
    int i=0;
    public List<HashMap<String, String>> onlineData;
    public NearestPlace(){
        super();
    }
    public NearestPlace(Place place, DisplayMetrics dm){
        super();
        this.mPlace = place;
        this.mMetrics = dm;
        Log.e(" call dialog ", String.valueOf(mPlace));
    }

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

        ViewGroup con=container;
        Bundle bd=savedInstanceState;

        rootView = inflater.inflate(R.layout.nearestlocation, container,false);
        Log.e("Mplace!!!!", String.valueOf(mPlace));

        if(mPlace!=null){

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycleviewNearestlocations);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        RecycleViewAdapterNearestLocations RecycleViewAdapterNearestLocations;
        RecycleViewAdapterNearestLocations = new RecycleViewAdapterNearestLocations(mPlace, getContext());
        Log.e("MplaceURL", "dd " + mPlace);
        recyclerView.setAdapter(RecycleViewAdapterNearestLocations);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        }

        else{

            Toast.makeText(getContext(),"No data found",Toast.LENGTH_LONG);
        }



        return rootView;
    }

    public WindowManager getWindowManager() {
        return windowManager;
    }

}

Adapter class

RecycleViewAdapterNearestLocations.java

public class RecycleViewAdapterNearestLocations extends RecyclerView.Adapter<RecycleViewAdapterNearestLocations.ViewHolder> {
    static Context home;
    Place mPlace;
    int i;
    private List<Place> mData = new ArrayList<>();

    public RecycleViewAdapterNearestLocations(Place mPlace, Context context) {
        this.mPlace=mPlace;
        this.home=context;
        i=1;
        Log.e("detailsofREC","place : "+mPlace);

        if(mPlace!=null){
        Toast.makeText(home,i+" Name ::"+mPlace.mPlaceName,Toast.LENGTH_LONG).show();
        }
        else{

        Toast.makeText(home,i+" Name null!!"+mPlace,Toast.LENGTH_LONG).show();
    }
}

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
            View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleviewnearest, null);
            ViewHolder viewHolder = new ViewHolder(itemLayoutView);
            DisplayMetrics displayMetrics = home.getResources().getDisplayMetrics();
            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            Log.e("card","width:"+width);
            Log.e("card","height:"+height);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
            viewHolder.itemView.setLayoutParams(layoutParams);
        return viewHolder;
        }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Log.e("Onbindview", String.valueOf(mPlace));

        if(mPlace!=null){

        Toast.makeText(home,i+ " TAG Name "+mPlace.mPlaceName,Toast.LENGTH_LONG).show();
            viewHolder.txtViewTitle.setText(mPlace.mPlaceName);
        }
        else{

            Toast.makeText(home,i+" TAG Name NULL!!!"+mPlace,Toast.LENGTH_LONG).show();
            i++;
        }


    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public ItemData mItem;
    public TextView txtViewTitle;
    public ImageView imgViewIcon;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.txtNearestPlaceName);
        imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imgNearestPlaceBackground);
            itemLayoutView.setOnClickListener(this);
    }

    public void setItem(ItemData item) {
        mItem = item;
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(home,DetailPage.class);
        home.startActivity(intent);
    }
}
    @Override
    public int getItemCount() {
        return 5;
    }

mPlace values like this :com.example.user.myapplication.classes.Place@38c8eb1f

Using this tutorials

and how to I set the Place name in RecyclerView

Please help me, Thank you

Upvotes: 0

Views: 868

Answers (1)

Er. Kaushik Kajavadara
Er. Kaushik Kajavadara

Reputation: 1667

The Problem with your code is:

  1. You are passing only single place to adapter

    Solution. pass list of places then only it will show you list of places.

  2. getItemCount should return size of your listItems that you are passing in adapter

    Solution. return listPlaces.size();

follow this Tutorial and adapter of this example and then pass your listPlaces in this and modify, It will work.

https://www.learn2crack.com/2016/02/image-loading-recyclerview-picasso.html

Hope it will help.

Upvotes: 1

Related Questions