0

Am trying to build my first app and my recyclerView only displays the first item from the json.I have tried changing the layout height to wrap_content but its still not working

Here is the xml layout for the recyclerview activity

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aleky.carrental.cars">
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rv"
        android:clickable="false"
        />
        </RelativeLayout>
</RelativeLayout>

the cardview xml layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    cardview:cardCornerRadius="2dp"
    cardview:cardElevation="3dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:paddingBottom="15dp">

        <TextView
            android:id="@+id/car_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:fontFamily="sans-serif-smallcaps"
            android:text="Car Name"
            android:textColor="#996515"
            android:textSize="24sp"
            android:textStyle="bold" />

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/Car_image"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_below="@+id/car_name"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="2dp" />

        <TextView
            android:id="@+id/tvfeature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="90dp"
            android:layout_marginTop="25dp"
            android:layout_toRightOf="@+id/car_name"
            android:text="Features"
            android:textColor="#000"
            android:textSize="22sp" />

        <View
            android:layout_width="100dp"
            android:layout_height="1dp"
            android:layout_below="@+id/tvfeature"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/Car_image"
            android:background="#000" />

        <ImageView
            android:id="@+id/ac"
            android:layout_width="40dp"
            android:layout_height="20dp"
            android:layout_below="@+id/tvfeature"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/Car_image"
            android:src="@drawable/air_conditioner" />

        <TextView
            android:id="@+id/feature1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvfeature"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/ac"
            android:text="Air conditioner"
            android:textColor="#000"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/tr"
            android:layout_width="40dp"
            android:layout_height="20dp"
            android:layout_below="@+id/feature1"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/Car_image"
            android:src="@drawable/transmission" />

        <TextView
            android:id="@+id/feature2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/feature1"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/tr"
            android:text="Manual"
            android:textColor="#333333"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/pass"
            android:layout_width="40dp"
            android:layout_height="20dp"
            android:layout_below="@+id/tr"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/Car_image"
            android:src="@drawable/passengers" />

        <TextView
            android:id="@+id/feature3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/feature2"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@id/pass"
            android:text="6 Passengers"
            android:textColor="#333333"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/pricetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Car_image"
            android:text="Price :"
            android:textColor="#333333"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/car_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Car_image"
            android:layout_marginLeft="2dp"
            android:layout_toRightOf="@+id/pricetxt"
            android:text="KSH.2000"
            android:textColor="#996515"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/organiser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pricetxt"
            android:layout_marginTop="5dp"
            android:text="Owner :"
            android:textColor="#333333"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/crAgent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pricetxt"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="5dp"
            android:layout_toEndOf="@+id/organiser"
            android:layout_toRightOf="@+id/organiser"
            android:text="Supercars Rentals"
            android:textColor="#05056c"
            android:textSize="14sp" />

        <Button
            android:id="@+id/btn_rent"
            style="@style/AppTheme.ButtonCorners"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/Car_image"
            android:layout_marginRight="12dp"
            android:clickable="true"
            android:text="Rent"
            android:textColor="#eef525" />

        <TextView
            android:id="@+id/car_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

The cars.java class to load items into the recyclerview

   public class cars extends AppCompatActivity {
    String url = "http://192.168.43.201/Car_rental/retrievecars.php";
    Context context = null;
    ProgressDialog dialog;
    private List<CarsItem> array_cars = new ArrayList<>();
    AdapterClass adapter;
    NetworkImageView carsImageview;
    private String user_Location = "cLocation";
    private String Category = "category";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cars);
        adapter = new AdapterClass(cars.this, array_cars);
        RecyclerView recycler = (RecyclerView) findViewById(R.id.rv);
        recycler.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(layoutManager);
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setAdapter(adapter);
        setCars();
        recycler.addOnItemTouchListener(new RecyclerTouchListener(this, recycler, new ClickListener() {

            @Override
            public void onClick(View view, int position) {


            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));


    }

    public void setCars() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("Loading cars...");
        dialog.show();

        if (array_cars != null) {
            array_cars.clear();
        }
        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        try {
                            hideDialog();
                            Log.d("Sever Message", s);

                            JSONObject cars = new JSONObject(s);

                            JSONArray jsonArray = cars.getJSONArray("Cars");
                            JSONObject jobject = null;
                            int len = jsonArray.length();

                            for (int i = 0; i < len; i++) {

                                jobject = jsonArray.getJSONObject(i);
                                CarsItem carsdata = new CarsItem();
                                carsdata.setId(jobject.getString("0"));
                                carsdata.setCarname(jobject.getString("1"));
                                carsdata.setImages(jobject.getString("car_image"));
                                carsdata.setfeature1(jobject.getString("5"));

                                carsdata.setfeature2(jobject.getString("6"));
                                carsdata.setfeature3(jobject.getString("7"));
                                carsdata.setPrice(jobject.getString("8"));
                                array_cars.add(carsdata);
                            }

                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                //Getting Image Location & category
                String Location = "";
                String Ccategory = "cars";

                //Creating parameters
                Map<String, String> params = new Hashtable<String, String>();

                //Adding parameters
                params.put(Category, Ccategory);
                params.put(user_Location, Location);

                //returning parameters
                return params;
            }
        };
        //RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding our request to the queue
        // requestQueue.add(request);
        AppController.getmInstance().addToRequestQueue(request);
    }

    public void hideDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public interface ClickListener {
        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

    public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        GestureDetector gestureDetector;
        cars.ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final cars.ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView recycler, MotionEvent e) {

            View child = recycler.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, recycler.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView recycler, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }

    }

}

