OnCreate 和 Onbindviewholder 没有调用 recyclerview

OnCreate and Onbindviewholder is not calling on recyclerview

我正在使用 google 地图 api,同时我正在尝试在 RecyclerView 上设置地点名称和图像,还使用 ​​bottomsheet 和 viewpager,我想获取应用程序午餐时的数据,实际上我得到的结果显示最近的地方以及如何在 recyclerview 上设置名称和图像..?
实际问题是在调用适配器时没有调用 onCreateViewHolderonBindViewHolder..!! 示例代码在这里

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();
            }
        }
    }

以上代码有两次构造函数调用

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;
    }

}

适配器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 值是这样的:com.example.user.myapplication.classes.Place@38c8eb1f

Using this tutorials

以及如何在 RecyclerView

中设置地名

请帮帮我,谢谢

您的代码存在的问题是:

  1. 您只将一个位置传递给适配器

    解决方案。通过地点列表,然后它会显示地点列表。

  2. getItemCount 应该 return 您在适配器中传递的列表项的大小

    解决方案。 return listPlaces.size();

按照本教程和本示例的适配器进行操作,然后将您的 listPlaces 传递到其中并进行修改,它将起作用。

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

希望对您有所帮助。