I'm developing an application which displays Phone contacts with CursorAdapter. When I run it, I faced with a list view which repeated just one contact as bellow ("david" is one of my contacts, just repeated in listview)
david 017224860
david 017224860
david 017224860
david 017224860
david 017224860
david 017224860 .
.
.
.
My activity looks like
public class Contacts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
startManagingCursor(cursor);
ContactCursorAdapterCT adapter= new ContactCursorAdapterCT(Contacts.this, cursor);
ListView contactLV = (ListView) findViewById(R.id.listviewblcontactsDB);
contactLV.setAdapter(adapter);
And my cursorAdapter looks like:
public class ContactCursorAdapterCT extends CursorAdapter {
public ContactCursorAdapterCT(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
while (cursor.moveToNext()) {
TextView name = (TextView)view.findViewById(R.id.blacklistDB1);
name.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
TextView phone = (TextView)view.findViewById(R.id.blacklistDB2);
phone.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.lv, parent, false);
bindView(v, context, cursor);
return v;
}
I noticed a few points:
cursor.moveToNext()
.getView()
calls newView()
and bindView()
on it's own; you shouldn't call these methods yourself.findViewById()
repeatedly.getColumnIndex()
repeatedly.