

Read the Json file in Android and Populate in ListView
source link: https://www.codesd.com/item/read-the-json-file-in-android-and-populate-in-listview.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Read the Json file in Android and Populate in ListView
I write this piece of code for reading json file and show it in listview but it return empty list . I would appreciate if anybody advise me. Thanks
eventList.json:
{
"events": [
{
"event": "Taste of day"
},
{
"event": "House Party at Park"
},
{
"event": "Farmers Markets"
},
{
"event": "Blues Festival Preview Events"
},
{
"event": "Cultural Alliance - Heritage and Fashion"
}
]
}
and this an Activity for reading and parsing json file :
public class EventSelectActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_select);
// Reading json file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"eventList.json")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
if ((br != null)) {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
JSONObject jsonObjMain = new JSONObject();
JSONArray jsonArray = jsonObjMain.getJSONArray("events");
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EventSelectActivity.this,
android.R.layout.simple_list_item_1, messages);
ListView list=(ListView)findViewById(R.id.eventList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(EventSelectActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
The variable sb
holds the consumed JSON data.
There was no reference to actual JSON object as it was empty.
JSONObject jsonObjMain = new JSONObject();
Change the code block:
try {
JSONObject jsonObjMain = new JSONObject(sb.toString());
JSONArray jsonArray = jsonObjMain.getJSONArray("events");
// ...
There is a undefined reference to the JSON field, "msg", maybe it should be "event"?
// Getting data from individual JSONObject
String message = jsonArray.getString(i);
messages.add(message);
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK