我正在尝试解析JSON数据,并使用RecyclerView
将其显示在片段中。但是,当我运行该应用程序时,出现黑屏。我第一次使用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);
}
}
}
有人可以帮忙吗?
答案 0 :(得分:0)
我建议您像下面这样实现declare namespace some {
type Api = {
foo(n: number): string;
bar(s: string, n: number): string
}
function api(): Api;
}
function wrapBuilder<T extends Record<keyof T, (...a: any[]) => any>>(fn: () => T) {
return function <K extends keyof T>(functionName: K): T[K] {
return ((...args: any[]) => {
try {
return fn()[functionName](...args);
} catch (error) {
throw error;
}
});
}
}
const wrapCallByName = wrapBuilder(() => some.api());
const foo = wrapCallByName('foo');
foo(1); //returns string
const bar = wrapCallByName('bar');
bar('', 1); //returns string
。
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
用新添加的数据更新时,我只是在调用notifyDataSetChanged
。
此外,请检查从服务器正确获取响应,并且在解析响应时是否没有任何异常。
希望这行得通!