Search code examples
androidandroid-listfragment

ListFragment content does not appear


Im trying to use a list fragment embedded in a parent fragment via layout XML (I've also done through fragment manager with no change). When I embed the listfragment by itself I am able to see the content from the cursor. When I override onCreateView of the parent fragment (ShoppingListActivity) and return a custom view I am no longer able to see my list fragment content.

On the list fragment I'm using a CursorLoader and onLoadFinished always called with a cursor that has content so it's not a matter of data not being present when I use the custom layout on the parent fragement combined. The listfragment doesn't have a custom layout (I originally started with the buttons from the parent fragment being added by overriding onCreateView of the list fragment but that produced the same result)

I also originally started out using the support library for the fragments but now use the native fragments.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 <Button
    android:id="@+id/btn_shopping_list_add_item_manually"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btn_shopping_list_add_item_barcode"
    android:onClick="addItemManually"
    android:text="TYPE"
    />

<Button
    android:id="@+id/btn_shopping_list_add_item_barcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:onClick="addItemFromBarcode"
    android:text="SCAN"
    />
<fragment     android:name="com.ainesophaur.shopping.assistant.fragments.ShoppingListFragment"
    android:id="@+id/list"
    android:layout_height="0dp"
    android:layout_below="@+id/btn_shopping_list_add_item_manually"
    android:layout_alignParentLeft="true"
    android:layout_width="fill_parent" />

---Edit----

Since I cannot upload images yet Ill draw what happens

When onCreateView is over ridden and a custom view is provided (The above XML layout) I get

Button1 Button2

Nothing

When I call the listview fragment directly I get

List Item1
List Item2

The list view fragment has a custom adapter extended from CursorAdapter and when I have the above specified XML layout returned from onCreateView then my newView and bindView of the adapter aren't called.

I've also tried to include a listview item in the parent fragment XML (instead of including the fragment itself) and gave it the standard android ID of list and I still get the same result..if I give the list view a unique ID and call setAdapter on the list then I also get no result (ie, bindView and newView of the adapter never get called) after onLoadFinished of the cursorloader ---EDIT

Now I can upload images :)

Missing List

List Shown


This is the code that contains the ListFragment and the CursorAdapter

ListFragment

public class ShoppingListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
    private int mStoreID;
    private long mListID;
    private String mStoreName;
    private ShoppingListItemsAdapter adapter;
    private Context mContext;
    private Cursor mCursor;
    private static final int SHOPPING_LIST_LOADER = 0x04;

     CursorLoader cursorLoader;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Bundle args = getArguments();
        mListID = 4; //args.getLong("listid");
        //mStoreName = args.getString("storename");

    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        adapter = new ShoppingListItemsAdapter(mContext, mCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(adapter);
        //setListAdapter(adapter);
        getLoaderManager().initLoader(SHOPPING_LIST_LOADER, null, this);
    }
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        cursorLoader = new CursorLoader(getActivity(),
                 Uri.withAppendedPath(ShoppingListProvider.CONTENT_LIST_ITEMS_URI, String.valueOf(mListID)), ShoppingDBAdapter.joinitemProj, null, null, null);
        return cursorLoader;
    }
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);
        //setListShown(true);
    }
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

    private OnClickListener addManual = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Fragment fragment = new AddItemFragment();
            Bundle args = new Bundle();
            args.putLong("listid", mListID);
            fragment.setArguments(args);
            getFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).addToBackStack(null)
            .commit();

            return;

        }
    };
    private static class ShoppingListItemViewHolder {
        public TextView separator;
        public TextView itemQuantity;
        public TextView itemName;
        public TextView itemPrice;
        public CharArrayBuffer quantityObtainedBuffer = new CharArrayBuffer(128);
        public CharArrayBuffer quantityNeededBuffer = new CharArrayBuffer(128);
        public CharArrayBuffer titleBuffer = new CharArrayBuffer(128);
        public CharArrayBuffer priceBuffer = new CharArrayBuffer(128);
        public CharArrayBuffer itemCategoryBuffer = new CharArrayBuffer(128);
  }
    public void addItemFromBarcode(View v)
    {
        return;
    }

extended CursorAdapter

private static class ShoppingListItemsAdapter extends CursorAdapter
{
    private static final int STATE_UNKNOWN = 0;
    private static final int STATE_SECTIONED_CELL = 1;
    private static final int STATE_REGULAR_CELL = 2;
    String LAST_CATEGORYNAME = "";
    private final CharArrayBuffer mBuffer = new CharArrayBuffer(128);
    private int[] mCellStates;

