Search code examples
androidandroid-listviewbaseadapter

I have to Display Contacts info (Name, Number, Image) of all contacts in a List View, using custom adapter which extends base adapter


I have created a ListView which should Display Contact-(Name,Number,Image).

I retrieved the name and number but, failed to display image.

I used custom adapter here is the adapter code

public class PranzAdapter extends BaseAdapter {

ArrayList<SingleRow> list;
Context c;

PranzAdapter(Context context, ArrayList<SingleRow> listItem){
    c = context;
    list= listItem;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row =  inflater.inflate(R.layout.single_row,null,false);

    TextView name   = (TextView) row.findViewById(R.id.textView1);
    TextView number = (TextView) row.findViewById(R.id.textView2);
    ImageView image = (ImageView) row.findViewById(R.id.imageView);

    SingleRow singleRow = list.get(position);

    name.setText(singleRow.name);
    number.setText(singleRow.number);

 //Error is this, Previously when any contact doesn't have image it pass `null`  

  if (singleRow.image!= null) {
        image.setImageURI(Uri.parse(singleRow.image));
    }
    return row;
 }
}

Update: I didn't put any default Image for the contact Image, so if any contact doesn't have Image it is throwing NullPointer so you can put a default Image or you have to handle the Exception using if condition.

I can display the name and number but while retrieving the image there is problem.

Here is the code for getting the contact info

public class MainActivity extends Activity {

ListView list;

ArrayList<SingleRow> listItem;

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

    list = (ListView) findViewById(R.id.listView);
    listItem = new ArrayList<SingleRow>();

    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    String id;
    String name;
    String number;
    String image;
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            id   = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while (pCur.moveToNext())
                {
                     number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                     image = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

                    SingleRow s = new SingleRow(name,number,image);
                    listItem.add(s);
                }
                pCur.close();

            }
        }
    }
    cursor.close();
    list.setAdapter(new PranzAdapter(this, listItem));
 }
}

Here is the model class I Used for Individual contact:

public class SingleRow {

String name;
String number;
String image;

//Constructor for Single row
SingleRow (String name,String number,String  image){
    this.name = name;
    this.number = number;
    this.image = image;
    }
}

Solution

  • I guess the problem is here:

    image.setImageURI(Uri.parse(temp.image));
    

    If your contact has no image, you are trying to parse a null.

    I am not 100% sure but as you havn't provided any stack trace I guess this is the problem.