我正在嘗試從我的資料庫讀取和顯示所有資料,但出現以下錯誤。此外,recyclerview 不顯示任何資料。
我如何從資料庫讀取。
public ArrayList<SatelliteForm> getAllData(SQLiteDatabase sqLiteDatabase){
ArrayList<SatelliteForm> arraySatelite = new ArrayList<>();
sqLiteDatabase = this.getWritableDatabase();
String SELECT_QUERY = "select name, country, category from " SatFormContracts.ContactsEntry.TABLE_NAME ";";
Cursor c = sqLiteDatabase.rawQuery(SELECT_QUERY, null);
if(c.moveToFirst()){
while(c.moveToNext()){
SatelliteForm form = new SatelliteForm();
form.setName(c.getString(1));
form.setCountry(c.getString(2));
form.setCategory(c.getString(3));
arraySatelite.add(form);
Log.i("nameSQL", c.getString(1) "" c.getString(2) "" c.getString(3));
}
}
c.close();
return arraySatelite;
}
我如何將元素添加到資料庫中。
public void onClick(View view) {
if(!(satName.getText().toString().isEmpty() || countryName.getText().toString().isEmpty() || categoryName.getText().toString().isEmpty())){
//adds the current names into the constructor
SatelliteForm addForm = new SatelliteForm(satName.getText().toString(),
categoryName.getText().toString(),
countryName.getText().toString());
//inserts the values
dbHelper.insertContact(db, addForm);
saveState.setText("Saved!");
}else{
saveState.setText("One or all the fields are empty");
Log.i("Campos", "Unos de los campos esta vacio");
}
}
});
我如何嘗試顯示保存在串列中的資料。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View listView = inflater.inflate(R.layout.fragment_list, container, false);
ArrayList<SatelliteForm> arraySatelite = dbHelper.getAllData(db);
RecyclerView recyclerView = listView.findViewById(R.id.recyclerView);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(arraySatelite);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager((getContext())));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
return listView;
}
我保存所有資料的類。
public class SatelliteForm {
private String name;
private String country;
private String category;
private ArrayList<String> names;
private List<String> list;
//Empty constructor
public SatelliteForm(){}
//Construtcor
public SatelliteForm(String name, String country, String category){
this.name = name;
this.country = country;
this.category = category;
this.names = names;
addNames(name, country, category);
}
//Method which adds the variables to an array
public void addNames(String name, String country, String category) {
list = Arrays.asList(name, country, category);
names = new ArrayList<>(list);
}
//Getters & Setters
public ArrayList<String> getArray(){
return names;
}
public String getName() { return name; }
public String getCountry() { return country; }
public String getCategory() { return category; }
public void setName(String name) { this.name = name; }
public void setCountry(String country) { this.country = country; }
public void setCategory(String category) { this.category = category; }
}
錯誤:[![error][1]][1][1]:https://i.stack.imgur.com/eyHoY.png
E/CursorWindow: Failed to read row 0, column 3 from a CursorWindow which has 4 rows, 3 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.uspherejda, PID: 7434
java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
uj5u.com熱心網友回復:
您應該從 0 開始并向上計數。我可以看到您從 1 開始,這就是錯誤的地方。
嘗試替換getString(1) 為getString(0).
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336552.html
