Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid autocompletetextview won't populate contacts with custom adapter
    text
    copied!<p>I've created a custom adapter and custom list item. I was originally using a simple list item and just populated either the name or the number. However, now I'd like to show both the name and phone number in each list item. When I type something now into the autocomplete text it won't populate any anything. I did some error testing and it seems that it isn't even calling the overridden getView() method. I can't seem to figure out why this isn't working.</p> <p>Also, yes I've added the READ_CONTACTS permission in the manifest. </p> <p><strong>MainActivity.java</strong></p> <pre><code>package com.sncrmck.autocompletetest; import java.util.ArrayList; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.widget.AutoCompleteTextView; import com.sncrmck.adapter.ContactAdapter; import com.sncrmck.obj.SimpleContact.SimpleContact; public class MainActivity extends Activity { AutoCompleteTextView acTextField; private ContactAdapter adapter; public ArrayList&lt;SimpleContact&gt; contactList = new ArrayList&lt;SimpleContact&gt;(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver resolver = getContentResolver(); acTextField = (AutoCompleteTextView) findViewById(R.id.autoComplete); Uri contacts = Uri.parse("content://icc/adn"); Cursor cursor = resolver.query(contacts, null, null, null, null); if (cursor.moveToFirst()) { String contactName; String phoneNumber; int nameColumn = cursor.getColumnIndex("name"); int phoneColumn = cursor.getColumnIndex("number"); do { contactName = cursor.getString(nameColumn); phoneNumber = cursor.getString(phoneColumn); if((contactName != " " || contactName != null) &amp;&amp; (phoneNumber!= " " ||phoneNumber!= null)) { SimpleContact sc = new SimpleContact(contactName, phoneNumber); contactList.add(sc); } } while (cursor.moveToNext()); } adapter = new ContactAdapter(this, R.layout.grid_item, contactList); acTextField.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p><strong>CustomAdapter.java</strong></p> <pre><code> package com.sncrmck.adapter; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast; import com.sncrmck.autocompletetest.R; import com.sncrmck.obj.SimpleContact.SimpleContact; public class ContactAdapter extends ArrayAdapter&lt;SimpleContact&gt; { private ArrayList&lt;SimpleContact&gt; entries; private Activity activity; public ContactAdapter(Activity a, int textViewResourceId, ArrayList&lt;SimpleContact&gt; entries){ super(a, textViewResourceId, entries); this.entries = entries; this.activity = a; Toast.makeText(getContext(), "Constructor", Toast.LENGTH_SHORT).show(); } public static class ViewHolder{ public TextView item1; public TextView item2; } @Override public View getView(int position, View convertView, ViewGroup parent) { Toast.makeText(getContext(), "Get View", Toast.LENGTH_SHORT).show(); View v = convertView; ViewHolder holder; if (v == null) { LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.grid_item, null); holder = new ViewHolder(); holder.item1 = (TextView) v.findViewById(R.id.big); holder.item2 = (TextView) v.findViewById(R.id.small);; } else holder=(ViewHolder)v.getTag(); final SimpleContact contactInfo = entries.get(position); if (contactInfo != null) { holder.item1.setText(contactInfo.getContactName()); holder.item2.setText(contactInfo.getContactNumber()); } return v; } } </code></pre> <p><strong>This is the SimpleContact object I'm passing to the ContactAdapter</strong></p> <pre><code>package com.sncrmck.obj.SimpleContact; public class SimpleContact { private String contactName; private String contactNumber; public SimpleContact(String string, String string2) { this.contactName = string; this.contactNumber = string2; } public String getContactName() { return contactName; } public void set(String contactName) { this.contactName= contactName; } public String getContactNumber() { return contactNumber; } public void setcustomSmall(String contactNumber) { this.contactNumber = contactNumber; } } </code></pre> <p><strong>Lastly, this is my custom list item - grid_item.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TextView android:textSize="24dp" android:id="@+id/big" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;TextView android:textSize="24dip" android:id="@+id/small" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre>
 

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