0

I'm working on an assignment that requires me to display both the name and number in a single row. I tried creating a custom class, Contact, in order to house the string, Name and Number. The code is pasted below:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //1. The class ContactsContract allows access to various types of data stored in the phone
        //Examples: Contacts, Phone settings, Contact groups etc.

        //Creates cursor to search contacts
        Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        //Creates array to store string (names from contact)
        ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
                R.layout.activity_main);



        //2. Using the ContactsContract class above, we got a cursor to the data. Now we iterate through it to get all the data

        while (cursor.moveToNext()) {

            //3. In our example, we get the DISPLAY_NAME but other data elements are also available

            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Contact newContact = new Contact();
            newContact.setName(name);
            newContact.setNumber(number);
            list.add(newContact);
        }

        setListAdapter(list);
    }
}

And this is my contact class:

public class Contact {
    private String name;
    private String number;

    void setName(String Name){
        name = Name;
    }

    void setNumber (String Number){
        number = Number;
    }
}
TheCoxer
  • 501
  • 1
  • 4
  • 15

2 Answers2

1

The key to achieve it is by managing a custome adapter for the Contact list.

CustomeAdapter.java

public class CustomeAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public CustomeAdapter(Activity activity, List objects) {
        super(activity, R.layout.row_list_item , objects);
        this.activity = activity;
        this.stocks = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ManageView mview = null;

        if(rowView == null)
        {

            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.row_list_item, null);


            mview= new ManageView();
            mview.namev = (TextView) rowView.findViewById(R.id.nameid);
            mview.numberv = (TextView) rowView.findViewById(R.id.numberid);

            // Cache the view objects 
            rowView.setTag(mview);
        } else {
            mview= (ManageView) rowView.getTag();
        }


        Contact  currentContact = stocks.get(position);
        mview.namev.setText(currentContact.getName());
        mview.numberv.setText(currentContact.getNumber());

        return rowView;
    }

    protected static class ManageView 
        protected TextView namev;
        protected TextView numberv;
    }
}

row_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/nameid" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/numberid"  />
</LinearLayout>

In your activity

  /* Creates array to store string (names from contact)
    ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
            R.layout.activity_main);*/
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>();
 while (cursor.moveToNext()) {

            //3. In our example, we get the DISPLAY_NAME but other data elements are also available

            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Contact newContact = new Contact();
            newContact.setName(name);
            newContact.setNumber(number);
            list.add(newContact);
        }
setListAdapter(new CustomeAdapter(this, list));
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
0

Listview has a setAdapter method. So just call listview.setAdapter(your_list_adapter_here);

Your list adapter is created from your list

You create an adapter class, as shown here Custom Adapter for List View

Community
  • 1
  • 1
Spider
  • 431
  • 7
  • 21