gpt4 book ai didi

java - Androidx RecycleView 不显示带有解析服务器的项目

转载 作者:行者123 更新时间:2023-12-02 09:36:17 27 4
gpt4 key购买 nike

这是我的回收 View 适配器

CategoryAdapter类

public class CategoryRAdapter extends RecyclerView.Adapter<CategoryRAdapter.MyViewHolder> {

private Context mcontext;
private List<Food> mData;
RequestOptions option;

public CategoryRAdapter(Context mcontext, List<Food> mData) {
this.mcontext = mcontext;
this.mData = mData;

option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_row, parent, false);

return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Food current = mData.get(position);
holder.tv_Name.setText(current.getName());
holder.tv_Rating.setText(current.getRating());
holder.tv_Descip.setText(current.getDescrip());
holder.tv_Category.setText(current.getCateg());
Glide.with(mcontext).load(mData.get(position).getImages()).apply(option).into(holder.img_Thumbnail);
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

TextView tv_Name;
TextView tv_Rating;
TextView tv_Descip;
TextView tv_Category;
ImageView img_Thumbnail;

public MyViewHolder(View itemView) {
super(itemView);

tv_Name = itemView.findViewById(R.id.food_name);
tv_Rating = itemView.findViewById(R.id.rating);
tv_Descip = itemView.findViewById(R.id.desc);
tv_Category = itemView.findViewById(R.id.category);
img_Thumbnail = itemView.findViewById(R.id.thumbnail);
}
}
}

这是我的模型

食品类

public class Food {
String Name;
String Images;
String Descrip;
String Rating;
String Categ;

public Food() {
}

public Food(String name, String images, String descrip, String rating, String categ) {
this.Name = name;
this.Images = images;
this.Descrip = descrip;
this.Rating = rating;
this.Categ = categ;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getImages() {
return Images;
}

public void setImages(String images) {
Images = images;
}

public String getDescrip() {
return Descrip;
}

public void setDescrip(String descrip) {
Descrip = descrip;
}

public String getRating() {
return Rating;
}

public void setRating(String rating) {
Rating = rating;
}

public String getCateg() {
return Categ;
}

public void setCateg(String categ) {
Categ = categ;
}
}

我的主要 Activity

CategoryActivity 类

public class CategoriaActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private List<Food> dbObjects;
private RecyclerView.Adapter adapter;
private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
TextView l_nombre,l_precio;
Button l_finalizar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categoria);
recyclerView = findViewById(R.id.recycle_id);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

dbObjects = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Category");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList, ParseException e) {

if (e == null) {

for (ParseObject obj : objList) {
Food food = new Food();
food.setName(obj.getString("Name"));
food.setDescrip(obj.getString("Descrip"));
food.setRating(obj.getString("Rating"));
food.setCateg(obj.getString("Categ"));
food.setImages(obj.getString("Images"));
dbObjects.add(food);
}
} else {
FancyToast.makeText(CategoriaActivity.this, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, true).show();
}
}
});

adapter = new CategoryRAdapter(this, dbObjects);
recyclerView.setAdapter(adapter);

FloatingActionButton fab = findViewById(R.id.shoppingFlot);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carritopop();
}
});
}

private void carritopop() {
dialogBuilder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.pop_cart,null);
l_finalizar = view.findViewById(R.id.l_finalizar);
l_finalizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pago();
}
});

dialogBuilder.setView(view);
dialog = dialogBuilder.create();
dialog.show();
}

private void pago() {
Intent intent = new Intent(CategoriaActivity.this, PagoActivity.class);
startActivity(intent);
}
}

我不知道代码有什么问题,回收 View 不起作用,它没有显示任何项目,如果有人可以帮助我并注意到我的代码中存在问题,我将非常感激

最佳答案

在查询完成回调中调用adapter.notifyDataSetChanged(),这将通知适配器列表已更改。发生这种情况是因为当您设置适配器时,由于查询在后台运行,因此列表为空。一旦完成,您必须告诉适配器新数据已添加到列表中:

if (e == null) {
for (ParseObject obj : objList) {
...
}
adapter.notifyDataSetChanged();
}

此外,请在发送查询之前创建适配器,否则如果在执行下一条语句之前加载数据,可能会导致 NullPointerException(这种情况极不可能发生,但为了安全起见不会有什么坏处)。

dbObjects = new ArrayList<>();
adapter = new CategoryRAdapter(this, dbObjects);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Category");

关于java - Androidx RecycleView 不显示带有解析服务器的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485266/

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