簡體   English   中英

為什么我的回收站視圖只顯示 1 張卡片

[英]why is my recycler view only showing 1 card

該應用程序用於注冊活動。 現在,即使數據庫中有數據,我也無法在回收站視圖中顯示多於一張卡。它只會顯示第一行數據。 我知道這一點,因為我已經刪除了第一行,並且應用程序顯示了新的第一行。 php 在我加載頁面時工作,它為我提供了數據庫中的數據。

回收商查看活動

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

回收者視圖適配器

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

}

活動顯示.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);
?>

編輯 1:添加 xml 代碼

主要活動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>

卡 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>

這是因為卡片的LinearLayout有match_parent作為它的layout_height和layout_width,所以它占據了整個空間。 您可以嘗試向上滾動卡片嗎?

<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

layout_height 應該是 wrap_content。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM