本文介绍了列表视图使用的CursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
林发展其与CursorAdapter中显示手机通讯录的应用程序。当我运行它,我面对着这只是重复一个联系人为波纹管列表视图(大卫是我接触的一个,只是重复列表视图)
大卫017224860
大卫017224860
大卫017224860
大卫017224860
大卫017224860
大卫017224860
。
我的行为看起来像
公共类联系方式延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.contacts);
光标光标=
getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
NULL,NULL,NULL,NULL);
startManagingCursor(光标);
ContactCursorAdapterCT适配器=新ContactCursorAdapterCT(Contacts.this,光标);
ListView控件contactLV =(ListView控件)findViewById(R.id.listviewblcontactsDB);
contactLV.setAdapter(适配器);
和我的CursorAdapter是这样的:
公共类ContactCursorAdapterCT扩展的CursorAdapter {
公共ContactCursorAdapterCT(上下文的背景下,光标C){
超(背景下,C);
// TODO自动生成构造函数存根
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
而(cursor.moveToNext()){
TextView的名称=(TextView中)view.findViewById(R.id.blacklistDB1);
name.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
TextView的手机=(TextView中)view.findViewById(R.id.blacklistDB2);
phone.setText(cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
// TODO自动生成方法存根
LayoutInflater充气= LayoutInflater.from(上下文);
视图V = inflater.inflate(R.layout.lv,父母,假);
bindView(ⅴ,上下文,光标);
返回伏;
}
解决方案
我注意到几点:
- 系统的CursorAdapter移动光标你,拿出你调用
cursor.moveToNext()
。 - 在适配器的
getView()
电话NewView的()
和bindView()
在它自己的;你不应该调用这些方法吧。 - 您应该看在Android开发者的讲座谷歌IO 了解有关加快适配器的技巧和窍门。温馨提示,如:
- 使用ViewHolder,而不是调用
findViewById()
反复。 - 重复保存光标的指标,而不是调用
getColumnIndex()。
- 取回的LayoutInflater一次,并保持局部引用。
- 使用ViewHolder,而不是调用
Im developing an application which display 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 the my contacts, just repeated in listview)
david017224860
david017224860
david017224860
david017224860
david017224860
david017224860 .
.
.
.
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:
- A CursorAdapter moves the Cursor for you, take out your call to
cursor.moveToNext()
. - The adapter's
getView()
callsnewView()
andbindView()
on it's own; you shouldn't call these methods yourself. - You should watch the Android developer's lectures at Google IO to learn tips and tricks about speeding up your adapter. Tips like:
- Using a ViewHolder, rather than calling
findViewById()
repeatedly. - Saving the indices of your Cursor, rather than calling
getColumnIndex()
repeatedly. - Fetching the LayoutInflater once and keeping a local reference.
- Using a ViewHolder, rather than calling
这篇关于列表视图使用的CursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!