[英]SimpleCursorAdapter not working in ListActivity, no errors in log
[英]simpleCursorAdapter without ListActivity?
您能给我一些提示,如何在没有ListActivity的情况下使用simpleCursorAdapter吗? 我的意思是,我想开发一个不使用listActivity的使用simpleCursorAdapter的应用程序,坦率地说,如果我不使用ListActivity,应如何设置simpleCursorAdapter项目,例如
ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from, to ); 或者可能
或者可能
ListAdapter适配器=新的SimpleCursorAdapter(this,.R.layout.mypage,cursor,from, to );
我的问题是“ 到 ”项目,因为在此页面(XML文件)中没有任何TextView,我想在listView中显示树字段,该树字段在XML文件中定义为“ mylist”,
在您的活动中
private String [] project = {ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
...
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
project, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(phones);
ContactCursorAdapter adapter = new ContactCursorAdapter(this, phones);
ListView contactLV = (ListView) findViewById(R.id.contactLV);
contactLV.setAdapter(adapter);
...
您自己的游标适配器类
public class ContactCursorAdapter extends CursorAdapter {
public ContactCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTV = (TextView)view.findViewById(R.id.nameTV);
nameTV.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
TextView phoneTV = (TextView)view.findViewById(R.id.phoneTV);
phoneTV.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.contact_for_lv, parent, false);
bindView(v, context, cursor);
return v;
}
}
ListView的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/phoneTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="14sp" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.