and finally my adapter class

    public class AdapterClass extends RecyclerView.Adapter<AdapterClass.CustomViewHolder> {
    private List<CarsItem> Array_cars;
    private ImageLoader imageLoader;
    private Context mContext;


    public AdapterClass(Context context, List<CarsItem> array_cars) {
        this.Array_cars = array_cars;
        this.mContext = context;
    }



    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cars_design, null);

        return new CustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder,final int i) {
        final CarsItem carsItem = Array_cars.get(i);
        holder.imageUrl = carsItem.getImages();

        imageLoader = AppController.getmInstance().getmImageLoader();
        holder.Carname.setText(carsItem.getCarname());
        holder.feature1.setText(carsItem.getfeature1());
        holder.feature2.setText(carsItem.getfeature2());
        holder.feature3.setText(carsItem.getfeature3());
        holder.price.setText(carsItem.getPrice());
        holder.agent.setText(carsItem.getcarowner());
        holder.carid.setText(carsItem.getId());
        holder.carimage.setImageUrl("http://192.168.43.201/Car_rental/cars/"+carsItem.getImages(), imageLoader);

        holder.rent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HireCar.class);

                intent.putExtra("carname",carsItem.getCarname());
                intent.putExtra("car_rate",carsItem.getPrice());
                intent.putExtra("car_image", carsItem.getImages());
                intent.putExtra("car_id", carsItem.getId());
                intent.putExtra("c_owner", carsItem.getcarowner());

                mContext.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return (null != Array_cars ? Array_cars.size() : 0);
    }

    class CustomViewHolder extends RecyclerView.ViewHolder  {
        String imageUrl;
        TextView Carname, feature1, feature2, feature3, price,agent,carid;
        NetworkImageView carimage;
        Button rent;

        public CustomViewHolder(View view) {
            super(view);
            this.Carname = (TextView) view.findViewById(R.id.car_name);
            this.feature1 = (TextView) view.findViewById(R.id.feature1);
            this.feature3 = (TextView) view.findViewById(R.id.feature3);
            this.price = (TextView) view.findViewById(R.id.car_price);
            this.feature2=(TextView) view.findViewById(R.id.feature2);
            this.carimage = (NetworkImageView) view.findViewById(R.id.Car_image);
            this.rent=(Button)view.findViewById(R.id.btn_rent);
            this.agent=(TextView)view.findViewById(R.id.crAgent);
            this.carid=(TextView)view.findViewById(R.id.car_id);

        }


    }
}
Alex Juma
  • 1
  • 1

3 Answers3

1

I think the problem is that you are not adding the elements to the adapter. You need to have a method in the adapter to add items after the adapter has been created. Inside that method you need to notify. Example:

void addItems(List<CarItem> carItems){
   Array_cars.addAll(carItems);
   notifyDataSetChanged();
}

However, I would go a bit far and find the number of items and try to notify only for the newly inserted ones(in case there is something already). This can be done like this:

void addItems(List<CarItem> carItems){
    int position = Array_cars.size();
    Array_cars.addAll(carItems);
    notifyItemRangeChanged(position, Array_cars.size() - position);
}

EDIT

Look at this example below: RecycleView

Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • where exactly do I add the method – Alex Juma Aug 06 '18 at 08:47
  • And how do I Implement it in the recyclerview class – Alex Juma Aug 06 '18 at 09:29
  • I added an example and a link, more than that I cannot do. – Andrei T Aug 06 '18 at 09:41
  • I think you should read a bit more about recycleview and then come back here, because it is quite obvious you do not know what an adapter is. – Andrei T Aug 06 '18 at 09:44
  • I know what an adapter is and its work its just that what you are saying I have tried it and its still not working infact it diplays a blank screen unlike previously where the first item was being displayed – Alex Juma Aug 06 '18 at 11:40
  • Well, no idea what you tried but for me this works pretty well on my adapter. Try with the first method and see if u get something. – Andrei T Aug 06 '18 at 13:55
  • Regarding the blank screen, this happens usually if item count is 0. Check that too. – Andrei T Aug 06 '18 at 13:59
  • I have tried with the first method it is only giving one item – Alex Juma Aug 08 '18 at 14:53
  • I am very surprised to hear this. I will try today after work to test with a small sample app. It could be nice if you update the question with all the code you have so I can import it in android studio directly. A pastebin link would do just fine. – Andrei T Aug 08 '18 at 15:19
-1

Here is the xml layout for the recyclerview activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aleky.carrental.cars">
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"
        android:clickable="false"
        />
        </RelativeLayout>
</RelativeLayout>
Parth Suthar
  • 123
  • 4
-1

You have added match_parent height of "RecyclerView" please change to wrap_content.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aleky.carrental.cars">
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rv"
        android:clickable="false"
        />
        </RelativeLayout>
</RelativeLayout>