【问题标题】:Android - Populating a listview using fragments and a Simple cursor adapterAndroid - 使用片段和简单的光标适配器填充列表视图
【发布时间】:2013-02-11 05:28:40
【问题描述】:

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

**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>
...

还有一个片段,它只是一个列表视图。

<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:background="@drawable/georgeback" 
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="60" >
</ListView>

<Button
    android:id="@+id/connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/connectBTN" />

</LinearLayout>

那个片段,列表视图,我想用数据库中的任何内容填充它,并在添加新内容时自动刷新(还没有到那个阶段)。当我使用下面的代码时,我设法用整个片段填充列表视图(即我看到列表框、按钮等,我什至可以与它们进行交互。Inception。)而不仅仅是数据。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_select_server,
            container, false);
    Cursor mNotesCursor = mDbHelper.fetchAllNotes();
    String[] from = new String[]{mDbHelper.KEY_TITLE};
    int[] to = new int[]{R.id.editName};
    mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.fragment_add_server,mNotesCursor,from,to,0);

    mMyListView=(ListView)view.findViewById(R.id.listView1);
    mMyListView.setAdapter(mCurAdapter);

    return view;
}

我相信我在构造函数的 from 和 to 字段上做错了。知道为什么我将整个片段视图作为输出而不只是数据库中的数据吗?还有其他建议吗?

【问题讨论】:

    标签: android android-listview android-fragments android-cursoradapter


    【解决方案1】:

    您的问题是当您传递片段布局而不是列表视图项布局时。从我可以看到您的列表视图项包含在您的 fragment_add_server.xml 布局中(因为您在 xml sn-p 之前和之后放置了 ...)。将 sn-p 移动到自己的文件中,例如 my_list_view_item.xml 并在创建 SimpleCursorAdapter 时使用它。

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

    【讨论】:

    • 很抱歉我没有添加第二个布局。 listview 是第二种布局,并且是一个不同的片段。我现在已将代码添加到我的原始帖子中。当我将第二个布局(R.layout.fragment_select_server)添加到构造函数时,列表视图中没有任何内容。
    【解决方案2】:

    我通过更改简单光标适配器中的布局解决了我遇到的问题。而不是

    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 列表布局就可以了! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多