    public ShoppingListItemsAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        mCellStates = (c == null) ? null : new int[c.getCount()];
    }
      @Override
        public void changeCursor(Cursor cursor) {
            super.changeCursor(cursor);
            mCellStates = (cursor == null) ? null : new int[cursor.getCount()];
        }
      @Override
        public Cursor swapCursor(Cursor cursor) {
            Cursor oldCursor = super.swapCursor(cursor);
            mCellStates = (cursor == null) ? null : new int[cursor.getCount()];
            return oldCursor;
        }
      @Override
        public void bindView(View view, Context context, Cursor cursor) {

          final ShoppingListItemViewHolder holder = (ShoppingListItemViewHolder) view.getTag();
          final int COLUMN_NAME_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_NAME);
          final int COLUMN_ITEMCATEGORY_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_CATEGORYID);
          final int COLUMN_PRICE_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_PRICE);
          final int COLUMN_OBTAINED_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_OBTAINED);
          final int COLUMN_NEEDED_INDEX = cursor.getColumnIndex(ShoppingDBAdapter.KEY_NEEDED);
          boolean needSeparator = false;  
          final int position = cursor.getPosition();
          cursor.copyStringToBuffer(COLUMN_NAME_INDEX, holder.titleBuffer);
          cursor.copyStringToBuffer(COLUMN_PRICE_INDEX, holder.priceBuffer);
          cursor.copyStringToBuffer(COLUMN_NEEDED_INDEX, holder.quantityNeededBuffer);
          cursor.copyStringToBuffer(COLUMN_ITEMCATEGORY_INDEX, holder.itemCategoryBuffer);
          switch (mCellStates[position]) 
             {
                  case STATE_SECTIONED_CELL:
                      needSeparator = true;
                      break;

                  case STATE_REGULAR_CELL:
                      needSeparator = false;
                      break;

                  case STATE_UNKNOWN:
                  default:
                      if (position == 0) {
                          if(holder.itemCategoryBuffer.sizeCopied > 0)
                          {
                              needSeparator = true;
                              int holderStoreNameSize = holder.itemCategoryBuffer.sizeCopied;
                              String holderStoreName = "";
                              for(int i = 0; i < holderStoreNameSize; i++)
                              {
                                  holderStoreName += holder.itemCategoryBuffer.data[i];
                              }
                              LAST_CATEGORYNAME = holderStoreName;
                          }
                      } 
                      else 
                      {
                          cursor.moveToPosition(position - 1);
                          cursor.copyStringToBuffer(COLUMN_ITEMCATEGORY_INDEX, mBuffer);
                          if (mBuffer.sizeCopied > 0 && holder.itemCategoryBuffer.sizeCopied > 0) 
                          {
                              int mBufferSize = mBuffer.sizeCopied;
                              int holderStoreNameSize = holder.itemCategoryBuffer.sizeCopied;
                              String mBufferName = "";
                              String holderStoreName = "";
                              for(int i = 0; i < holderStoreNameSize; i++)
                              {
                                  holderStoreName += holder.itemCategoryBuffer.data[i];
                              }
                              if(!LAST_CATEGORYNAME.equals(holderStoreName))
                              {
                                  needSeparator = true;
                                  LAST_CATEGORYNAME = holderStoreName;
                              }
                          }
                          cursor.moveToPosition(position);
                      }
                      mCellStates[position] = needSeparator ? STATE_SECTIONED_CELL : STATE_REGULAR_CELL;
                      break;
              }
          if (needSeparator) {
                holder.separator.setText(holder.itemCategoryBuffer.data, 0 , holder.itemCategoryBuffer.sizeCopied);
                holder.separator.setVisibility(View.VISIBLE);
            } else {
                holder.separator.setVisibility(View.GONE);
            }
            holder.itemName.setText(holder.titleBuffer.data, 0, holder.titleBuffer.sizeCopied);
            holder.itemPrice.setText(holder.priceBuffer.data, 0, holder.priceBuffer.sizeCopied);
            holder.itemQuantity.setText(holder.quantityNeededBuffer.data, 0, holder.quantityNeededBuffer.sizeCopied);
      }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
         View v = LayoutInflater.from(context).inflate(R.layout.fragment_shopping_list_items_row, parent, false);
         ShoppingListItemViewHolder holder = new ShoppingListItemViewHolder();
         holder.separator = (TextView)v.findViewById(R.id.separator);
         holder.itemName = (TextView)v.findViewById(R.id.item_name);
         holder.itemPrice = (TextView)v.findViewById(R.id.item_price);
         holder.itemQuantity = (TextView)v.findViewById(R.id.item_quantity);
         v.setTag(holder);
         return v;
    }
}

Solution

  • Once I changed the layout XML used by the list view fragment from RelativeLayout to a LinearLayout I was able to see the items.