0

我正在爲以下飲食做一個應用程序。我有一個列表視圖中的片段:Android - 使用光標填充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中發現光標內有數據。所以這是填充有問題,但我找不到答案。我從設備上下載數據庫,結構很好。謝謝你的幫助。

+4

'回報rootView的;' – Sanoop

回答

1

這是片段onCreateView

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

如果你從這裏返回查看,您稍後將onDestroyView()被釋放視圖時調用。

其中返回片段的用戶界面視圖,或null。

在你的情況你是返回一個新的視圖對象

因此而不是返回

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

回報已經膨脹查看

return rootView;