Android - Populating a listview using fragments and a Simple cursor adapter

谁说我不能喝 提交于 2019-12-22 00:48:39

问题


I know that this question is probably asked a hundred times so far, but i still haven't manage to solve my problem. I have one activity that's comprised of 2 fragments. One fragment is a form that adds information to the database:

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

And another fragment that's just a 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: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>

That fragment, the listview, i want to populate it with whatever there is in the database, and auto refresh when something new is added(haven't got to that stage yet). When i'm using the following code below i manage to populate the listview with the entire fragment(i.e i see the listboxes,buttons etc and i can even interact with them. Inception.) instead of just the data.

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

I believe that i'm doing something wrong with the from and to fields of the constructor. Any idea why i'm getting as an output the entire fragment view and not just the data in the database? Any other suggestions?


回答1:


Your problem is when you pass the fragment layout instead of the list view item layout. From what I can see your list view item is included inside your fragment_add_server.xml layout (since you put ... before and after the xml snippet). Move the snippet into its own file, something like my_list_view_item.xml and use that when creating your SimpleCursorAdapter.

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



回答2:


I solved the problem i had by changing the layout in the simplecursoradapter. Instead of

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

i did:

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

Essentially changing the layout to the generic android list layout did the trick! :)



来源:https://stackoverflow.com/questions/15077669/android-populating-a-listview-using-fragments-and-a-simple-cursor-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!