Android - 使用游标填充 ListView
Android - Populating ListView with Cursor
我正在制作一个用于跟踪饮食的应用程序。我有一个带有列表视图的片段:
<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);
}
}
我没有任何错误,但列表视图是空的。我用 Toast 检查了 Cursor 里面有数据。所以填充有问题,但我找不到答案。我从设备上下载了数据库,结构很好。感谢您的帮助。
这些是片段onCreateView
View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
如果您 return 从此处查看视图,稍后将在 onDestroyView()
释放视图时调用您。
哪个 Returns 片段的 UI 视图,或 null。
在您的情况下,您正在 return 创建一个新的视图对象
所以不用 returning
return inflater.inflate(R.layout.fragment_products, container, false);
return 已经膨胀的视图
return rootView;
我正在制作一个用于跟踪饮食的应用程序。我有一个带有列表视图的片段:
<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);
}
}
我没有任何错误,但列表视图是空的。我用 Toast 检查了 Cursor 里面有数据。所以填充有问题,但我找不到答案。我从设备上下载了数据库,结构很好。感谢您的帮助。
这些是片段onCreateView
View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
如果您 return 从此处查看视图,稍后将在 onDestroyView()
释放视图时调用您。
哪个 Returns 片段的 UI 视图,或 null。
在您的情况下,您正在 return 创建一个新的视图对象
所以不用 returning
return inflater.inflate(R.layout.fragment_products, container, false);
return 已经膨胀的视图
return rootView;