没有方法在recylcerview android中调用

时间:2018-02-21 19:54:43

标签: android android-recyclerview

这里,只有构造函数正在调用并且数据正在arraylist中传递,但没有其他任何事情发生,没有其他函数正在调用 -

适配器代码

public class RentCarRecyclerAdapter extends 
RecyclerView.Adapter<RentCarRecyclerAdapter.MyViewHolder> {


    public  static   ArrayList<RentCarHolder> dataList;
    Context context;


    public RentCarRecyclerAdapter(Context c, ArrayList<RentCarHolder> list) {
        context = c;
        dataList = list;


    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView carimage;
        TextView carname, rentprice, ageofdriver, numberofdoors, maxpeople, airconditioned, mileage, transmission, status;
        Button booknow;
        ProgressBar progressBar;

        public MyViewHolder(View itemView) {
            super(itemView);

            carimage = (ImageView) itemView.findViewById(R.id.carpic);
            carname = (TextView) itemView.findViewById(R.id.carname);
            rentprice = (TextView) itemView.findViewById(R.id.rentprice);
            ageofdriver = (TextView) itemView.findViewById(R.id.minimumdriverage);
            numberofdoors = (TextView) itemView.findViewById(R.id.doorcount);
            maxpeople = (TextView) itemView.findViewById(R.id.maxpeople);
            airconditioned = (TextView) itemView.findViewById(R.id.airconditioned);
            mileage = (TextView) itemView.findViewById(R.id.unlimitedmilage);
            transmission = (TextView) itemView.findViewById(R.id.transmission);
            booknow = (Button) itemView.findViewById(R.id.book);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
            status = (TextView) itemView.findViewById(R.id.status);


        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_rentacar, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);


        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.carname.setText(dataList.get(position).getCarname());
        holder.rentprice.setText(dataList.get(position).getRentprice());
        holder.ageofdriver.setText(dataList.get(position).getAgeofdriver());
        holder.numberofdoors.setText(dataList.get(position).getNumberofdoors());
        holder.maxpeople.setText(dataList.get(position).getMaxpeople());
        holder.airconditioned.setText(dataList.get(position).getAirconditioned());
        holder.mileage.setText(dataList.get(position).getMileage());
        holder.transmission.setText(dataList.get(position).getTransmission());
        holder.status.setText(dataList.get(position).getStatus());


        Glide.with(context)
            .load(dataList.get(position).getCarimageURL())

            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    holder.progressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(holder.carimage);
            holder.booknow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(new Intent(context, CarBookingActivity.class).putExtra("position",position));
                }
            });

     }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


}

xml代码

 <android.support.v7.widget.RecyclerView
    android:id="@+id/carshowrecycler"
    android:layout_below="@id/menu"
    android:layout_width="match_parent"

    android:layout_height="match_parent"


    android:orientation="horizontal"


    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>

我已经按照许多答案的解决方案,但没有人帮助过。我一直在研究它持续3-4个小时,并研究了很多关于堆栈溢出的答案,但没有任何帮助 请帮帮我!

活动代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="belllab.naseem.tourism.Activities.RentACarActivity">

<LinearLayout
    android:id="@+id/menu"
    android:layout_width="match_parent"
    android:layout_height="50dp"

    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/locallayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="10dp"
            android:id="@+id/localairporttext"
            android:text=" Local/Airport Pick / Drop"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <View
            android:id="@+id/localairportview"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#7e7c7c" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/outstationlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/outstationtext"
            android:layout_width="wrap_content"


            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="10dp"
            android:text="OutStation"
            android:textColor="@color/black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <View
            android:id="@+id/outstationview"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#7e7c7c" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/selflayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/selfvehicle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="10dp"
            android:text="Self Driven Vehicle"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <View
            android:id="@+id/selfvehicleview"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="#7e7c7c" />
    </RelativeLayout>

</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/carshowrecycler"
    android:layout_below="@id/menu"
    android:layout_width="match_parent"

    android:layout_height="match_parent"


    android:orientation="horizontal"


    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>


  </RelativeLayout>

