简体   繁体   English

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

[英]RecyclerView is populating with the last item on my list (I'm getting the same item)

The Recyclerview is populating with the last item in my list 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);

Here's my adapter onBindViewHolder这是我的适配器 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这是我的 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这是我的 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.问题是它显示的是相同的项目,而不是显示不同的项目。这是我收到的 output。

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.这应该是 output 一个不同的组织,但我一直得到相同的 output。我认为 for 循环中有问题,YAHRA 是我列表中的最后一项。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM