gpt4 book ai didi

java - getter 和 setter 在 recyclerview android 中没有按预期工作

转载 作者:行者123 更新时间:2023-11-29 10:10:06 24 4
gpt4 key购买 nike

我正在从 volley post 请求中获取 json 值。我正在使用 setter 方法将这些值添加到列表中。当我在适配器 onBindViewholder() 方法中检索值并将其显示在 recyclerview 中时,结果未按预期显示:

下面的代码是指从 MainActivity.java 中的 volley 请求和响应向列表添加值:

    private ProductsPojo pojo;
public static ProductsAdapter productsAdapter;
private List<ProductsPojo> pojoList;

pojo = new ProductsPojo();
pojoList = new ArrayList<>();

StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Log.d("Appet8","Products response:"+response.toString());

try {
JSONObject jsonObject = new JSONObject(response);
JSONArray products = jsonObject.getJSONArray("products");

for (int i=0;i<products.length();i++) {
JSONObject product_object = products.getJSONObject(i);
String name = product_object.getString("name");
String price = product_object.getString("price");
String product_id = product_object.getString("id");
String sessionname = product_object.getString("sessionname");
String image = product_object.getString("image");
String categoryname = product_object.getString("categoryname");

pojo.setName(product_object.getString("name"));
pojo.setImage(product_object.getString("image"));
pojoList.add(pojo);

}
productsAdapter = new ProductsAdapter(pojoList,getApplicationContext());

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("customer_id", customer_id);
return params;
}
};

AppController.getInstance().addToRequestQueue(request,tag_request);

下面的代码是指在 ProductFragment.java 中将适配器设置为 recyclerview:

private GridLayoutManager layoutManager;
private RecyclerView recyclerView;

recyclerView = (RecyclerView) view.findViewById(R.id.productList);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(getActivity().getApplicationContext(),3);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(MainActivity.productsAdapter);

下面的代码引用了显示值的适配器类,ProductsAdapter.java:

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductsViewHolder> {

private List<ProductsPojo> productList;
private Context context;

public ProductsAdapter(List<ProductsPojo> productList,Context context) {
this.productList=productList;
this.context = context;
}

@Override
public ProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.products_list, parent, false);
ProductsViewHolder holder = new ProductsViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(final ProductsViewHolder holder,final int position) {

final ProductsPojo pojo = productList.get(position);
Log.d("Appet8","Name:"+pojo.getName());

holder.vTitle.setText(pojo.getName());

holder.vTitle.setTypeface(MainActivity.font);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pojo.setSelected(!pojo.isSelected());
holder.itemView.setBackgroundColor(pojo.isSelected() ? Color.parseColor("#4D79CF08") : Color.parseColor("#2D6F6F6F"));

if(pojo.isSelected()) {
holder.selected.setVisibility(View.VISIBLE);
} else if(!pojo.isSelected()) {
holder.selected.setVisibility(View.GONE);
}
}
});
}

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

public static class ProductsViewHolder extends RecyclerView.ViewHolder {

protected TextView vTitle;
protected ImageView image,selected;
protected CardView product_card;

public ProductsViewHolder(View v) {
super(v);
vTitle = (TextView) v.findViewById(R.id.title);
image = (ImageView) v.findViewById(R.id.product);
product_card = (CardView) v.findViewById(R.id.product_card);
selected = (ImageView) v.findViewById(R.id.selected);
}
}

}

这是我从 volley 请求中得到的响应:

{
"products":[
{
"name":"Idli",
"price":"120",
"id":"Fi2mYuQA",
"sessionname":"Breakfast",
"image":"VCYwmSae2BShoshone_Falls-1200px.jpeg",
"categoryname":"Veg"
},
{
"name":"Meals123",
"price":"200",
"id":"bmF8Is1Y",
"sessionname":"Dinner",
"image":"sIe8JBFzaRstock-photo-115193575.jpg",
"categoryname":"Non Veg"
},
{
"name":"Dosa",
"price":"100",
"id":"e9sWHV4A",
"sessionname":"Breakfast",
"image":"j8nu4GpVa7Shoshone_Falls-1200px.jpeg",
"categoryname":"Veg"
},
{
"name":"Coca",
"price":"40",
"id":"0oJDfdCz",
"sessionname":"Cold Drinks",
"image":"LrkS8QpAs7Shoshone_Falls-1200px.jpeg",
"categoryname":"Veg"
},
{
"name":"ICe",
"price":"100",
"id":"2ykEgtSs",
"sessionname":null,
"image":"KtPX9C26oRShoshone_Falls-1200px.jpeg",
"categoryname":"Veg"
}
]
}

This is the output i am getting. Item names are repeated.

这是我得到的输出。项目名称重复。

以下代码引用 ProductsPojo.java:

public class ProductsPojo {
public String name;
public String image;
private boolean isSelected = false;

public void setName(String name) {
this.name = name;
}

public void setImage(String image) {
this.image = image;
}

public String getName() {
return name;
}

public String getImage() {
return image;
}

public void setSelected(boolean selected) {
isSelected = selected;
}


public boolean isSelected() {
return isSelected;
}
}

最佳答案

在我看来你只创建了一个 ProductsPojo 实例,在这里:

pojo = new ProductsPojo();

然后在循环中不断修改这个实例,然后一次又一次地将它添加到列表中。这样,您最终在列表中得到的相同项目(最后一个)与您在响应中获得的对象数量一样多。

您想要做的可能是每次都在 for 循环的开头创建一个新的 ProductsPojo,如下所示:

for (int i=0;i<products.length();i++) {
ProductsPojo pojo = new ProductsPojo();

JSONObject product_object = products.getJSONObject(i);
String name = product_object.getString("name");
String price = product_object.getString("price");
String product_id = product_object.getString("id");
String sessionname = product_object.getString("sessionname");
String image = product_object.getString("image");
String categoryname = product_object.getString("categoryname");

pojo.setName(product_object.getString("name"));
pojo.setImage(product_object.getString("image"));
pojoList.add(pojo);
}

关于java - getter 和 setter 在 recyclerview android 中没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com