设置适配器代码

 RentCarRecyclerAdapter adapter =    new RentCarRecyclerAdapter(RentACarActivity.this,list);
                    carsRecyclerView.setAdapter(adapter);

行代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">

    <RelativeLayout
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       <LinearLayout

       android:layout_above="@+id/book"
      android:layout_width="match_parent"
      android:orientation="vertical"
      android:layout_height="match_parent">
      <RelativeLayout
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_for_car">

        <ImageView

        android:layout_width="match_parent"
       android:id="@+id/carpic"
        android:layout_height="match_parent"
      />
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:id="@+id/progress"
            />
      </RelativeLayout>

          <TextView
          android:id="@+id/carname"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="Taxi"
          android:textSize="20sp"
          android:textStyle="bold"

          />

          <View
          android:layout_width="match_parent"
         android:layout_height="1dp"
         android:layout_marginLeft="15dp"
         android:layout_marginRight="15dp"
         android:background="@color/transparentview"
        />
           <RelativeLayout
          android:layout_marginLeft="45dp"
         android:layout_marginRight="45dp"
         android:layout_width="match_parent"
          android:layout_height="wrap_content">
          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            />

        <TextView
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:text="availiable"
            android:id="@+id/status"
            android:layout_alignParentRight="true"

            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:layout_height="wrap_content"

            />
        </RelativeLayout>


         <RelativeLayout
        android:layout_marginLeft="45dp"
        android:layout_marginRight="45dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Price per day"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            />

        <TextView
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:text="RS 7,600"
            android:id="@+id/rentprice"
            android:layout_alignParentRight="true"

            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:layout_height="wrap_content"

            />
      </RelativeLayout>
      <View
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@color/transparentview"
        />
        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Max People"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:id="@+id/maxpeople"
                   android:layout_alignParentRight="true"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Door Count"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:id="@+id/doorcount"
                    android:layout_alignParentRight="true"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Minimum driver age"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:id="@+id/minimumdriverage"
                    android:layout_alignParentRight="true"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Transmission"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:id="@+id/transmission"
                    android:layout_alignParentRight="true"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Air Conditioned?"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:layout_alignParentRight="true"
                    android:id="@+id/airconditioned"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginLeft="45dp"
                android:layout_marginRight="45dp"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Unlimited mileage?"
                    android:layout_marginTop="5dp"
                    />

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:layout_alignParentRight="true"
                    android:id="@+id/unlimitedmilage"




                    android:layout_marginTop="5dp"
                    android:text="10" />
            </RelativeLayout>


        </LinearLayout>

    </ScrollView>

      </LinearLayout>
         <View
          android:layout_above="@+id/book"
           android:layout_marginBottom="6dp"
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:layout_marginLeft="15dp"
          android:layout_marginRight="15dp"
         android:background="@color/transparentview"
        />

         <Button


        android:layout_width="140dp"
        android:id="@+id/book"
        android:text="Book Now"
        android:background="@drawable/bookbuttonbackground"
        android:textColor="@color/white"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_height="40dp" />

    </RelativeLayout>
</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

代码中的问题是代码是你在xml和java代码中都使用布局管理器删除它们中的任何一个 任何其次验证你使用设计编译或recylcerview编译gradle而不是两者 确保在代码中你没有使recylcerview可见性消失

答案 1 :(得分:0)

适配器代码没有问题

从XML app:layoutManager="android.support.v7.widget.LinearLayoutManager"

中删除此行

您需要在此活动中获取recyclelerView

RecyclerView recyclerView = findViewById(R.id.carshowrecycler);
RentCarRecyclerAdapter adapter = new RentCarRecyclerAdapter (this,dataList);

LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);

然后将layoutManager和适配器分配给您的recyclerView

recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);

答案 2 :(得分:0)

对于RecyclerView可能缺少这个:

recyclerView.setLayoutManager(new LinearLayoutManager(runningActivity));

我每次都忘了它并且想知道为什么没有显示任何东西(相同的效果)。

我不知道为什么,但是在将LayoutManager分配给Recylerview之后,它可以正常工作。