Search code examples
androidandroid-listfragmentandroid-support-library

android.support.v4.app.ListFragment just doesn't show


I extends the ListFragment in support lib, using a subclass of SimpleCursorAdapter as the adapter. All the data loading part is ok (read via debug), the list just doesn't show, I have a blank page on the Activity. Here is my code:

import android.support.v4.app.FragmentActivity;

public class HostActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_with_fragment);
}
}

layout file (activity_view_with_fragment.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <fragment
            android:id="@+id/lsStopTimetableFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:layout_weight="1"
            class="com.package.Fragment.MyFragment">
    </fragment>
</LinearLayout>

This is the ListFragment code:

import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
public class MyFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
    public  void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        String [] from = new String[] {"col_1", "col_2"}; 
        int [] to = new int [] {R.id.view1, R.id.view2}; 
        //..put value in args
        Loader l = getLoaderManager().restartLoader(0, args, this);
        if(l != null)
            l.forceLoad();

        adapter = new CustomCursorAdapter(this.getActivity()
              , R.layout.row_entry, null, from,
                to, 0);
        setListAdapter(adapter);
    }
    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.swapCursor(cursor);
    }
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        adapter.swapCursor(null);
    }

}

And in CustomCursorAdapter, I do implement the

getView (int pos, View convertView, ViewGroup parent)

method. Any tip? thanks in advance.


Solution

  • Have you tried to change the layout_width and layout_height parameters in fragment item from 0dp to match_parent? If you are trying to make an activity with only list fragment which fills the whole activity, you don't need to create layout like:

    <LinearLayout>
    <fragment/>
    </LinearLayout>
    

    you can directly create layout with fragment tag as root and only tag:

    <fragment/>