Question

I created a Custom ListView using BaseAdapter now I want to populate this Listview with Contact Name and Number from Phone book..I am trying this..

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Rabtaye extends Activity {

    ListView msgList;
    ArrayList<MessageDetails> details;
    AdapterView.AdapterContextMenuInfo info;

    Cursor cursor;

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

        msgList = (ListView) findViewById(R.id.MessageList);
        details = new ArrayList<MessageDetails>();

        MessageDetails Detail = new MessageDetails();
        cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        startManagingCursor(cursor);

        String info[] = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone._ID };
         for(String a:info){
         Detail.setName(a);
         }
         Detail.setNumber("0313");
         details.add(Detail);
//      int to[] = { R.id.name, R.id.number };
//      ListAdapter cursada = new SimpleCursorAdapter(this,
//              android.R.layout.simple_expandable_list_item_2, cursor, info,
//              to);
        msgList.setAdapter(new CustomAdapter(details, this));


        msgList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView s = (TextView) arg1.findViewById(R.id.name);
                String abc = s.getText().toString();
                Toast.makeText(Rabtaye.this, abc, Toast.LENGTH_LONG).show();
            }
        });
    }
}

I used different ways, but I am not able to populate ListView with Contact Name or Number. In this code I have to statically add name and number...I am quite confuse here..little help would be greatly appreciated. Thanks in Advance. I am newbie so please go easy on me..:)

Was it helpful?

Solution

Hmmm. You're on the right track, but I think you're wandering a bit.

A ListView is a view object, not a BaseAdapter. As far as I can tell from your code, you don't need a custom adapter for your ListView.

What you should do is bind a regular CursorAdapter to your ListView, load the Contacts data you want from the Contacts Provider using a CursorLoader, then move the resulting Cursor to the CursorAdapter.

You can find the instructions for doing this in this Android training class: Loading Data in the Background.

What do you want in your custom ListView that you can't get from ListView itself?

OTHER TIPS

If you're putting more than just a single list of items into a ListView (it looks like you're doing name, number, etc.) you have to create your own custom adapter. Extend the BaseAdapter class either in your activity, or in a new class. Then you'll have to override the getView() method. This is what is called every time a new item gets inflated.

You pass your ArrayList into the BaseAdapter when you create it. The getView() method of the adapter is where you code your logic to take the data and present it as a list item. You'll have to define a new XML file as a layout for each list item that contains your TextViews and whatnot for your different children in each list item, and that's what you populate with your data.

There are a bunch of examples all over if you search for "baseadapter". Here's one that gives a pretty good intro.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top