ListView适配器不显示所有请求的数据

时间:2016-08-30 07:00:03

标签: android sqlite listview

我使用本地sqlite数据库中的数据填充listview。从我当前的查询我想获得foodname和蛋白质,已经在Textview,适配器和查询中设置它来获取这些数据,但它只返回名称而不是蛋白质。

这是适配器类:

public class Db_listView_Adapter extends CursorAdapter {
public Db_listView_Adapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

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

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView foodName = (TextView) view.findViewById(R.id.foodNameInListView);
    TextView foodProtein = (TextView) view.findViewById(R.id.foodProteinInListView);
    TextView foodCarbs = (TextView) view.findViewById(R.id.foodCarbsInListView);
    TextView foodFat = (TextView) view.findViewById(R.id.foodFatsInListView);
    TextView foodCalories = (TextView) view.findViewById(R.id.foodCaloriesInListView);
    TextView foodFibers = (TextView) view.findViewById(R.id.foodFibersInListView);
    TextView foodVitA = (TextView) view.findViewById(R.id.foodVitAInListView);
    TextView foodVitB = (TextView) view.findViewById(R.id.foodVitBInListView);
    TextView foodCalcium = (TextView) view.findViewById(R.id.foodCalciumInListView);
    TextView foodMagnesium = (TextView) view.findViewById(R.id.foodMagnesiumInListView);

    String foodNameString = cursor.getString(cursor.getColumnIndexOrThrow("_FoodName"));
    String foodProteinString = cursor.getString(cursor.getColumnIndexOrThrow("_FoodProtein"));

    foodName.setText(foodNameString);
    foodProtein.setText(foodProteinString);
}

}

这是listview布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/foodNameInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodProteinInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodCarbsInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodFatsInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodCaloriesInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodFibersInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodVitAInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodVitBInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodCalciumInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/foodMagnesiumInListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>

这里我尝试从数据库中获取数据并填写listview:

public class Db_fragment extends Fragment {
View view;
ListView listView;
Db_listView_Adapter db_listView_adapter;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.db_fragment, container, false);
    listView = (ListView) view.findViewById(R.id.dbView);

    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final MyDatabaseOpenHelper mDbHelper = new MyDatabaseOpenHelper(view.getContext());


    final EditText foodName = (EditText) view.findViewById(R.id.nameForDb);
    Button enterData = (Button) view.findViewById(R.id.enterData);
    Button showData = (Button) view.findViewById(R.id.showData);
    final TextView errorTextNameMissing = (TextView) view.findViewById(R.id.errorTextNameMissing);

    showData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (foodName.getText().toString().trim().length() != 0) {

                errorTextNameMissing.setText("");
                SQLiteDatabase myDb = mDbHelper.getReadableDatabase();
                String[] projection = {TableFoodMacros.FoodMacros._ID, TableFoodMacros.FoodMacros.FOOD_NAME, TableFoodMacros.FoodMacros.FOOD_PROTEIN};
                String rawQuery = "SELECT _ID, _Foodname , _FoodProtein FROM FoodMacros where _FoodName = " + "'" + foodName.getText().toString() + "'";

                Cursor c = myDb.rawQuery(rawQuery, null);
                if(c.getCount() == 0){
                    Intent goToSaveFood = new Intent(getActivity(),Save_Food.class);
                    goToSaveFood.putExtra("FoodName", foodName.getText().toString());
                    myDb.close();
                    startActivity(goToSaveFood);
                }else {

                    db_listView_adapter = new Db_listView_Adapter(view.getContext(), c);
                    listView.setAdapter(db_listView_adapter);
                    myDb.close();
                }
            } else {
                errorTextNameMissing.setText(getResources().getString(R.string.errorTextNameMissing));
            }

        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

所以我发现问题出现在DB中添加数据的活动中,我在onCreate中而不是onClick ...; x