List View - returning more than 1 data in a row

2

I'm implementing a% of notes and I have to have a list view that should list ALL previously registered "NOTES" using the listing method implemented in DAO; Each line of the ListView should display the NOTE, Matter, Bimester, and Note Date.

This is how I add more than one in the listview row.

    
asked by anonymous 14.04.2015 / 17:10

1 answer

2

To show more than one information in an item in a List View you need to create a custom layout for the List View a List Adapter , which serves to associate the information of your objects with elements of your layout .

Eg: If we have a list of objects (we'll call List ) and we want to display 3 information about each object in that list, we must first create a layout_item with 3 fields (field1, field2 and field3), which could be as follows:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView_campo1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="6dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
    android:id="@+id/textView_campo2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
    android:id="@+id/textView_campo3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="6dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

Next, we need to create a List Adapter , as it is the one that will associate the information of each object of your ListItems with an element of your > layout_item . To do this, we will create the AdapterItem class:

public class AdapterItem extends BaseAdapter {

    // Lista de itens e contexto da aplicação
    List<SuaClasse> lista;
    Context context;

        public AdapterItemSuaClasse(List<SuaClasse> lista, Context context) {
            this.lista = lista;
            this.context = context;
        }

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

        @Override
        public Object getItem(int position) {

            return lista.get(position);
        }

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

        // Método responsável por criar o layout de um item da List View e associar as informações de cada item
        // a um elemento do layout
        @Override
        public View getView(int position, View view, ViewGroup parent) {

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

                   // cria uma view com o layout  do seu item
                   view = mInflater.inflate(R.layout.layout_item, null);

                   // Atribuição normal dos campos de uma view
                    TextView campo1 = (TextView) view.findViewById(R.id.textView_campo1);
                    TextView campo2 = (TextView) view.findViewById(R.id.textView_campo2);
                    TextView campo3 = (TextView) view.findViewById(R.id.textview_campo3);

                    campo1.setText(lista.get(position).getCampo1());
                    campo2.setText(lista.get(position).getCampo2());
                    campo3.setText(lista.get(position).getCampo3());

                return view;
        }

    }

After creating the layout and the adapter , you need to instantiate the adapter and pass it to your List and application context. So the adapter will automatically pick up each item from the ListItems and associate its attributes with the layout fields. Then we should tell your List View that you are using a adapter to create the elements of this List View :

    List View listView = (ListView) view.findViewById(R.id.listview);

    AdapterItem adapterItem = new AdapterItem(listaItens, context);
    listView.setAdapter(adapterItem);
    
14.04.2015 / 19:28