Jeet.Deir Jeet.Deir - 1 year ago 94
Android Question

Pretty Print JSON-ish in List

So as a part of my Android application, I need to show a list that has information about all the sensors in the device.

Here is the code I am using to do the same.

//Read sensors and populate list
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getSensorList(Sensor.TYPE_ALL);

LayoutInflater inflater = getLayoutInflater();
View convertView = inflater.inflate(R.layout.sensor_list, null);
ListView lv = (ListView) convertView.findViewById(;

AlertDialog.Builder alertDialog = new AlertDialog.Builder(CollectActivity.this);
alertDialog.setTitle("Available Sensor List");
lv.setAdapter(new ArrayAdapter < Sensor > (CollectActivity.this, android.R.layout.simple_list_item_1, sensor));;

And the XML file is as simple as it can get viz.

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android=""


The output is like this (JSON-ish?):


My question is, how can I pretty print it, that is making human readable with the least amount of code? Time limitation doesn't allow me to create another activity etc. with lots of parsing logic etc.
Edit: The output is
, as I wrote in the code. I was hoping there maybe some internal library that I may have missed!


Answer Source

Simply write your own Adapter for ArrayAdapter<Sensor> (or use a RecyclerView)

What you see is not JSON, it is the Sensor.toSrtring() value. And one TextView is used for android.R.layout.simple_list_item_1

You have to create your own XML layout for the data you want to display, then implement getView in the Adapter to bind a Sensor object to the Views

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download