gpt4 book ai didi

android - 如何使用 volley 在 recyclerview 中填充 cardview - 它没有获取 JSON?

转载 作者:太空狗 更新时间:2023-10-29 14:47:17 24 4
gpt4 key购买 nike

我正在使用以下类,它们可以很好地处理静态数据。

这是我的 JSON 响应

[{"place_name":"Udon House"},{"place_name":"Casa Biance"},{"place_name":"Pizza hut"},{"place_name":"Pearl Continental "},{"place_name":"Lahore Meuseum"},{"place_name":"Shalimar Gardens"}]

这是我的 POJO 类

public class places {

//Variable in json

public String place_name;

//Getters and setters

public String getPlace_name() {
return place_name;
}

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

}

这是我的 CustomAdapter 类

public class placesAdapter extends 
RecyclerView.Adapter<placesAdapter.ViewHolder> {

places places;
private Context context;
private List<places> places_list;
placesAPI p;

public placesAdapter(List<places> places)
{
this. places_list = places;
}

@Override
public placesAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(placesAdapter.ViewHolder viewHolder, int i) {

places_list.get(i);

viewHolder.place_name.setText(places_list.get(i).getPlace_name());
}

@Override
public int getItemCount() {
if (places_list != null) {
return places_list.size();
}
return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView place_name;

public ViewHolder(View view) {
super(view);

place_name = (TextView)view.findViewById(R.id.tv_blah);
}
}

}

这是我的 fragment 类

public class BlankFragment extends Fragment {
List<places> placesData;
//placesAdapter adapter;
RecyclerView rv;

public BlankFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
//RecyclerView.Adapter<String> adapter = new Adapter<>(getActivity(), R.layout.custom_tab);


LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);


final placesAdapter rvAdapter = new placesAdapter(getActivity(), placesData);
rv.setAdapter(rvAdapter);
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String url = "http://192.168.10.5/tourist/placeTest.php";

JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());

try {

List<places> placesArray = new ArrayList<places>();

JSONArray jsonArray = response.getJSONArray(response.toString());

for (int i = 0; i < response.length(); i++) {

JSONObject placeObject = jsonArray.getJSONObject(i);
places p = new places();
p.place_name = placeObject.getString("place_name");
placesArray.add(p);
}

rvAdapter.notifyDataSetChanged();

}
catch (JSONException e) {
e.printStackTrace();
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
requestQueue.add(req);
// Adding request to request queue
return rootView;
}
}

android moniter 给我这个:

07-25 12:59:00.048 830-830/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
07-25 12:59:00.923 513-572/? E/KeyguardUpdateMonitor: Object tried to add another callback
java.lang.Exception: Called by
at com.android.internal.policy.impl.keyguard.KeyguardUpdateMonitor.registerCallback(KeyguardUpdateMonitor.java:1107)
at com.android.internal.policy.impl.keyguard.KeyguardSelectorView.onResume(KeyguardSelectorView.java:331)
at com.android.internal.policy.impl.keyguard.KeyguardHostView.onScreenTurnedOn(KeyguardHostView.java:1118)
at com.android.internal.policy.impl.keyguard.KeyguardViewManager.onScreenTurnedOn(KeyguardViewManager.java:404)
at com.android.internal.policy.impl.keyguard.KeyguardViewMediator.handleNotifyScreenOn(KeyguardViewMediator.java:1767)
at com.android.internal.policy.impl.keyguard.KeyguardViewMediator.access$2300(KeyguardViewMediator.java:106)
at com.android.internal.policy.impl.keyguard.KeyguardViewMediator$4.handleMessage(KeyguardViewMediator.java:1413)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:60)
07-25 12:59:00.935 135-9746/? E/AudioMTKHardware: setParameters() still have param.size() = 1, remain param = "screen_state=on"
07-25 12:59:05.455 22623-22623/? E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.a
07-25 12:59:06.122 20191-21273/? E/dalvikvm: Could not find class 'android.telephony.CellInfoWcdma', referenced from method nqp.a
07-25 12:59:06.196 22623-22623/? E/dalvikvm: Could not find class 'com.facebook.common.jobscheduler.compat.JobServiceCompat', referenced from method com.facebook.common.jobscheduler.compat.ServiceInfoParser.<init>
07-25 12:59:06.846 22603-22603/? E/dalvikvm: Could not find class 'android.content.pm.LauncherApps', referenced from method com.google.android.apps.gsa.search.shared.multiuser.l.connect
07-25 12:59:06.993 20191-21273/? E/dalvikvm: Could not find class 'android.telephony.CellInfoWcdma', referenced from method nqp.c
07-25 12:59:07.369 20191-21273/? E/dalvikvm: Could not find class 'android.telephony.CellInfoWcdma', referenced from method nqq.a
07-25 12:59:10.201 22674-22674/com.nuku.mc.populate_recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.nuku.mc.populate_recyclerview.BlankFragment$1.onResponse(BlankFragment.java:79)
at com.nuku.mc.populate_recyclerview.BlankFragment$1.onResponse(BlankFragment.java:64)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
07-25 12:59:10.348 513-574/? E/AppErrorDialog: Failed to get ILowStorageHandle instance
07-25 12:59:11.632 513-811/? E/CellLocation: create GsmCellLocation
07-25 12:59:11.651 513-571/? E/CellLocation: create GsmCellLocation
07-25 12:59:11.663 843-1303/? E/CellLocation: create GsmCellLocation
07-25 12:59:11.704 513-571/? E/CellLocation: create GsmCellLocation

最佳答案

您必须预先初始化您的 placesData 列表,然后使用它从您的 json 数组中添加对象。注意标有 //IMPORTANT!!! 的行:

public class BlankFragment extends Fragment {
List<places> placesData = new ArrayList<>(); // IMPORTANT!!!

//...
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());

try {
for (int i = 0; i < response.length(); i++) {
JSONObject placeObject = jsonArray.getJSONObject(i);
places p = new places();
p.place_name = placeObject.getString("place_name");
placesData.add(p); // IMPORTANT!!!
}

rvAdapter.notifyDataSetChanged();

}
catch (JSONException e) {
e.printStackTrace();
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
requestQueue.add(req);
return rootView;
}
}

此外,您不需要 onResponse 方法中的局部变量 placesArray

关于android - 如何使用 volley 在 recyclerview 中填充 cardview - 它没有获取 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556136/

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