我创建了SQL数据库并用值填充它。在TextView中,我需要显示特定行的乘法结果。 我做了SQL语句,我希望这是正确的。
public List<Food> multiplyFat(){
String totalFat = "SELECT " +FoodEntry.COLUMN_FAT_TOTAL + " FROM " + FoodEntry.TABLE_NAME + " WHERE ( "
+FoodEntry.COLUMN_FAT_TOTAL + " * " + FoodEntry.COLUMN_GRAM + " ) > 0";
SQLiteDatabase db = this.getWritableDatabase();
List<Food> storeTotalFat = new ArrayList<>();
Cursor cursor = db.rawQuery(totalFat, null);
if (cursor.moveToFirst()){
do {
double fat = Double.parseDouble(cursor.getString(0));
storeTotalFat.add(new Food(fat));
} while (cursor.moveToNext());
}
cursor.close();
return storeTotalFat;
}
为了更清楚,我需要将行COLUMN_FAT_TOTAL中的值与行COLUMN_GRAM相乘,并将结果显示到TextView中。或者我应该把这些SQL语句:
String totalFat = "SELECT " +FoodEntry.COLUMN_FAT_TOTAL + " * " +FoodEntry.COLUMN_GRAM + " FROM " +FoodEntry.TABLE_NAME;
这是更简单的方法,但我不确定它是否正确。
无论如何,我需要在TextView中显示这个multiplyFat()
函数(结果)。任何帮助或建议都会非常有帮助。
答案 0 :(得分:0)
您需要显示乘法总数,然后您不应该对COLUMN_FAT_TOTAL执行选择。相反,您应该在选择查询中进行计算并将该计算存储在别名中。然后,您可以使用该别名。
修改您的查询,如下所示
String totalFat = "SELECT " +FoodEntry.COLUMN_FAT_TOTAL + " * " + FoodEntry.COLUMN_GRAM + "AS Result FROM " + FoodEntry.TABLE_NAME";
答案 1 :(得分:0)
你走在正确的轨道上。如果数据库简单直接,请在数据库中进行数学计算,这是一个例子。
我可以为你的sql语句建议以下内容(类似于kapsym答案):
String totalFat = "SELECT " + FoodEntry.COLUMN_FAT_TOTAL + ", " + FoodEntry.COLUMN_GRAM + "," FoodEntry.COLUMN_FAT_TOTAL + " * " + FoodEntry.COLUMN_GRAM + "AS TotalFat FROM " + FoodEntry.TABLE_NAME + " WHERE ( " + FoodEntry.COLUMN_FAT_TOTAL + " * " + FoodEntry.COLUMN_GRAM + " ) > 0";
答案 2 :(得分:0)
public List multiplyFat(){
String totalFat = "SELECT " +FoodEntry.COLUMN_FAT_TOTAL + " FROM " + FoodEntry.TABLE_NAME + " WHERE ( "
+FoodEntry.COLUMN_FAT_TOTAL + " * " + FoodEntry.COLUMN_GRAM + " ) > 0";
以上在MySQL中完美运行,我只是尝试过。
SQLiteDatabase db = this.getWritableDatabase(); --> db = this.getReadableDatabase();
由于您没有更改SQLite数据库中的任何内容,因此您需要一个&#39; .getReadableDatbase();&#39;不是&#39; .getWritableDatabase();&#39;
List<Food> storeTotalFat = new ArrayList<>();
Cursor cursor = db.rawQuery(totalFat, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
do {
double fat = Double.parseDouble(cursor.getString(0));
storeTotalFat.add(new Food(fat));
} cursor.moveToNext();
}
cursor.close();
return storeTotalFat;
}
看看我是如何第一次转移到第一个索引,然后我要求它继续一个while循环,直到它到达SQL结果中的最后一个元素。你的方式实际上并没有增加光标,它只能将第一个结果添加到storeTotalFat ArrayList。
--------第2部分---------
现在让我们假装我们回到了活动中,哇! 假设有一个对数据库助手的类变量引用,并添加一个新的ArrayList来保存我们刚刚获得的查询结果的结果。
DBHelper myDBhelper = DBHelper.getInstance(MainActivity.this)
ArrayList<String> sqlResultArray = new ArrayList<>();
如果你想要一个Recyclerview你做一个单独的RviewAdapter类,在你的情况下FoodFatRecyclerViewAdapter并添加以下
RecyclerView rv = (RecyclerView) findViewById(R.id.foodFatRecView);
LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(llm);
FoodFatRecyclerViewAdapter foodFatAdapter = new FoodFatRecyclerViewAdapter(MainActivity.this, sqlResultArray);
rv.setAdapter(foodFatAdapter);
现在至于将结果添加到textview中,创建一个名为FoodFatRecyclerViewAdapter的新类
public class FoodFatRecyclerViewAdapter extends RecyclerView.Adapter<FoodFatViewHolder> {
ArrayList<String> mFoodFatItems;
Context mContext;
public FoodFatRecyclerViewAdapter(Context context, ArrayList<String> array) {
mContext = context;
mFoodFatItems = array;
}
@Override
public FoodFatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.this_xml_layout_will_describe_the_arrangement_of_the_indi_item_in_the_list_not_the_master_view, parent, false);
FoodFatViewHolder recyclerView = new FoodFatViewHolder(view);
return recyclerView;
}
@Override
public void onBindViewHolder(FoodFatViewHolder holder, final int position) {
holder.mFatTotalTextView.setText(mFoodFatItems.get(position));
}
@Override
public int getItemCount() {
return mFoodFatItems.size();
}
}
现在是拼图的最后一块,View Holder创建了一个名为FoodFatViewHolder的类
public class FoodFatViewHolder extends RecyclerView.ViewHolder{
TextView mFatTotalTextView;
public FoodFatViewHolder(View itemView) {
super(itemView);
mFatTotalTextView = (TextView) itemView.findViewById(R.id.the_textview_id_within_the_individ_item_in_rv_xml_layout);
}
}
瞧!它应该工作