Show more than one item in a Listview item

0

I need to show 3 fields of a table of a BD within a item in a Listview , what I have so far is that it shows the data but in different items . Any ideas on how to include everything in a single item?

 jsonArray = new JSONArray(result);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject preguntaDatos = null;
                    try {
                        preguntaDatos = jsonArray.getJSONObject(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String pregunta = null;
                    String respuesta= null;
                    String comentario=null;
                    try {
                        assert preguntaDatos != null;
                        pregunta = preguntaDatos.getString("pregunta");
                        respuesta = preguntaDatos.getString("respuesta");
                        comentario = preguntaDatos.getString("comentario");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    preguntas.add(pregunta);
                    preguntas.add(respuesta);
                    preguntas.add(comentario);
                }

expected result

    
asked by Ashley G. 12.01.2017 в 15:41
source

2 answers

3

OK I think I understand you want the data in a single element, you can define a String variable where you would store the 3 data, and then add them to the List questions:

String pregrespcomment;

 try {
                        assert preguntaDatos != null;
                   pregrespcomment = preguntaDatos.getString("pregunta") + "\n" + preguntaDatos.getString("respuesta") + "\n" + preguntaDatos.getString("comentario");
                        /*pregunta = preguntaDatos.getString("pregunta");
                        respuesta = preguntaDatos.getString("respuesta");
                        comentario = preguntaDatos.getString("comentario");*/
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    preguntas.add(pregrespcomment);
                    /*preguntas.add(pregunta);
                    preguntas.add(respuesta);
                    preguntas.add(comentario);*/

What I propose is to add the data to a single variable:

  pregrespcomment = preguntaDatos.getString("pregunta") + "\n" + preguntaDatos.getString("respuesta") + "\n" + preguntaDatos.getString("comentario");

and then add it to the list, to be able to show it in your ListView .

preguntas.add(pregrespcomment);
    
answered by 12.01.2017 / 16:15
source
1

One option is to use CustomListViews .

For example, you would create an xml that is called custom_item.xml which you put 3 TextViews to store the data of the 3 fields you need and it would be more or less like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:id="@+id/campo1"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"/>

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

 <TextView android:id="@+id/campo3" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"/>
</LinearLayout>

As for the getView(); method of your adapter , you only inflate your xml created and the rest is the same:

   convertView = mInflater.inflate(R.layout.custom_item, null);

To see the entire tutorial visit this link .

    
answered by 12.01.2017 в 16:10