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);
                SatelliteForm form = new SatelliteForm();
                Log.i("nameSQL", c.getString(1) + "" + c.getString(2) + "" + c.getString(3));
        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(),
                    //inserts the values
                    dbHelper.insertContact(db, addForm);
                    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(;
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(arraySatelite);
    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(){}
    public SatelliteForm(String name, String country, String category){ = name; = 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) { = name; }
    public void setCountry(String country) { = country; }
    public void setCategory(String category) { this.category = category; }


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.


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).

Answered By – RafalA

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published