Skip to content
Advertisement

Reading and displaying values from SQLite and storing them inside a List

I am trying to read and display all the data from my db but I get the following error. Also the recyclerview is not displaying any data.

How I read from the DB.

 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;
    }

How I add the elements into the db.

 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");
                }
            }
        });

How I try to display the data saved in a list.

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;
}

Class where I save all my data.

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

Advertisement

Answer

You should start from 0 and count up. I can see that you start from 1 and that’s where it is wrong.

Try replacing getString(1) with getString(0).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement