List View - returning more than 1 data in a row

Asked

Viewed 448 times

2

I’m implementing a app and I have to have a list view which shall list ALL previously registered "NOTES" using the listing method implemented in the DAO; Each line of Listview must present the registration date of NOTE, Material, Two-month period and the Note.

or as I do to add more than one die on the line of listview.

1 answer

2

To show more than one information in an item of a List View it is necessary to create a layout customized for items from List View and a List Adapter, which serves to associate the inforamations of its objects to the elements of its layout.

Ex.: If we have a list of objects (we call list) and we want to display 3 information of each object of this list, we must create a first one layout_item with 3 fields (campo1, campo2 and campo3), 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>

Then we need to create a List Adapter, because it is he who will associate the information of each information of an object of his list to an element of its layout_item. For this, we will create the class Adapteritem:

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 as parameter to your list and the context of the application. Thus, Adapter will automatically take each item from list and associate its attributes to the layout fields. Then we should say for your List View that you are using a Adapter to create the elements of List View:

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

    AdapterItem adapterItem = new AdapterItem(listaItens, context);
    listView.setAdapter(adapterItem);

Browser other questions tagged

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