我正在尝试从资产文件夹中的 json 文件中获取值并将其加载到我的简单列表视图中。代码中没有错误,我确实尝试记录所有状态。我可以在日志 cat 中看到所需的值,但是当我使用适配器时,屏幕会加载但我看不到数据。
我也在这个测试应用程序中使用片段,我总共有 4 个选项卡。在一个选项卡中,我想显示从 json 文件加载的所有数据。
我的适配器有问题吗?一些帮助将非常感激。
public class Test extends Fragment {
Context context;
// TextView tview;
ImageView iv;
ImageView iv1;
private static final String STATE = "state";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
View rootView = inflater.inflate(R.layout.xyz, container, false);
ListView lv1 = (ListView) rootView.findViewById(R.id.list);
TextView tv1 = (TextView) rootView.findViewById(R.id.rowidtext);
lv1.setAdapter(new SimpleAdapter(getActivity(), jsonlist, R.layout.xyz,
new String[] { STATE }, new int[] { R.id.rowidtext }));
try {
JSONObject json = new JSONObject(loadJSONFromAsset());
if (json != null) {
JSONArray array = json.getJSONArray("Information");
Log.i("Data1234", array.toString());
for (int i = 0; i <= array.length(); i++) {
JSONObject c = array.getJSONObject(i);
String vstate = c.getString(STATE);
Log.i("STATE", vstate);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(STATE, vstate);
jsonlist.add(map);
}
Log.i("Data Coming? ", jsonlist.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("Data Coming? ", jsonlist.toString());
return rootView;
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("testjson1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
日志猫:
10-28 22:43:08.364: D/dalvikvm(5806): GC_FOR_ALLOC freed 60K, 8% free 2779K/3016K, paused 23ms, total 25ms
10-28 22:43:08.364: I/dalvikvm-heap(5806): Grow heap (frag case) to 3.957MB for 1127536-byte allocation
10-28 22:43:08.474: D/dalvikvm(5806): GC_FOR_ALLOC freed 2K, 6% free 3878K/4120K, paused 100ms, total 100ms
10-28 22:43:08.804: D/gralloc_goldfish(5806): Emulator without GPU emulation detected.
10-28 22:43:13.845: I/Data1234(5806): [{"state":"JAVA","outdoorfun":"yes","additionalinfo":"{ search: 'online', website: 'http:\/\/states.arkansas.com', latitudeandlongitude: '93905, 63550}","cold":"no","type":"landlocked"},{"state":"ANDROID","outdoorfun":"no","additionalinfo":"{ search: 'online', website: 'http:\/\/states.chicago.com', latitudeandlongitude: '23905, 45355}","cold":"yes","type":"windy"},{"state":"CORE JAVA","outdoorfun":"no","additionalinfo":"{ search: 'online', website: 'http:\/\/states.chicago.com', latitudeandlongitude: '23905, 45355}","cold":"yes","type":"windy"},null]
10-28 22:43:13.845: I/STATE(5806): JAVA
10-28 22:43:13.845: I/STATE(5806): ANDROID
10-28 22:43:13.845: I/STATE(5806): CORE JAVA
10-28 22:43:13.855: W/System.err(5806): ***org.json.JSONException: Value at 3 is null.***
10-28 22:43:13.855: W/System.err(5806): at org.json.JSONArray.get(JSONArray.java:259)
10-28 22:43:13.855: W/System.err(5806): at org.json.JSONArray.getJSONObject(JSONArray.java:480)
10-28 22:43:13.855: W/System.err(5806): at com.mike.myapp.Train.onCreateView(Train.java:91)
10-28 22:43:13.855: W/System.err(5806): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-28 22:43:13.865: W/System.err(5806): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-28 22:43:13.875: W/System.err(5806): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
10-28 22:43:13.875: W/System.err(5806): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
10-28 22:43:13.875: W/System.err(5806): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
10-28 22:43:13.875: W/System.err(5806): at com.mike.myapp.MainActivity.onTabSelected(MainActivity.java:91)
10-28 22:43:13.875: W/System.err(5806): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:572)
10-28 22:43:13.875: W/System.err(5806): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1098)
10-28 22:43:13.875: W/System.err(5806): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
10-28 22:43:13.887: W/System.err(5806): at android.view.View.performClick(View.java:4240)
10-28 22:43:13.887: W/System.err(5806): at android.view.View$PerformClick.run(View.java:17721)
10-28 22:43:13.887: W/System.err(5806): at android.os.Handler.handleCallback(Handler.java:730)
10-28 22:43:13.895: W/System.err(5806): at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 22:43:13.895: W/System.err(5806): at android.os.Looper.loop(Looper.java:137)
10-28 22:43:13.895: W/System.err(5806): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-28 22:43:13.895: W/System.err(5806): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 22:43:13.905: W/System.err(5806): at java.lang.reflect.Method.invoke(Method.java:525)
10-28 22:43:13.905: W/System.err(5806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-28 22:43:13.905: W/System.err(5806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-28 22:43:13.905: W/System.err(5806): at dalvik.system.NativeStart.main(Native Method)