Search code examples
androidandroid-listviewandroid-fragmentsandroid-cursoradapter

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


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?


Solution

  • 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! :)