Jeet.Deir Jeet.Deir - 3 months ago 9
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(R.id.lv_sensor);

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


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

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lv_sensor"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

</ListView>


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

Sensor_out

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
json-ish
, as I wrote in the code. I was hoping there maybe some internal library that I may have missed!

Thanks.

Answer

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