chikwapuro
chikwapuro

Reputation: 1354

how do i displaying JSON results in a ListView

i have the following Code and lists displaying wrong results on my Listview.

public class ZonesActivity extends AppCompatActivity {

ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zones);

    String myJSONString = null;
    String myJSONObject = null;

    ArrayList<Zones> allZones = new ArrayList<Zones>();

    try {
        InputStream inputStream = getAssets().open("zonesjson.json");
        int sizeOfJSONFile = inputStream.available();

        byte[] bytes = new byte[sizeOfJSONFile];

        inputStream.read(bytes);

        //close the input stream
        inputStream.close();
        myJSONString = new String(bytes, "UTF-8");
        myJSONObject = new JSONObject(myJSONString).toString();


    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }


    try {
        JSONObject jsonObjMain = new JSONObject(myJSONObject);

        // Creating JSONArray from JSONObject
        JSONArray jsonArray = jsonObjMain.getJSONArray("zones");

        for (int i = 0; i < jsonArray.length(); i++) {

            // Creating JSONObject from JSONArray
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            // Getting data from individual JSONObject
            String zname = jsonObj.getString("zname");
            String location = jsonObj.getString("location");
            String pastname = jsonObj.getString("pastname");
            int starttime = jsonObj.getInt("starttime");
            int endtime = jsonObj.getInt("endtime");
            String contactdetails = jsonObj.getString("contactdetails");
            String address = jsonObj.getString("address");

            Zones zonesList = new Zones();
            zonesList.setZoneName(zname);
            zonesList.setZoneLocation(location);
            zonesList.setZonePastor(pastname);
            zonesList.setZoneStartTime(starttime);
            zonesList.setZoneEndTime(endtime);
            zonesList.setZoneContactNumber(contactdetails);
            zonesList.setZoneAddress(address);

            allZones.add(zonesList);
        }


        mListView = (ListView)findViewById(R.id.zoneListView1);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, allZones);
        mListView.setAdapter(arrayAdapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }



}

}

its Displaying the following results on my emulator: please assist where i am getting is wrong

enter image description here

Upvotes: 0

Views: 46

Answers (1)

David Rauca
David Rauca

Reputation: 1583

You should create a custom array adapter. The default one doesn't know how to display an entity "Zones", so it displays zones.toString() for each item.

Hope that this example to help you:

public class ZonesAdapter extends ArrayAdapter<Zones> {

    public ZonesAdapter(Context context, int resource, List<Zones> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.zones_item_layout, parent, false);
        }
        Zones item = getItem(position);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(item.getZoneName());
        // ...
        // Set other fields
        // ...
        return convertView;
    }
}

Then

ZonesAdapter arrayAdapter = new ZonesAdapter(this, R.layout.zones_item_layout, allZones);
mListView.setAdapter(arrayAdapter);

zones_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    // Add other views

</LinearLayout>

Upvotes: 1

Related Questions