Skip to content
Advertisement

why is my recycler view only showing 1 card

the app is used to sign up for events. Now I am unable to display more then one card in the recycler view even though there is data in the database.it only would display only the first row of data. I know this as I have deleted the first row and the app shows the new first row. The php works as i have loaded the page and it gives me the data from the database.

recycler view activity

public class MainActivity2 extends AppCompatActivity {
    private static final String URL = "http://10.0.2.2/mad/activitydisplay.php";
    RecyclerView recyclerView;
    UserAdapter userAdapter;
    List<Users> usersList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        recyclerView = findViewById(R.id.recylcerList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        usersList=new ArrayList<>();

        LoadAllUser();

    }

    private void LoadAllUser() {
        JsonArrayRequest request =new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray array) {
                for (int i=0;i<array.length();i++){
                    try {
                        JSONObject object=array.getJSONObject(i);
                        String name=object.getString("eventname").trim();
                        String type=object.getString("type").trim();
                        String location=object.getString("location").trim();
                        String time=object.getString("time").trim();
                        String date=object.getString("date").trim();
                        String maxparticipants=object.getString("maxparticipants").trim();

                        Users user= new Users();
                        user.setName(name);
                        user.setType(type);
                        user.setLocation(location);
                        user.setTime(time);
                        user.setDate(date);
                        user.setMaxparticipants(maxparticipants);
                        usersList.add(user);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
                userAdapter=new UserAdapter(MainActivity2.this,usersList);
                recyclerView.setAdapter(userAdapter);

            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error){
                Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue=Volley.newRequestQueue(MainActivity2.this);
        requestQueue.add(request);
    }
}

recycler view adpater

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
    Context context;
    List<Users> usersList;

    public UserAdapter(Context context, List<Users> usersList) {
        this.context = context;
        this.usersList = usersList;
    }

    @NonNull
    @Override
    public UserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list,parent,false);
        return new UserHolder(userLayout);
    }

    @Override
    public void onBindViewHolder(@NonNull UserHolder holder, int position) {
        Users users=usersList.get(position);
        holder.Name_id.setText(users.getName());
        holder.Type_id.setText(users.getType());
        holder.Location_id.setText(users.getLocation());
        holder.Time_id.setText(users.getTime());
        holder.Date_id.setText(users.getDate());
        holder.Slots_id.setText(users.getMaxparticipants());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, cardviewclick.class);
                intent.putExtra("Name",holder.Name_id.getText());
                intent.putExtra("Type",holder.Type_id.getText());
                intent.putExtra("Location",holder.Location_id.getText());
                intent.putExtra("Time",holder.Time_id.getText());
                intent.putExtra("Date",holder.Date_id.getText());
                intent.putExtra("Slots",holder.Slots_id.getText());

                context.startActivity(intent);
            }
        });
    }

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

    public class UserHolder extends RecyclerView.ViewHolder{
        TextView Name_id,Type_id,Location_id,Time_id,Date_id,Slots_id;
        CardView cardView;

        public UserHolder(@NonNull View itemView){
            super(itemView);
            Name_id=itemView.findViewById(R.id.textTitle);
            Type_id=itemView.findViewById(R.id.textType);
            Location_id=itemView.findViewById(R.id.textLocation);
            Time_id=itemView.findViewById(R.id.textTime);
            Date_id=itemView.findViewById(R.id.textDate);
            Slots_id=itemView.findViewById(R.id.textSlots);
            cardView=itemView.findViewById(R.id.card);
        }
    }

}

activitydisplay.php

conn = new mysqli($servername, $username, $password, $dbname);

$stmt = $conn->prepare("SELECT eventname, type ,location,maxparticipants,time,date FROM activitytable");
$stmt ->execute();
$stmt -> bind_result($eventname, $type, $location,$maxparticipants,$time,$date);

$data= array();

while($stmt ->fetch()){

    $temp = array();
    
    $temp['eventname'] = $eventname;
    $temp['type'] = $type;
    $temp['location'] = $location;
    $temp['maxparticipants'] = $maxparticipants;
    $temp['time'] = $time;
    $temp['date'] = $date;
    
    array_push($data,$temp);
    }

    echo json_encode($data);
?>

edit 1:add xml code

main activity 2

<LinearLayout 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"
    tools:context=".MainActivity2">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recylcerList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

card xml

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

    <androidx.cardview.widget.CardView
        android:id="@+id/card"
        app:cardElevation="12dp"
        app:cardCornerRadius="16dp"
        android:layout_margin="16dp"
        android:backgroundTint="#efefef"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textTitle"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Type:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textType"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Location:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textLocation"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>



            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Date:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textDate"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Time:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textTime"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal" >

                <TextView
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Slots:"
                    android:textColor="@color/black"
                    android:textSize="24dp" />

                <TextView
                    android:id="@+id/textSlots"
                    style="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="24dp" />
            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

Advertisement

Answer

It is because the LinearLayout of the card has match_parent as its layout_height and layout_width so it is taking up the whole space. Can you try to scroll the card up?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"       <-------- here
    android:layout_height="match_parent">.    <-------- here

The layout_height should be wrap_content.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement