Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I would do to put contacts in a listView</p> <p>In your activity:</p> <pre><code>private ListView mContactsListView; private ListContactItemAdapter mContactsListAdapter; this.mContactsListAdapter = new ListContactItemAdapter(this, R.layout.contact_row); // after doing setContentView, assuming you have defined a listview in your layout file this.mContactsListView= (ListView) this.findViewById(R.id.contactsListView); // You may want to create a custom adapter, which I wrote below for showing you example this.mContactsListView.setAdapter(this.mContactsListAdapter); for (/* each contact */) { Contact contact = new Contact(); contact.name = contactName; contact.number = contactNumber; this.mContactsListAdapter.add(contact); } // In order to refresh your list and make data appear this.mContactsListAdapter.notifyDataSetChanged(); </code></pre> <p>Of course in my example you need a model object containing contact data:</p> <pre><code>public class Contact { public String name; public String number; } </code></pre> <p>And this could be your custom adapter using the Contact class above:</p> <pre><code>public class ListContactItemAdapter extends ArrayAdapter&lt;Contact&gt; { private int mLineLayout; private LayoutInflater mInflater; public ListContactItemAdapter(Context pContext, int pLineLayout) { super(pContext, pLineLayout); this.mLineLayout = pLineLayout; this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } static class ViewHolder { TextView contactName; TextView contactNumber; } @Override public View getView(int pPosition, View pView, ViewGroup pParent) { ViewHolder holder; if (pView == null) { pView = this.mInflater.inflate(this.mLineLayout, null); holder = new ViewHolder(); holder.contactName = (TextView) pView.findViewById(R.id.contactName); holder.contactNumber = (TextView) pView.findViewById(R.id.contactNumber); pView.setTag(holder); } else { holder = (ViewHolder) pView.getTag(); } Contact contact = getItem(pPosition); if (contact != null) { holder.contactName.setText(contact.name); holder.contactNumber.setText(contact.number); } return pView; } </code></pre> <p>}</p> <p>For that example your need a layout to define rows, of your listview. This could be a contact_row.xml example:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/contactName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="6dp" android:text="Contact name" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/contactNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/contactName" android:paddingLeft="6dp" android:text="number" android:textAppearance="?android:attr/textAppearanceSmall" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I didn't try but this should work. And anyway, I hope it will make you understand how listviews basically work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload