我目前正在开发一个物理应用程序,它应该显示一个公式列表,甚至解决其中的一些(唯一的问题是 ListView)
I'm currently developing a physics app that is supposed to show a list of formulas and even solve some of them (the only problem is the ListView)
这是我的主要布局
<LinearLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:measureWithLargestChild="false" android:orientation="vertical" tools:context=".CatList" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/titlebar" > <TextView android:id="@+id/Title1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/app_name" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#ff1c00" android:textIsSelectable="false" /> </RelativeLayout> <ListView android:id="@+id/listFormulas" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>这是我的主要活动
package com.wildsushii.quickphysics; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONException; import org.json.JSONObject; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.res.AssetManager; import android.view.Menu; import android.widget.ListView; public class CatList extends Activity { public static String AssetJSONFile (String filename, Context context) throws IOException { AssetManager manager = context.getAssets(); InputStream file = manager.open(filename); byte[] formArray = new byte[file.available()]; file.read(formArray); file.close(); return new String(formArray); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cat_list); ListView categoriesL = (ListView)findViewById(R.id.listFormulas); ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>(); Context context = null; try { String jsonLocation = AssetJSONFile("formules.json", context); JSONObject formArray = (new JSONObject()).getJSONObject("formules"); String formule = formArray.getString("formule"); String url = formArray.getString("url"); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } //My problem is here!! } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.cat_list, menu); return true; } }我实际上知道我可以在不使用 JSON 的情况下完成此操作,但我需要更多练习解析 JSON.顺便说一句,这是 JSON
I actually know I can make this without using JSON but I need more practice parsing JSON. By the way, this is the JSON
{ "formules": [ { "formule": "Linear Motion", "url": "qp1" }, { "formule": "Constant Acceleration Motion", "url": "qp2" }, { "formule": "Projectile Motion", "url": "qp3" }, { "formule": "Force", "url": "qp4" }, { "formule": "Work, Power, Energy", "url": "qp5" }, { "formule": "Rotary Motion", "url": "qp6" }, { "formule": "Harmonic Motion", "url": "qp7" }, { "formule": "Gravity", "url": "qp8" }, { "formule": "Lateral and Longitudinal Waves", "url": "qp9" }, { "formule": "Sound Waves", "url": "qp10" }, { "formule": "Electrostatics", "url": "qp11" }, { "formule": "Direct Current", "url": "qp12" }, { "formule": "Magnetic Field", "url": "qp13" }, { "formule": "Alternating Current", "url": "qp14" }, { "formule": "Thermodynamics", "url": "qp15" }, { "formule": "Hydrogen Atom", "url": "qp16" }, { "formule": "Optics", "url": "qp17" }, { "formule": "Modern Physics", "url": "qp18" }, { "formule": "Hydrostatics", "url": "qp19" }, { "formule": "Astronomy", "url": "qp20" } ] }我尝试了很多东西,甚至删除了整个项目以创建一个新项目:(
I have tried a lot of things and even delete the entire project to make a new one :(
推荐答案正如 Faizan 在 他们在这里的回答:
As Faizan describes in their answer here:
首先使用以下代码从您的资产文件中读取 Json 文件.
First of all read the Json File from your assests file using below code.
然后你可以简单地读取这个函数返回的字符串
and then you can simply read this string return by this function as
public String loadJSONFromAsset() { String json = null; try { InputStream is = getActivity().getAssets().open("yourfilename.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; }并像那样使用这种方法
try { JSONObject obj = new JSONObject(loadJSONFromAsset()); JSONArray m_jArry = obj.getJSONArray("formules"); ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> m_li; for (int i = 0; i < m_jArry.length(); i++) { JSONObject jo_inside = m_jArry.getJSONObject(i); Log.d("Details-->", jo_inside.getString("formule")); String formula_value = jo_inside.getString("formule"); String url_value = jo_inside.getString("url"); //Add your values in your `ArrayList` as below: m_li = new HashMap<String, String>(); m_li.put("formule", formula_value); m_li.put("url", url_value); formList.add(m_li); } } catch (JSONException e) { e.printStackTrace(); }有关 JSON 的更多详细信息阅读此处
For further details regarding JSON Read HERE
更多推荐
如何将资产文件夹中的本地 JSON 文件解析为 ListView?
发布评论