public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "Name", "Entry" };
int[] to = { android.R.id.text2, android.R.id.text1 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list =
new ArrayList<Map<String, String>>();
list.add(putData("Bulbasaur", "001"));
list.add(putData("Ivysaur", "002"));
list.add(putData("Venusaur", "003"));
return list;
}
private HashMap<String, String> putData(String name, String entry) {
HashMap<String, String> item = new HashMap<String,
String>();
item.put("Name", name);
item.put("Entry", entry);
return item;
}
What I aim to do is add a JSON reference in either the putData or buildData classes listed above. I have another class (below) that I will use to reference the locally stored JSON file.
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("pokelist.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;
}
The problem I'm having is that, no matter what I seem to do, the data from the JSON file will never be displayed in the ListActivity. I know the ListActivity works, but I'm not entirely sure how I should go about implementing the HashMap with data from a JSON file.
Can someone provide me with some guidance please. Please don't provide links; I've been looking everywhere all day for some form of example on the matter, to no avail. I could use some help that I could understand.
To clarify What I want to do is load the JSON file, from the HDD, and put the key values into a HashMap; which will then be displayed in my ListView (list).
JSON code:
{
"pokemon": [
{"Entry":"001",
"Name":"Bulbasaur", "Type":"1"},
{"Entry":"002",
"Name":"Ivysaur" , "Type":"1"},
{"Entry":"003",
"Name":"Venusaur", "Type":"1"}
]
}
Recent Log Messages
12-04 22:00:53.652: E/Trace(2093): error opening trace file: No such file or directory (2) 12-04 22:00:53.712: W/System.err(2093): java.io.FileNotFoundException: pokelist.json 12-04 22:00:53.763: W/System.err(2093): at android.content.res.AssetManager.openAsset(Native Method) 12-04 22:00:53.763: W/System.err(2093): at android.content.res.AssetManager.open(AssetManager.java:315) 12-04 22:00:53.796: W/System.err(2093): at android.content.res.AssetManager.open(AssetManager.java:289) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.loadJSONFromAsset(MainActivity.java:78) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.buildData(MainActivity.java:44) 12-04 22:00:53.796: W/System.err(2093): at com.example.myjsonproject.MainActivity.onCreate(MainActivity.java:27) 12-04 22:00:53.796: W/System.err(2093): at android.app.Activity.performCreate(Activity.java:5104) 12-04 22:00:53.796: W/System.err(2093): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 12-04 22:00:53.796: W/System.err(2093): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-04 22:00:53.803: W/System.err(2093): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 12-04 22:00:53.836: W/System.err(2093): at android.os.Handler.dispatchMessage(Handler.java:99) 12-04 22:00:53.836: W/System.err(2093): at android.os.Looper.loop(Looper.java:137) 12-04 22:00:53.836: W/System.err(2093): at android.app.ActivityThread.main(ActivityThread.java:5041) 12-04 22:00:53.836: W/System.err(2093): at java.lang.reflect.Method.invokeNative(Native Method)
Thank you.