Add content to a listview (Android)

0

Live community .. I was trying to develop an android application that receives JSON data, and wanted to re-take advantage of some of that specific JSON data to add to an Android listView.

I started by creating a layout called activity_list_services.xml. And I created a listview and a textview that I used just for testing ..

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.obscu.gestrepair5.ListServices">

    <ListView
        android:id="@+id/lstService"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="-190dp"
        tools:layout_editor_absoluteY="-21dp" />

    <TextView
        android:id="@+id/typeService2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="197dp"
        tools:layout_editor_absoluteY="270dp" />
</android.support.constraint.ConstraintLayout>

In the corresponding Activity I have the ListServices where I have an IP that can fetch the content without problems.

Even the arraylist service data is storing all the content I want successfully. But I'm not able to present this content in the Android ListView.

public class ListServices extends AppCompatActivity {

    RequestQueue rq;
    String name;
    TextView typeService;

    Ip ip = new Ip();
    String url= ip.stIp()+"/service";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_services);
        rq = Volley.newRequestQueue(this);
        //sendjsonrequest();
        populateListView();

        typeService = (TextView) findViewById(R.id.typeService2);
    }

    private void populateListView(){
        final ArrayList<String> servicedata = new ArrayList<String>();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JSONArray jsonArray = null;
                try {
                    jsonArray = response.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        name = jsonObject.getString("nameService");
                        typeService.setText(name);
                        servicedata.add(name);
                        Log.i(String.valueOf(servicedata), "onResponse: ");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                typeService.setText("Ups, ocorreu um erro");
            }
        });
        rq.add(jsonObjectRequest);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_services, servicedata);
        ListView list = (ListView) findViewById(R.id.lstService);
        list.setAdapter(adapter);


    }
}

Any tips? Cumps ..

    
asked by anonymous 18.08.2017 / 23:50

1 answer

1

Keep the instance of your adapter and within onResponse() call adapter.addAll(servicedata) and have notify the controls involved.

Something like:

public void onResponse(JSONObject response) {
                JSONArray jsonArray = null;
                try {
                    jsonArray = response.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        name = jsonObject.getString("nameService");
                        typeService.setText(name);
                        servicedata.add(name);
                        Log.i(String.valueOf(servicedata), "onResponse: ");
                    }

                   adapter.addAll(servicedata);
                   adapter.notifyDataSetChanged();

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

What happens is that when you pass your String collection, it is still empty.

    
21.08.2017 / 21:26