2012-11-27 23 views
0

你能否给我一些提示,告诉我如何在没有ListActivity的情况下使用simpleCursorAdapter? 我的意思是,我想开发一种使用simpleCursorAdapter没有listActivity, 坦率地说,如果我不使用ListActivity,我应该如何设置simpleCursorAdapter项目, 例如simpleCursorAdapter没有ListActivity?


ListAdapter适配器=新SimpleCursorAdapter(这样的应用程序, android.R.layout.two_line_list_item,cursor,from,to);或者也许


或也许

ListAdapter适配器=新SimpleCursorAdapter(此,.R.layout.mypage,光标,从,到);


我的问题是“”项目,因为我没有在这个页面(XML文件)的任何TextView的,我有我想要的ListView中,我在我的XML定义为显示领域的树文件作为 “MYLIST”,

回答

0

在你的活动

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; 
     } 


} 

XML的ListView控件

<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> 
+0

感谢乌拉圭回合的答案,但请你解释一下,我没有得到它,我应该在哪里定义的TextView?在哪个XML文件中,在我的XML文件中没有TextView,它只包含ListView和一些按钮。 – developer

+0

您必须在contact_for_lv.xml文件中定义TextView。有些视图可能是ListView的项目(行)。 – prozhyga

相关问题