Android Contacts On Listview
Solution 1:
Try making following changes to your current adapter class:
publicclassContactsAdapterextendsBaseAdapter {
ArrayList<ContactDetails> contactDetails = newArrayList<ContactDetails>();
Context c;
LayoutInflater mInflater;
publicContactsAdapter(Context context,ArrayList<ContactDetails> ContactDetails) {
contactDetails = ContactDetails;
c=context;
mInflater = LayoutInflater.from(context);
Log.v("constructor", contactDetails.size()+"");
}
@OverridepublicintgetCount() {
Log.v("getCount", "");
return contactDetails.size();
}
@Overridepublic Object getItem(int position) {
Log.v("getItem", position+" ");
return contactDetails.get(position);
}
@OverridepubliclonggetItemId(int position) {
Log.v("getItemId", position+"");
return position;
}
@Overridepublic View getView(int position, View convertView,ViewGroup parent) {
final ViewHolder holder;
Log.v("in :","get View");
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.contacts_listitem, null);
holder = newViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
Log.v("TAG", "convert view not null");
}
else
holder=(ViewHolder)convertView.getTag();
textview.setText(contactDetails.get(position).Name);
return convertView;
}
staticclassViewHolder
{
TextView text;
}
}
and also include layout file named contacts_listitem.xml as shown below in my answer.
Now you need to change these lines in your main activity too:
...
contactsAdapter = new ContactsAdapter(Contacts.this,ContactList);
EmergencyContactList.setAdapter(contactsAdapter);
...
Here,also you have not set adapter to your listview so it is not showing you anything there.you missed the line EmergencyContactList.setAdapter(contactsAdapter);. please do as above and let me know the result.
EDIT :
You can also try using custom ArrayAdapter class like:
ContactsAdapter.class:
publicclassContactsAdapterextendsArrayAdapter<String> {
String[] array;
LayoutInflater mInflater;
public ContactsAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
array=objects;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.contacts_listitem, null);
holder = new ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.text.setText(array[position]);
return convertView;
}
staticclassViewHolder{
TextView text;
}
}
Create a layout file contacts_listitem.xml:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:id="@+id/text"android:textSize="18dip"android:padding="5dip"android:textColor="#90000000"android:layout_weight="0.9"
/></LinearLayout>
Now you need to use ContactsAdapter's object in your main activity like:
...
ContactsAdapter aa=newContactsAdapter(context,R.layout.contacts_listitem, newString[]{"contact1","contact2","contact3","contacts4"});
listView.setAdapter(aa);
...
Here,you can build your String array in onActivityResult() accordingly.(I didn't get what you have done in your adapter class to show contact details so i just put strings from String array in adapter.You have to code as per your need.)
EDIT :
Change these lines of code in your app:
public String[] getContactsArray(ArrayList<ContactDetails> List)
{
ContactsArray = new String[List.size()];
for(int i=0;i<=List.size()-1;i++)
{
ContactsArray[i] = List.get(i).Name +"\n"+ List.get(i).Number;
}
return ContactsArray;
}
Here,you don't need to check if the list is empty or not,because you already do so before calling this function.
Post a Comment for "Android Contacts On Listview"