gpt4 book ai didi

java - fragment 中的回收器 View 为空

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:30 25 4
gpt4 key购买 nike

我正在尝试解析 JSON 数据并使用 RecyclerView 将其显示在 fragment 中。但是,当我运行该应用程序时,我得到一个空白屏幕。我第一次使用 Activity 完成了这段代码,效果非常好。然后我将其转换为 Fragment。在 Fragment 中,它显示一个空白屏幕。

这是我的 fragment 的代码

public class Tab1 extends Fragment {
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tab1, container, false);
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(requireActivity());
parseJson();
return rootView;
}

private void parseJson(){
String url ="http://maranamassapp.cf/json_getdata.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("server_response");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject ser =jsonArray.getJSONObject(i);
String creatorname = ser.getString("filmname");
String imageUrl = ser.getString("filmimage");
String cat = ser.getString("filmcat");

mExampleList.add(new ExampleItem(imageUrl,creatorname,cat));
}

mExampleAdapter = new ExampleAdapter(getContext(),mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

mRequestQueue.add(request);
}
}

和适配器代码

public class ExampleAdapter extends RecyclerView.Adapter <ExampleAdapter.ExampleViewHolder>{
private Context mContext;
private ArrayList<ExampleItem> mExampleList;
public ExampleAdapter(Context context, ArrayList<ExampleItem> examplelist){
mContext =context;
mExampleList = examplelist;
}

@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.example_item,parent,false);
return new ExampleViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
String imageUrl = currentItem.getmImageUrl();
String creatorName = currentItem.getmCreator();
String cat =currentItem.getmCat();

holder.mTextViewCreator.setText(creatorName);
holder.mTextViewCat.setText(cat);
Glide.with(mContext)
.load(imageUrl)
.into(holder.mImageView);
}

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

public class ExampleViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextViewCreator;
public TextView mTextViewCat;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.image_view);
mTextViewCreator = itemView.findViewById(R.id.text_view_creator);
mTextViewCat = itemView.findViewById(R.id.text_view_cat);
}
}
}

有人可以帮忙吗?

最佳答案

我建议您实现如下所示的RecyclerView

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tab1, container, false);
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(requireActivity());

// Set the adapter here.
// Use getActivity() instead of getContext()
mExampleAdapter = new ExampleAdapter(getContext(),mExampleList);

parseJson();
return rootView;
}

private void parseJson(){
String url ="http://maranamassapp.cf/json_getdata.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("server_response");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject ser =jsonArray.getJSONObject(i);
String creatorname = ser.getString("filmname");
String imageUrl = ser.getString("filmimage");
String cat = ser.getString("filmcat");

mExampleList.add(new ExampleItem(imageUrl,creatorname,cat));
}

// Just call notifyDataSetChanged here
mExampleAdapter.notifyDataSetChanged();

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

mRequestQueue.add(request);
}

请注意,我在 onCreateView 中设置适配器,传递 getActivity() 作为上下文而不是 getContext。在 parseJson 函数中,当使用新添加的数据更新 mExampleList 时,我只是调用 notifyDataSetChanged

此外,检查您是否正确地从服务器获取响应,并且在解析响应时没有遇到任何异常。

希望这有效!

关于java - fragment 中的回收器 View 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52025555/

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