gpt4 book ai didi

java - RecyclerView 并不总是显示结果

转载 作者:行者123 更新时间:2023-12-02 02:22:24 25 4
gpt4 key购买 nike

我遇到了这个奇怪的问题,RecyclerView 无法向我显示结果。我使用 Volley 来调用服务器上的 REST API,该 API 返回一个 json 文件。 Log.i() 向我显示我的结果每次都会被抓取,但它们并不总是显示在屏幕上。我想说他们出现的概率约为 30%。

fragment 列表

public class FragmentList extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_list, container, false);
RecyclerView recyclerView;
FoodAdapter adapter;

final List<Food> foodList = new ArrayList<>();
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

String url = "https://www.example.com";
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.i(TAG, response);
JSONArray foodResp = new JSONArray(response);
for (int i = 0; i < foodResp.length(); i++) {
JSONObject foodObj = foodResp.getJSONObject(i);
foodList.add(
new Food(
1,
R.drawable.ic_launcher_background,
foodObj.getString("mealTitle"),
foodObj.getString("mealDesc"))
);
}

} catch (Exception e) {
Log.i(TAG, "Error: " + e.getMessage());
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "err: " + error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("data", "value");

return params;
}
};
Volley.newRequestQueue(getActivity()).add(sr);

adapter = new FoodAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);

return v;
}

}

fragment_fragment_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.localfridge.localfridge.FragmentList">

<!-- TODO: Update blank fragment layout -->

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

FoodAdapter 只是以防万一

FoodAdapter.java

ublic class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {

private Activity mCtx;
private List<Food> foodList;

public FoodAdapter(Activity mCtx, List<Food> foodList) {
this.mCtx = mCtx;
this.foodList = foodList;
}

@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_item, null);

FoodViewHolder foodView = new FoodViewHolder(view);

return foodView;
}

@Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
Food food = foodList.get(position);
holder.txtFoodTitle.setText(food.getFoodTitle());
holder.txtFoodDesc.setText(food.getFoodDesc());
holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(food.getImage(), null));
}

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

class FoodViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView txtFoodTitle, txtFoodDesc, txtFoodLoc;

public FoodViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.foodImg);
txtFoodTitle = itemView.findViewById(R.id.foodTitle);
txtFoodDesc = itemView.findViewById(R.id.foodDesc);
}
}
}

最佳答案

as Volley 请求 是异步的。所以当你得到结果时填充你的适配器并设置适配器,意味着当 onResponse 被调用时

解决方案1

    @Override
public void onResponse(String response) {
try {
Log.i(TAG, response);
JSONArray foodResp = new JSONArray(response);
for (int i = 0; i < foodResp.length(); i++) {
JSONObject foodObj = foodResp.getJSONObject(i);
foodList.add(
new Food(
1,
R.drawable.ic_launcher_background,
foodObj.getString("mealTitle"),
foodObj.getString("mealDesc"))
);
}
adapter = new FoodAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);
} catch (Exception e) {
Log.i(TAG, "Error: " + e.getMessage());
}

}

更新

解决方案2

按照评论中的建议。您可以提前设置适配器,但在这种情况下您必须通知适配器数据已更改

@Override
public void onResponse(String response) {
try {
Log.i(TAG, response);
JSONArray foodResp = new JSONArray(response);
for (int i = 0; i < foodResp.length(); i++) {
JSONObject foodObj = foodResp.getJSONObject(i);
foodList.add(
new Food(
1,
R.drawable.ic_launcher_background,
foodObj.getString("mealTitle"),
foodObj.getString("mealDesc"))
);
}
if(adapter!=null)
{
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.i(TAG, "Error: " + e.getMessage());
}

}

关于java - RecyclerView 并不总是显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316446/

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