Leonard Febrianto
Leonard Febrianto

Reputation: 984

Populating Array2d into ListView

I have some trouble about this. So my problem is i want to put the result from json_encode into listview. Without long chit chat i'll show you this :

This is the result of json :

{
"length":
[
  {
      "kode_schedule":"sch0001",
      "kode_matkul":"TIB01",
      "title":"Basis Data",
      "ruang":"501",
      "latitude":"-6.18861653446272",
      "longitude":"106.801122526546",
      "lantai":"5",
      "hari":"Selasa",
      "jam":"17:45:00"
  },          
  {
      "kode_schedule":"sch0001",
      "kode_matkul":"TIB02",
      "title":"Pemrograman Berorientasi Objek",
      "ruang":"LABB",
      "latitude":"-6.18864706134991",
      "longitude":"106.801161122636",
      "lantai":"5",
      "hari":"Selasa",
      "jam":"19:30:00"
   }
]
}

This is my code so far :

class GetLength extends AsyncTask<String, String, String> {
JSONParser jParser = new JSONParser();
protected String doInBackground(String... params) {
            String title;
            String ruang;
            String lantai;
            String hari;
            String jam;
            String latitude, longitude;
            try {
                // Building Parameters
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("username", txtNim.getText().toString()));
                JSONObject json = jParser.makeHttpRequest("http://studentstracking.hol.es/installation.php", "POST", param);
                JSONArray array = json.getJSONArray("length");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject row = array.getJSONObject(i);
                    kode_matkul = row.getString("kode_matkul");
                    title = row.getString("title");
                    ruang = row.getString("ruang");
                    latitude = row.getString("latitude");
                    longitude = row.getString("longitude");
                    lantai = row.getString("lantai");
                    hari = row.getString("hari");
                    jam = row.getString("jam");
                    kode_matkul_list.add(kode_matkul);
                    title_list.add(title);
                    ruang_list.add(ruang);
                    latitude_list.add(latitude);
                    longitude_list.add(longitude);
                    lantai_list.add(lantai);
                    hari_list.add(hari);
                    jam_list.add(jam);
                    array2d[] array2ds = new array2d[i];
                    array2ds[i] = new array2d(kode_matkul, title, ruang, longitude, latitude, lantai, hari, jam);
                }

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
}

And this is the another class :

class array2d {
        String kode_matkuls;
        String titles;
        String ruangs;
        String lantais;
        String haris;
        String jams;
        String latitudes, longitudes;

        public array2d(String kode_matkuls, String titles, String ruangs, String longitudes, String latitudes, String lantais, String haris, String jams) {
            this.kode_matkuls = kode_matkuls;
            this.titles = titles;
            this.ruangs = ruangs;
            this.lantais = lantais;
            this.haris = haris;
            this.jams = jams;
            this.latitudes = latitudes;
            this.longitudes = longitudes;
        }
    }

And this is the method :

private void populate(ArrayList<String> array) {
        ListView showList = (ListView) findViewById(R.id.listView2);
        ArrayAdapter<String> shows = new ArrayAdapter<String>(
                getApplicationContext(),
                android.R.layout.simple_list_item_1, array);
        showList.setAdapter(shows);
    }

Anyone can fix this ? Or maybe there is other way to make it simple ? Just for simple, i want to that json show in listview.

Thanks for your cooperation

Upvotes: 0

Views: 57

Answers (2)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

You can try the following approach. This will make the solution much easier.

Step 1 : Create a CustomListViewAdapter class which should look something like this,

public class CustomListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView kode_matkul;
        TextView title;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        kode_matkul = (TextView) itemView.findViewById(R.id.kode_matkul);
        title = (TextView) itemView.findViewById(R.id.title);

        // Capture position and set results to the TextViews
        kode_matkul .setText(resultp.get("kode_matkul"));
        title.setText(resultp.get("title"));

        return itemView;
    }
}

Step 2 : For your custom adapter you need to specify a layout ( e.g. here I'm using listview_item.xml ) which will be the view inside the list. The layout could look something like this,

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

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

</LinearLayout>

Step 3 : In your activity Create a Custom List Adapter & Hashmap to bind and store the parsed JSON data

CustomListViewAdapter adapter;
ArrayList<HashMap<String, String>> arraylist;

Now every time your parse a JSON value put the parsed data inside the Hashmap

     for (int i = 0; i < array.length(); i++) {
     JSONObject row = array.getJSONObject(i);
     HashMap<String, String> map = new HashMap<String, String>();
     // Retrive JSON String
     map.put("kode_matkul ", row.getString("kode_matkul"));
     map.put("title ", row.getString("title "));
     ...........................................
     // Set the JSON Objects into the array
     arraylist.add(map);
   }

Step 4 : In your post execute method set the list to list view

@Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new CustomListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
        }

Upvotes: 1

Nithin Np
Nithin Np

Reputation: 190

//store it in a arraylist
ArrayList<String> my_array = new ArrayList<String>();
my_array.add("your json content");
//then set addapter to listview
ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.list,my_array);
listview.setAdapter(my_Adapter);

Upvotes: 0

Related Questions