Add content to a listview (Android)

Asked

Viewed 287 times

0

Viva comunidade. I was trying to develop an android application that receives JSON data, and I wanted to re-leverage some of this 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 textview that I used only 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 Listservices where I have an IP that can fetch the content without problems.

Even the arraylist service data is storing all the content I wish successfully. But I’m not able to present this content in the Listview of Android..

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..

1 answer

1


Keep your Adapter instance and inside onResponse() call the adapter.addAll(servicedata) and 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();
                }
            }

Because what happens is that when you pass your String collection, it’s still empty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.