RecyclerView 正在填充我列表中的最后一个项目(我得到了相同的项目)

发布于 2025-01-10 02:29:29 字数 7249 浏览 1 评论 0原文

Recyclerview 正在填充我列表中的最后一项

private void loadOrganization() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_ORG,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object

                            for (int i = 0; i < array.length(); i++) {

                                //getting orgs object from json array
                                JSONObject organization_detail = array.getJSONObject(i);

                                //adding the org to org list
                                organization_list.add(new organization_detail(
                                        organization_detail.getInt("Org_ID"),
                                        organization_detail.getString("Org_Name"),
                                        organization_detail.getString("Org_Description"),
                                        organization_detail.getString("Org_Moderator"),
                                        organization_detail.getString("Org_President"),
                                        organization_detail.getString("Org_VicePresident"),
                                        organization_detail.getString("Org_ActiveSocMedAcc"),
                                        organization_detail.getString("Org_Logo"),
                                        organization_detail.getInt("Cluster_ID")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            organization_adapter adapter = new organization_adapter(organization.this, organization_list);
                            recyclerView.setAdapter(adapter);

这是我的 onBindViewHolder 适配器

 @Override
    public organizationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.organization_list, null);
        return new organizationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull organizationViewHolder holder, int position) {

        organization_detail organization_detail = organization_list.get(position);



        //loading the image
        Glide.with(mCtx)
                .load(organization_detail.getOrg_Logo())
                .into(holder.imageViewOrg);

        holder.textViewOrgName.setText(organization_detail.getOrg_Name());



    }

这是我的模型

 public class organization_detail {
    private int Org_ID;
    private static String Org_Name;
    private String Org_Description;
    private String Org_Moderator;
    private String Org_President;
    private String Org_VicePresident;
    private String Org_ActiveSocMedAcc;
    private static String Org_Logo;
    private int Cluster_ID;

        public organization_detail(int Org_ID, String Org_Name, String Org_Description,String Org_Moderator,String Org_President, String Org_VicePresident, String Org_ActiveSocMedAcc,
                                   String Org_Logo, int Cluster_ID){

            this.Org_ID = Org_ID;
            this.Org_Name = Org_Name;
            this.Org_Description = Org_Description;
            this.Org_Moderator = Org_Moderator;
            this.Org_President= Org_President;
            this.Org_VicePresident = Org_VicePresident;
            this.Org_ActiveSocMedAcc = Org_ActiveSocMedAcc;
            this.Org_Logo= Org_Logo;
            this.Cluster_ID= Cluster_ID;

        }
                public int getOrg_ID() {
                    return Org_ID;
                }

                public String getOrg_Name() {
                    return Org_Name;
                }

                public String getOrg_Description() {
                    return Org_Description;
                }

                public String getOrg_Moderator() {
                    return Org_Moderator;
                }

                public String getOrg_President() {
                    return Org_President;
                }

                public String getOrg_VicePresident() {
                    return Org_VicePresident;
                }

                public String getOrg_ActiveSocMedAcc() {
                    return Org_ActiveSocMedAcc;
                }

                public String getOrg_Logo() {
                    return Org_Logo;
                }

                public int getCluster_ID() {
                    return Cluster_ID;
                }


}

这是我的 ViewHolder em>

@Override
    public organizationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.organization_list, null);
        return new organizationViewHolder(view);
    }

这就是我从数据库获取数据的方式

<?php 
 

 //database constants
 define('DB_HOST', 'localhost');
 define('DB_USER', 'root');
 define('DB_PASS', '');
 define('DB_NAME', 'orgspace');
 
 //connecting to database and getting the connection object
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
 
 //Checking if any error occured while connecting
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }
 
 //creating a query
 $stmt = $conn->prepare("SELECT * FROM organization ORDER BY Org_Name ASC");



 //imgPath
$imgPath = 'http://192.168.1.11:80/OrgSpace/OrgSpace-Admin/logo_uploads/';

 //executing the query 
 $stmt->execute();
 
 //binding results to the query 
 $stmt->bind_result($Org_ID, $Org_Name, $Org_Description, $Org_Moderator, $Org_President,$Org_VicePresident, $Org_ActiveSocMedAcc,$Org_Logo,$Cluster_ID);
 
 $organizations = array(); 
 
            //traversing through all the result 
            while($stmt->fetch()){
                    $temp = array();
                    $temp['Org_ID'] = $Org_ID; 
                    $temp['Org_Name'] = $Org_Name; 
                    $temp['Org_Description'] = $Org_Description; 
                    $temp['Org_Moderator'] = $Org_Moderator; 
                    $temp['Org_President'] = $Org_President; 
                    $temp['Org_VicePresident'] = $Org_VicePresident;
                    $temp['Org_ActiveSocMedAcc'] = $Org_ActiveSocMedAcc;
                    $temp['Org_Logo'] = $imgPath.$Org_Logo; 
                    $temp['Cluster_ID'] = $Cluster_ID;

            array_push($organizations, $temp);
            }
            
 //displaying the result in json format 
 echo json_encode($organizations, JSON_PRETTY_PRINT);



<?

问题是它显示相同的项目而不是显示不同的项目。这是我收到的输出。

这应该输出不同的组织,但我不断得到相同的输出。我认为 for 循环中有问题,YAHRA 是我列表中的最后一项。

The Recyclerview is populating with the last item in my list

