Reputation: 3783
I have a JSON like this:
[{"message":"Hello","id":1,"name":"Hello"},{"message":"James","id":2,"name":"Smith"}]
Which I use to populate an ArrayList<HashMap<String, String>>
in order to diplay the json's items into a ListView.
This is the code I am currently using:
private void createListView(){
for(int i = 0 ; i < json.length() ; i++){
try{
JSONObject temp = json.getJSONObject(i);
HashMap<String, String> map = new HashMap<>();
map.put("name", temp.getString("name"));
map.put("message", temp.getString("message"));
data.add(map);
}catch (JSONException e){
e.printStackTrace();
}
}
String[] from = new String[]{"name", "message"};
int to[] = new int[]{android.R.id.text1};
ListAdapter adapter = new SimpleAdapter(getBaseContext(), data, android.R.layout.simple_list_item_1, from, to);
setListAdapter(adapter);
Log.d("Debug", json.toString());
}
This code almost work, but not the way I would. It display only the name field.
How can I modify it in order to display both the message and name field ?
Upvotes: 0
Views: 225
Reputation: 2757
Change this line
int to[] = new int[]{android.R.id.text1};
to
int to[] = new int[]{android.R.id.text1,android.R.id.text2};
Hope this will helps you.
Upvotes: 2
Reputation: 5542
SimpleAdapter playlistadapter = new SimpleAdapter(getActivity().getApplicationContext(), songsList, R.layout.file_view,
new String[] { "songTitle","songAlbum", "songartist","songPath" }, new int[] { R.id.checkTextView, R.id.text2, R.id.text3, R.id.text4 });
playlistadapter.setViewBinder(new SimpleAdapter.ViewBinder(){
boolean setViewValue(View view, Object data, String textRepresentation){
if(view.getId () == R.id.songartist){
view.setText("("+textRepresentation+")");
}
}
});
Upvotes: 1
Reputation: 7653
Use "android.R.layout.simple_list_item_2" instead of "android.R.layout.simple_list_item_1"
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
Upvotes: 1
Reputation: 7131
Error in this line
int to[] = new int[]{android.R.id.text1};
Here you are mention only one control . I guess that is for message. You need to add another one control for Name
.
Upvotes: 1
Reputation: 132982
It display only the message field.
Because using android.R.layout.simple_list_item_1
for row which contain only on TextView with android.R.id.text1
id
How can I modify it in order to display both the message and name field ?
Use android.R.layout.simple_list_item_2
as layout for rows and change to
array :
int to[] = new int[]{android.R.id.text1,android.R.id.text2};
Upvotes: 2