RecyclerView 正在填充我列表中的最后一项(我得到相同的项目)
RecyclerView is populating with the last item on my list (I'm getting the same item)
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
@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);
<?
问题是它显示的是相同的项目而不是不同的 ones.This 是我一直收到的输出。
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.
我已经创建了一个类似于您的项目的副本,然后发现了问题
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;
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
@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);
<?
问题是它显示的是相同的项目而不是不同的 ones.This 是我一直收到的输出。
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.
我已经创建了一个类似于您的项目的副本,然后发现了问题
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;