Wrong result of populating an Android ListView
<p>I'm trying to populate a ListView from the phone contacts, but the result is just wrong.</p>

<p>I created a list of contacts:</p>

<pre><code>List&lt;Contact&gt; contacts = getContactNames();
</code></pre>

<p>and my adapter is:</p>

<pre><code>ArrayAdapter&lt;Contact&gt; adapter = new ArrayAdapter&lt;&gt;(this, R.layout.contacts_list, R.id.contactName, contacts);
listView.setAdapter(adapter);
</code></pre>

<p>but when I run my application, I have the name of my package in the textView contactName and Description in textView contactNumber</p>

<p>I have three files, AcitivityMain.class, contacts_list.xml and Contact.class:</p>

<p><strong>ActivityMain</strong></p>

<pre><code>public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the list view
        this.listView = (ListView) findViewById(R.id.listView);

        // Read and show the contacts
        showContacts();
    }

    /**
     * Show the contacts in the ListView.
     */
    private void showContacts() {
        // Check the SDK version and whether the permission is already granted or not.
        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp; checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        } else {
            // Android version is lesser than 6.0 or the permission is already granted.
            List&lt;Contact&gt; contacts = getContactNames();
            ArrayAdapter&lt;Contact&gt; adapter = new ArrayAdapter&lt;&gt;(this, R.layout.contacts_list, R.id.contactName, contacts);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted
                showContacts();
            } else {
                Toast.makeText(this, "Until you grant the permission, we cannot display the names", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * Read the name of all the contacts.
     *
     * @return a list of names.
     */
    private List&lt;Contact&gt; getContactNames() {
        List&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();
        Contact contact = new Contact();
        // Get the ContentResolver
        ContentResolver cr = getContentResolver();
        // Get the Cursor of all the contacts
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if (cursor.getCount() &gt; 0) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                contact.setContactName(name);
                if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) {
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contact.setContactNumber(number);
                        contacts.add(contact);
                    }
                    pCur.close();
                }
            }
        }
        return contacts;
    }
}
</code></pre>

<p><strong>Contact class</strong></p>

<pre><code>public class Contact {
    private String contactName;
    private String contactNumber;

    public Contact() {}

    public Contact(String name, String number) {
        this.contactName = name;
        this.contactNumber = number;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Contact)) return false;

        Contact contact = (Contact) o;

        if (getContactName() != null ? !getContactName().equals(contact.getContactName()) : contact.getContactName() != null)
            return false;
        return getContactNumber() != null ? getContactNumber().equals(contact.getContactNumber()) : contact.getContactNumber() == null;

    }

    @Override
    public int hashCode() {
        int result = getContactName() != null ? getContactName().hashCode() : 0;
        result = 31 * result + (getContactNumber() != null ? getContactNumber().hashCode() : 0);
        return result;
    }
}
</code></pre>

<p><strong>contacts_list</strong></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="?android:attr/listPreferredItemHeight"
android:padding="6dip" &gt;

&lt;ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:src="@mipmap/ic_launcher" /&gt;

&lt;TextView
    android:id="@+id/contactNumber"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" /&gt;

&lt;TextView
    android:id="@+id/contactName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/contactNumber"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>and the result is:
<a href="https://i.stack.imgur.com/pnywk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pnywk.png" alt="enter image description here"></a></p>