nanjero echizen
nanjero echizen

Reputation: 231

Android - How can I put database rows into a ListView?

I am trying to view the content of my database by listing it in a ListView. What am i doing wrong? The goal is to load a list of the database data when the page loads after a button click on the homepage.

The XML page simply has a ListView, named "studentList", inside a ScrollView

Java code:

public class edit_student extends AppCompatActivity {
    private dbasemanager dBase;
    private ListView studentInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_student);

        dBase.openReadable();
        studentInfoList = (ListView)findViewById(R.id.studentList);
        ArrayList<String> dBaseContent = dBase.retrieveRows();
        ArrayAdapter<String> arrayAdpt = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, dBaseContent);
        studentInfoList.setAdapter(arrayAdpt);
        dBase.close();
    }
}

This is openReadable() Function:

public dbasemanager openReadable() throws android.database.SQLException {
    helper = new SQLHelper(context);
    db = helper.getReadableDatabase();
    return this;
}

This is the retrieveRows() Function:

 public ArrayList<String> retrieveRows() {
    ArrayList<String> studentRows = new ArrayList<>();
    String[] columns = new String[] {"sid", "first_name", "last_name"};
    Cursor cursor = db.query(Table_Name, columns, null, null, null, null, null);
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {
        studentRows.add(cursor.getString(0) + ", " + cursor.getString(1) 
                        + ", " + cursor.getString(2));
        cursor.moveToNext();
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return studentRows;
}

Logcat:

logcat

Upvotes: 0

Views: 81

Answers (1)

adalpari
adalpari

Reputation: 3121

There is a null pointer in line 17.

You need to construct databasemanager object before call it

public class edit_student extends AppCompatActivity {
    private dbasemanager dBase;
    private ListView studentInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_student);

        // construct databasemanager
        dBase = new dbasemanager(...);

        dBase.openReadable();
        studentInfoList = (ListView)findViewById(R.id.studentList);
        ArrayList<String> dBaseContent = dBase.retrieveRows();
        ArrayAdapter<String> arrayAdpt = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, dBaseContent);
        studentInfoList.setAdapter(arrayAdpt);
        dBase.close();
    }
}

BTW, I recommend you to use java code conventions to name classes. For example: edit_student should be editStudent and dbasemanager should be DBManager or DbManager.

Upvotes: 1

Related Questions