OnCreate和Onbindviewholder没有调用recyclerview

时间:2016-07-01 12:46:28

标签: android google-maps android-recyclerview google-maps-api-2

我正在使用google maps 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;
    }

}

适配器类

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 个答案:

答案 0 :(得分:1)

您的代码存在问题:

  1. 您只将单个地方传递给适配器

    解。传递地点列表,然后只显示地点列表。

  2. getItemCount应返回您在适配器中传递的listItem的大小

    解。 return listPlaces.size();

  3. 按照本教程和此示例的适配器,然后将listPlaces传递给此并修改,它将起作用。

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

    希望它会有所帮助。