[英]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.