Kim Ryan Ronquillo Kim Ryan Ronquillo - 12 days ago 5
Android Question

JSON object to JSON Array to Arraylist to Arraydapter

i want to ask how to display my array list, i get the data from my webhost database and pass it to a JSON Object, then i used a JSON Array to make my objects as an array after that i pass it an ArrayList to display all the data's that i get, if i print the output of ArrayList here's what i get..

I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"Oct 31, 2016","checkno":"123","datepaid":"Oct 31, 2016","clientname":"naruto","bank":"bdo","amount":"100","status":"UNFINISHED"}
I/System.out: {"checkdate":"qwe","checkno":"123","datepaid":"qwe","clientname":"qwe","bank":"qwe","amount":"123","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 5, 2016","checkno":"1400056800","datepaid":"Nov 6, 2016","clientname":"Gelli Espiritu","bank":"BDO","amount":"123","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 5, 2016","checkno":"1400458800","datepaid":"Nov 5, 2016","clientname":"qwerty","bank":"east west","amount":"123","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 5, 2016","checkno":"1400458800","datepaid":"Nov 5, 2016","clientname":"qwerty","bank":"east west","amount":"123","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 6, 2016","checkno":"123456789","datepaid":"Nov 8, 2016","clientname":"Naruto","bank":"MAYBANK","amount":"123456789","status":"UNFINISHED"}
I/System.out: {"checkdate":"January 17, 2016","checkno":"13554400","datepaid":"January 20, 2016","clientname":"Mark Sample","bank":"BPI","amount":"100000","status":"UNFINISHED"}
I/System.out: {"checkdate":"January 17, 2016","checkno":"13554400","datepaid":"January 20, 2016","clientname":"Mark Sample","bank":"BPI","amount":"100000","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 6, 2016","checkno":"1401234","datepaid":"Nov 6, 2016","clientname":"GELLI gumana ka","bank":"BPI","amount":"1234","status":"UNFINISHED"}
I/System.out: {"checkdate":"March 7, 2016","checkno":"98671278","datepaid":"March 10, 2016","clientname":"Ben Sample","bank":"LANDBANK","amount":"2147483647","status":"UNFINISHED"}
I/System.out: {"checkdate":"Dec 9, 2016","checkno":"98678968","datepaid":"Dec 30, 2016","clientname":"John Sample","bank":"CHINABANK","amount":"92745","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 8, 2016","checkno":"87976","datepaid":"Nov 4, 2016","clientname":"GWEN SAMPLE","bank":"UNION BANK","amount":"6789","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 3, 2016","checkno":"897657","datepaid":"Nov 26, 2016","clientname":"Donald Sample","bank":"BANK","amount":"98797","status":"UNFINISHED"}
I/System.out: {"checkdate":"Nov 30, 2016","checkno":"12564","datepaid":"Dec 3, 2016","clientname":"Ronald Sample","bank":"GM bank","amount":"10120","status":"UNFINISHED"}
I/System.out: {"checkdate":"Dec 1, 2016","checkno":"123456","datepaid":"Dec 4, 2016","clientname":"Jolly Sample","bank":"china bank","amount":"12345","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 2, 2016","checkno":"132456","datepaid":"Dec 5, 2016","clientname":"Bet Sample","bank":"land bank","amount":"1451","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 4, 2016","checkno":"145623","datepaid":"Dec 6, 2016","clientname":"Aileen Sample","bank":"land bank","amount":"123445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 7, 2016","checkno":"145656","datepaid":"Dec 9, 2016","clientname":"Joyce Sample","bank":"land bank","amount":"323445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 7, 2016","checkno":"145656","datepaid":"Dec 9, 2016","clientname":"Joyce Sample","bank":"land bank","amount":"323445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 8, 2016","checkno":"165623","datepaid":"Dec 10, 2016","clientname":"Joy Sample","bank":"land bank","amount":"4523445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 9, 2016","checkno":"6445623","datepaid":"Dec 11, 2016","clientname":"jay Sample","bank":"china bank","amount":"4523445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 10, 2016","checkno":"64456245","datepaid":"Dec 113, 2016","clientname":"Rigor Sample","bank":"BDO","amount":"44523445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 10, 2016","checkno":"64456245","datepaid":"Dec 113, 2016","clientname":"Rigor Sample","bank":"BDO","amount":"44523445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 11, 2016","checkno":"456789","datepaid":"Dec 14, 2016","clientname":"Ynna Sample","bank":"BDO","amount":"45123445","status":"UNFINISHED"}
I/System.out: {"checkdate":"..Dec 12, 2016","checkno":"9814156","datepaid":"Dec 15, 2016","clientname":"Francheka Sample","bank":"China bank","amount":"41523445","status":"UNFINISHED"}


i used Fragment and here's the fragment

RecordFragment.java

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class RecordFragment extends Fragment implements Response.Listener<String> {
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> listdata = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_record, container, false);
ListView lvCheck = (ListView) v.findViewById(R.id.lvCheck);
arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, listdata);
lvCheck.setAdapter(arrayAdapter);
return v;
}

public void onAttach(Context context) {
super.onAttach(context);
String url = "http://samplechecker.site88.net/Request.php";
StringRequest stringRequest = new StringRequest(url, this, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error while reading data", Toast.LENGTH_SHORT).show();
}
});

MySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

}


@Override
public void onResponse(String response) {


try {


JSONObject jsonResponse = new JSONObject(response);
JSONArray arr = jsonResponse.getJSONArray("message");
boolean success = jsonResponse.getBoolean("success");


for (int i = 0; i < arr.length(); i++) {
JSONObject json_object = arr.getJSONObject(i);
String checkno = json_object.getString("checkno");
String clientname = json_object.getString("clientname");
String bank = json_object.getString("bank");
String datepaid = json_object.getString("datepaid");
}


if(success){
System.out.println(response);


if (arr != null) {
for (int i=0;i<arr.length();i++){
listdata.add(arr.get(i).toString());

System.out.println(listdata.get(i));
}




}



}
System.out.println("SUCCESS");




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

}


}


and here's my XML

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


<ListView
android:id="@+id/lvCheck"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Answer

In your onCreateView(), initialize your listdata variable before providing it to the adapter :

listdata = new ArrayList<String>();

Once you have added the response data to your ArrayList, notify the adapter about this change :

if(success) {
    System.out.println(response);
    if (arr != null) {
        for (int i=0;i<arr.length();i++){
            listdata.add(arr.get(i).toString());
            System.out.println(listdata.get(i));
        }
    }
    arrayAdapter.notifyDataSetChanged();
}
Comments