I have successfully produced a ListView, which takes JSON data from a remote MySQL database, and adds the 3 fields (employee no, employee name, and title) to an Output string called "employees". This displays on an Android phone, OK, but what I really want to do is display ONLY employee name and title - and then when this record in the list is clicked on, the I want to display ONLY employee no in the next Activity. Using Split String, I have managed to just show "employee no" in the next Activity, but that still leaves the problem of just showing employee name + title ONLY in the ListView. If I only add those 2 fields together in the Output string, then "employee no" would be "invisible" when single item clicked on.. Many thanks in advance! Here is the relevent code in my MainActivity..
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("employee name");
String number = jsonChildNode.optString("employee no");
String title = jsonChildNode.optString("title");
String outPut = name + "-" + title + "-" + number;
employeeList.add(createEmployee("employees", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_activated_1,
new String[] { "employees" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
You have to create an object of Employee which has three fields
Then fill an ArrayList with the data returned from the server, and populate it using a Custom Adapter. This way you could manage what should appear in the listview item.
hope it'll help. If there is something not obvious do not hesitate to ask. :)
Edit 1:
Here is an example :
The code in onCreate in MainActivity
The custom adapter
custom_row.xml
Edit 2:
To integrate with this code convert your employeeList from
to
Here is an example :
hope that'll help you