Search code examples
androidlistviewfragmentloadingandroid-cursoradapter

ListView in Fragment not calling newView


New to android/java programming. I have a ListView and a Button inside a Fragment that is part of a tab application. The problem i have is that even though i attach the CursorAdapter to the listView and even though the getCount() from the CursorAdapter returns something other than 0 the newView() method from the Adapter does not get called.

I can see the button and i can use it. If i change the background of the list i see the background colour.

I'm using the support library - API level 11.

public class MyListFragment extends Fragment {
private static final String TAG = "FragmentTabs";

private String mTag ;
private MainLogCursorAdapter mainAdapter;
private Cursor cursor;
private MainLogSource mainLogSource;
private LayoutInflater mInflater;
private ListView listView;
private Button newLogButton;
public MyListFragment() {
}

public MyListFragment(String tag) {
    mTag = tag;
    Log.d(TAG, "Constructor: tag=" + tag);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
mInflater = LayoutInflater.from(getActivity());

    mainLogSource = new MainLogSource(getActivity().getApplicationContext());

    cursor = mainLogSource.getCursor();

    mainAdapter = new MainLogCursorAdapter(getActivity   ().getApplicationContext    (), cursor);

    View view = (View) mInflater.inflate(R.layout.listview1, null);

    listView = (ListView) view.findViewById(R.id.listView1);
    listView.setAdapter(mainAdapter);

    System.out.println("Adapter set in onActivityCreated");

    if (mTag.equals("Log")) {
        loadMainLog();

    } else {
        if (mTag.equals("Rem")) {
            loadReminderLog();

        } else
            loadConfig();

    }
    }
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment

    if (mTag==null) 

        mTag = new String("Log");

    if (mTag.equals("Log")) {
return inflater.inflate(R.layout.listview1, container, false);

    }

    return null;
}

Adapter:

public class MainLogCursorAdapter extends CursorAdapter implements Filterable{
private LayoutInflater mLayoutInflater;
private Context mContext;
private MainLogSource dbh;
private String mainTypeDesc;

public MainLogCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
    mLayoutInflater = LayoutInflater.from(context);
    dbh = new MainLogSource(context);
    mContext = context;
    mCursor = c;

    public void bindView(View view, Context context, Cursor cursor) {
    String key = (String) cursor.getString(cursor
            .getColumnIndex(MainLogHelper.KEY));

some other code here ...

}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mLayoutInflater.inflate(R.layout.row, parent, false);
    System.out.println("newView");
    return v;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    System.out.println(super.getCount() + " count returned");
    return super.getCount();
}
}

And the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.9" 
    android:transcriptMode="alwaysScroll"
    >

</ListView>

<Button 
    android:id="@+id/NewLogButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:onClick="createLog"
    android:text="@string/NewLogCreation" />

</LinearLayout>

Solution

  • Okay, so you inflate and return the view in onCreateView, but you don't set the adapter to that ListView. Instead, you're inflating the layout again in onActivityCreated, but that's a new, different view, so setting the adapter to the listview in that hierarchy does nothing since the second view is never actually attached anywhere.

    You'll need to set the adapter in onCreateView. If your cursor cannot be ready at this point, just create the adapter with a null cursor and then call CursorAdapter.swapCursor() later (probably in onActivityCreated). Remember to always close the old cursor returned by swapCursor() (if it's not null).