Android - 使用Cursor填充ListView

时间:2016-09-09 12:02:22

标签: android listview android-fragments android-cursoradapter

我正在制作一款适合以下饮食的应用。我有一个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>

和我的数据库的游标适配器:

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));

}

这是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>

我正在尝试从数据库填充数据到列表视图:

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);
}
}

我没有任何错误,但listview是空的。我用Toast诅咒Cursor里面有数据。所以填充有问题,但我找不到答案。我从设备上下载了数据库,结构很好。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

这些是片段$images_array = glob($dir.'*.jpg'); foreach ($images_array as $item) { //your code goes here }

onCreateView

如果您从此处返回视图,稍后将在视图发布时在View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 中调用。

返回片段的UI的视图,或为null。

在您的情况下,您将返回一个新的视图对象

所以不要回来

onDestroyView()

返回已经膨胀的视图

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