private void loadOrganization() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_ORG,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object

                            for (int i = 0; i < array.length(); i++) {

                                //getting orgs object from json array
                                JSONObject organization_detail = array.getJSONObject(i);

                                //adding the org to org list
                                organization_list.add(new organization_detail(
                                        organization_detail.getInt("Org_ID"),
                                        organization_detail.getString("Org_Name"),
                                        organization_detail.getString("Org_Description"),
                                        organization_detail.getString("Org_Moderator"),
                                        organization_detail.getString("Org_President"),
                                        organization_detail.getString("Org_VicePresident"),
                                        organization_detail.getString("Org_ActiveSocMedAcc"),
                                        organization_detail.getString("Org_Logo"),
                                        organization_detail.getInt("Cluster_ID")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            organization_adapter adapter = new organization_adapter(organization.this, organization_list);
                            recyclerView.setAdapter(adapter);

Here's my adapter onBindViewHolder

 @Override
    public organizationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.organization_list, null);
        return new organizationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull organizationViewHolder holder, int position) {

        organization_detail organization_detail = organization_list.get(position);



        //loading the image
        Glide.with(mCtx)
                .load(organization_detail.getOrg_Logo())
                .into(holder.imageViewOrg);

        holder.textViewOrgName.setText(organization_detail.getOrg_Name());



    }

And this is my model

 public class organization_detail {
    private int Org_ID;
    private static String Org_Name;
    private String Org_Description;
    private String Org_Moderator;
    private String Org_President;
    private String Org_VicePresident;
    private String Org_ActiveSocMedAcc;
    private static String Org_Logo;
    private int Cluster_ID;

        public organization_detail(int Org_ID, String Org_Name, String Org_Description,String Org_Moderator,String Org_President, String Org_VicePresident, String Org_ActiveSocMedAcc,
                                   String Org_Logo, int Cluster_ID){

            this.Org_ID = Org_ID;
            this.Org_Name = Org_Name;
            this.Org_Description = Org_Description;
            this.Org_Moderator = Org_Moderator;
            this.Org_President= Org_President;
            this.Org_VicePresident = Org_VicePresident;
            this.Org_ActiveSocMedAcc = Org_ActiveSocMedAcc;
            this.Org_Logo= Org_Logo;
            this.Cluster_ID= Cluster_ID;

        }
                public int getOrg_ID() {
                    return Org_ID;
                }

                public String getOrg_Name() {
                    return Org_Name;
                }

                public String getOrg_Description() {
                    return Org_Description;
                }

                public String getOrg_Moderator() {
                    return Org_Moderator;
                }

                public String getOrg_President() {
                    return Org_President;
                }

                public String getOrg_VicePresident() {
                    return Org_VicePresident;
                }

                public String getOrg_ActiveSocMedAcc() {
                    return Org_ActiveSocMedAcc;
                }

                public String getOrg_Logo() {
                    return Org_Logo;
                }

                public int getCluster_ID() {
                    return Cluster_ID;
                }


}

This is my ViewHolder

@Override
    public organizationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.organization_list, null);
        return new organizationViewHolder(view);
    }

And this is how I get my data from database

<?php 
 

 //database constants
 define('DB_HOST', 'localhost');
 define('DB_USER', 'root');
 define('DB_PASS', '');
 define('DB_NAME', 'orgspace');
 
 //connecting to database and getting the connection object
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
 
 //Checking if any error occured while connecting
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }
 
 //creating a query
 $stmt = $conn->prepare("SELECT * FROM organization ORDER BY Org_Name ASC");



 //imgPath
$imgPath = 'http://192.168.1.11:80/OrgSpace/OrgSpace-Admin/logo_uploads/';

 //executing the query 
 $stmt->execute();
 
 //binding results to the query 
 $stmt->bind_result($Org_ID, $Org_Name, $Org_Description, $Org_Moderator, $Org_President,$Org_VicePresident, $Org_ActiveSocMedAcc,$Org_Logo,$Cluster_ID);
 
 $organizations = array(); 
 
            //traversing through all the result 
            while($stmt->fetch()){
                    $temp = array();
                    $temp['Org_ID'] = $Org_ID; 
                    $temp['Org_Name'] = $Org_Name; 
                    $temp['Org_Description'] = $Org_Description; 
                    $temp['Org_Moderator'] = $Org_Moderator; 
                    $temp['Org_President'] = $Org_President; 
                    $temp['Org_VicePresident'] = $Org_VicePresident;
                    $temp['Org_ActiveSocMedAcc'] = $Org_ActiveSocMedAcc;
                    $temp['Org_Logo'] = $imgPath.$Org_Logo; 
                    $temp['Cluster_ID'] = $Cluster_ID;

            array_push($organizations, $temp);
            }
            
 //displaying the result in json format 
 echo json_encode($organizations, JSON_PRETTY_PRINT);



<?

The thing is it is showing the same item instead of showing different ones.This is the output that I've been receiving.

This should output a different orgs but I keep getting the same output. I think something is wrong in the for loop and that YAHRA is the last item on my list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2025-01-17 02:29:29

我创建了一个与您的项目类似的副本,然后发现问题

in your model just remove "static" 
from
 private static String Org_Name;
 private static String Org_Logo;
to 
 private String Org_Name;
 private String Org_Logo;

I have created a copy similar to your project and then found Problem

in your model just remove "static" 
from
 private static String Org_Name;
 private static String Org_Logo;
to 
 private String Org_Name;
 private String Org_Logo;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文