[英]How to fetch data to fragment?
在主要活動中,菜單導致片段BMICaclulator
和History
。 BMICalculator
片段有 3 個 EditText、一個單選按鈕和一個提交按鈕。
fragment_bmicalculator.xml:
<EditText
android:id="@+id/height"/>
<EditText
android:id="@+id/weight" />
<EditText
android:id="@+id/age"/>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gender">
<RadioButton
android:id="@+id/gender_male"/>
<RadioButton
android:id="@+id/gender_female"/>
</RadioGroup>
<Button
android:id="@+id/calculate"/>
插入數據庫工作。 傳遞的數據是從 editText 和 radioButton* 中獲取的。
bmi計算器.java:
MyDatabaseHelper myDB = new MyDatabaseHelper(getActivity());
myDB.addData(height, weight, age, gender, bmiInterpretation);
我的數據庫助手.java:
private Context context;
private static final String DATABASE_NAME = "bmi.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "bmiData";
private static final String COLUMN_ID = "id";
private static final String COLUMN_WEIGHT = "weight";
private static final String COLUMN_HEIGHT = "height";
private static final String COLUMN_AGE = "age";
private static final String COLUMN_GENDER = "gender";
private static final String COLUMN_BMI = "bmi";
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_WEIGHT + " TEXT, " +
COLUMN_HEIGHT + " TEXT, " +
COLUMN_AGE + " TEXT, " +
COLUMN_GENDER + " TEXT, " +
COLUMN_BMI + " TEXT);";
db.execSQL(query);
}
void addData(String weight, String height, String age, String gender, String bmi){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_WEIGHT, weight);
cv.put(COLUMN_HEIGHT, height);
cv.put(COLUMN_AGE, age);
cv.put(COLUMN_GENDER, gender);
cv.put(COLUMN_BMI, bmi);
long result = db.insert(TABLE_NAME,null, cv);
if(result == -1){
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Added Successfully!", Toast.LENGTH_SHORT).show();
}
如何獲取數據並打印為 fragment_history 中的列表? RecyclerView 和自定義適配器使我的應用程序崩潰。
首先,我認為最好定義一個 class 比方說數據,這樣:
public class Data{
private String weight;
private String height;
private String age;
private String gender;
private String bmi;
// Constructor, getter and setters ...
}
現在讓我們定義返回 SQLite 數據庫中所有數據的 function:
public List<Data> fetchData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT (weight, height, age, gender, bmi) FROM " + TABLE_NAME, null);
List<Data> data = new ArrayList<Data>();
if(cursor.moveToFirst()){
do {
data.add(new Data(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5)))
}while(cursor.moveToNext())
}
cursor.close()
return data;
}
現在,要在 ListView 中插入數據,在fragment_history的布局中定義,讓我們假設 xml 文件中的 ListView 具有 id list_view ,並且您為每個項目定義了一個自定義布局,名為 list_item(用於顯示實例數據的)。 最后一步是創建一個適配器,並將來自數據庫和適配器的數據鏈接起來,例如以這種方式:
// onViewCreated of fragment_history
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
ListView lw = view.findViewById(R.id.list_view);
MyDatabaseHelper myDB = new MyDatabaseHelper(getActivity());
List<Data> data = myDB.fetchData();
ArrayAdapter<Data> adapter = new ArrayAdapter<>(getActivity(), R.layout.list_item, data);
lw.setAdapter(adapter);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.