Błażej Gutowski
Błażej Gutowski

Reputation: 3

Android - Populating ListView with Cursor

I'm making an app for following diet. I have a fragment with listview:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.b_gutowski.szama.ProductsFragment"
    android:id="@+id/productsFragment">


    <ListView
        android:id="@+id/productsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add"
        android:onClick="addProductBtnClick"
        app:backgroundTint="@color/accentColor"
        app:layout_anchor="@id/productsList"
        app:layout_anchorGravity="bottom|right|end" />


</android.support.design.widget.CoordinatorLayout>

and cursor adapter for my database:

public class ProductCursorAdapter extends CursorAdapter {
public ProductCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return LayoutInflater.from(context).inflate(R.layout.product_item, viewGroup, false);

}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // Find fields to populate in inflated template
    TextView productName = (TextView) view.findViewById(R.id.productName);
    TextView productProteins = (TextView) view.findViewById(R.id.productProteins);
    TextView productCarbs = (TextView) view.findViewById(R.id.productCarbs);
    TextView productFat = (TextView) view.findViewById(R.id.productFat);

    String name = cursor.getString(cursor.getColumnIndexOrThrow("productName"));
    float proteins = cursor.getInt(cursor.getColumnIndexOrThrow("productProteins"));
    float carbs = cursor.getInt(cursor.getColumnIndexOrThrow("productCarbs"));
    float fat = cursor.getInt(cursor.getColumnIndexOrThrow("productFat"));
    // Populate fields with extracted properties
    productName.setText(name);
    productProteins.setText(String.valueOf(proteins));
    productCarbs.setText(String.valueOf(carbs));
    productFat.setText(String.valueOf(fat));

}

This is layout of product_item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/itemName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nazwa"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/itemProteins"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Białko"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/itemCarbs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Węglowodany"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/itemFat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tłuszcz"
    android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

I'm trying populate data from database to list view:

public class ProductsFragment extends Fragment {


public ProductsFragment() {
    // Required empty public constructor
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    View rootView = inflater.inflate(R.layout.fragment_products, container, false);
    DBHandler handler = new DBHandler(getActivity());
    SQLiteDatabase db = handler.getReadableDatabase();
    Cursor productsCursor = db.rawQuery("SELECT  * FROM products", null);
    ListView productsList = (ListView) rootView.findViewById(R.id.productsList);


    ProductCursorAdapter cursorAdapter = new ProductCursorAdapter(getActivity(), productsCursor);

    productsList.setAdapter(cursorAdapter);

    return inflater.inflate(R.layout.fragment_products, container, false);
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_products_meals, menu);
    super.onCreateOptionsMenu(menu,inflater);
}
}

I haven't any errors, but listview is empty. I chcecked with Toast that there are data inside Cursor. So it's something wrong with populating but i can't find the answer. I downloaded database from device and the structure is good. Thanks for any help.

Upvotes: 0

Views: 43

Answers (1)

Sanoop Surendran
Sanoop Surendran

Reputation: 3486

These is the fragments onCreateView

View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

which Returns the View for the fragment's UI, or null.

In your case you are returning a new view object

So instead of returning

  return inflater.inflate(R.layout.fragment_products, container, false);

return the already inflated View

  return rootView;

Upvotes: 1

Related Questions