【问题标题】:Reading and displaying values from SQLite and storing them inside a List从 SQLite 读取和显示值并将它们存储在 List 中
【发布时间】:2021-12-10 08:20:53
【问题描述】:

我正在尝试从我的数据库中读取并显示所有数据,但出现以下错误。 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; }
}

错误: [![错误][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.

【问题讨论】:

    标签: java android sql sqlite android-studio


    【解决方案1】:

    您应该从 0 开始并向上计数。我可以看到你从 1 开始,这就是错误的地方。

    尝试将getString(1) 替换为getString(0)

    【讨论】:

    • 我也是这么想的,但是如果我从 0 开始,我不会存储 id 吗?这就是我从 1 开始的原因。
    • @noobieCoder 查询是:select name, country, category from ...。返回的列中没有 id。
    • like forpas 解释说你没有在查询中使用 id 这就是为什么你需要从 0 开始。当光标为空时会报错
    • @RafalA Pepega 时刻在我身边。
    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 2013-05-09
    • 2021-06-09
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 2022-01-24
    相关资源
    最近更新 更多