Android-使用片段和简单的光标适配器填充listview

Android-使用片段和简单的光标适配器填充listview,android,android-listview,android-fragments,android-cursoradapter,Android,Android Listview,Android Fragments,Android Cursoradapter,我知道这个问题到目前为止可能被问了一百次,但我仍然没有设法解决我的问题。我有一个活动由两个片段组成。一个片段是向数据库添加信息的表单: **R.layout.fragment_add_server:** <?xml version="1.0" encoding="utf-8"?> ... <LinearLayout android:id="@+id/nameLinear" android:layout_width="fill_parent" androi

我知道这个问题到目前为止可能被问了一百次,但我仍然没有设法解决我的问题。我有一个活动由两个片段组成。一个片段是向数据库添加信息的表单:

**R.layout.fragment_add_server:**
<?xml version="1.0" encoding="utf-8"?>
...
<LinearLayout
    android:id="@+id/nameLinear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp" >

    <TextView
        android:id="@+id/nameText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:text="@string/strName"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:hint="@string/editName" />
</LinearLayout>
...

我相信我对构造函数的from和to字段做了一些错误的处理。你知道为什么我得到的是整个片段视图,而不仅仅是数据库中的数据吗?还有其他建议吗?

您的问题是当您传递片段布局而不是列表视图项布局时。据我所见,列表视图项包含在
片段\u添加\u server.xml
布局中(因为您将
..
放在xml片段的前后)。将代码段移动到它自己的文件中,比如
my_list\u view\u item.xml
,并在创建
SimpleCorsorAdapter
时使用它

mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.my_list_view_item,mNotesCursor,from,to,0);

我通过更改SimpleCorsorAdapter中的布局解决了我遇到的问题。而不是

mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.fragment_add_server,mNotesCursor,from,to,0);
我做到了:

mCurAdapter = new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_list_item_1,mNotesCursor,from,to,0);

实际上,将布局更改为通用的android列表布局就成功了!:)

很抱歉,我没有添加第二个布局。listview是第二个布局,是一个不同的片段。我现在已经将代码添加到我原来的帖子中。当我将第二个布局(R.layout.fragment\u select\u server)添加到构造函数中时,listview中没有显示任何内容。
mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.fragment_add_server,mNotesCursor,from,to,0);
mCurAdapter = new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_list_item_1,mNotesCursor,from,to,0);