gpt4 book ai didi

首次启动 fragment 时,Android ListView 不显示内容

转载 作者:行者123 更新时间:2023-11-30 01:16:56 25 4
gpt4 key购买 nike

我在 fragment 中有一个 ListView ,其中有一个自定义适配器从我的 mysql 数据库加载产品列表。当我第一次从我的抽屉导航 Activity 导航到该 fragment 时,它没有显示任何项目,但在日志中打印了相同的项目。但是当第二次启动同一个 fragment 时,所有项目都是可见的。这是我在 fragment 的 onCreateView() 中的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_complain, container, false);
sharedPreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
new BackgroundViewComplainTask().execute();
jsonString = sharedPreferences.getString("complain", "");
complainList = (ListView)view.findViewById(R.id.list);
addComplain = (Button)view.findViewById(R.id.addComplain);
String productName, serial, complainType, complainMessage, added_on;
viewComplainAdapter = new ViewComplainAdapter(this.getActivity(), R.layout.complain_layout);
try {
Log.d("json",jsonString);
jsonObject = new JSONObject(jsonString);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
ViewComplainsList viewComplainsList;

while (count<jsonArray.length()){
JSONObject JO = jsonArray.getJSONObject(count);
productName = JO.getString("productName");
serial = JO.getString("serial");
complainType = JO.getString("complainType");
complainMessage = JO.getString("complainMessage");
added_on = JO.getString("added_on");
viewComplainsList = new ViewComplainsList(complainMessage, complainType, productName, serial, added_on);
viewComplainAdapter.add(viewComplainsList);
count++;
}
complainList.setAdapter(viewComplainAdapter);
viewComplainAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
if(jsonString.isEmpty()){
Toast.makeText(getActivity(),"No Complains found",Toast.LENGTH_LONG).show();
this.startActivity(new Intent(getActivity(), MainActivity.class));
}
addComplain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_activity_frame, new AddComplainFragment()).addToBackStack("complainFragment").commit();
}
});
return view;
}

jsonString 会打印在日志中。

这是我的 Activity 日志:

06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: org.json.JSONException: End of input at character 0 of 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at org.json.JSONObject.(JSONObject.java:156) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at org.json.JSONObject.(JSONObject.java:173) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at com.example.thadaninilesh.productapp.ViewComplainFragment.onCreateView(ViewComplainFragment.java:72) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.Fragment.performCreateView(Fragment.java:2053) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.BackStackRecord.run(BackStackRecord.java:834) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.os.Looper.loop(Looper.java:135) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at java.lang.reflect.Method.invoke(Native Method) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 06-08 15:08:27.450 10110-10110/com.example.thadaninilesh.productapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 06-08 15:08:27.480 10110-10394/com.example.thadaninilesh.productapp D/response: {"server_response":[{"serial":"123456789QWERT","complainType":"qwerty","complainMessage":"The body is completely damaged","added_on":"08-06-2016","productName":"Acer Travelmate"},{"serial":"123456789QWERT","complainType":"qwerty","complainMessage":"The body is completely damaged","added_on":"08-06-2016","productName":"Acer Travelmate"},{"serial":"123456789QWERT","complainType":"qwerty","complainMessage":"The body is completely damaged","added_on":"08-06-2016","productName":"Acer Travelmate"},{"serial":"123456789QWERT","complainType":"qwertyu","complainMessage":"The body is completely damaged","added_on":"08-06-2016","productName":"Acer Travelmate"},{"serial":"123456789QWERT","complainType":"Physical Condition","complainMessage":"--","added_on":"08-06-2016","productName":"Acer Travelmate"},{"serial":"12345678DFGBH","complainType":"sdcwsdw","complainMessage":"wdwdf","added_on":"08-06-2016","productName":"Galaxy J7"},{"serial":"12345678DFGBH","complainType":"asd","complainMessage":"--","added_on":"08-06-2016 08:06","productName":"Galaxy J7"}]}

最佳答案

我解决了...问题是执行顺序。调用 Asynctask 后,我没有等待它完成,而是在从服务器获取 json 字符串之前填充 ListView 。通过在 Asynctask 的 onPostExecute 方法中填充 ListView 来解决它。感谢那些试图帮助我的人。

关于首次启动 fragment 时,Android ListView 不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711195/

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