jeudi 4 juin 2015

Android - SQLite populating spinner and list view

I have created an android application which allows the user to store notes on a relevant subject or module at school/university using SQLite. I am trying to populate a spinner within my notes activity from the database. So for example if the user has added a notes where the subject is 'Maths', this should populate within the spinner.

When the user selects the relevant subject/module from the spinner, I want the listview to populate with the relevant subject notes, i.e. if there is more than one notes row on a certain subject it is also displayed within the listview. I have tried to use similar questions on here to try and find a solution to my problem but have had no luck so far.

I have inserted the table, and the notes activity. The notes activity currently just populates the listview with all the data in the table.

Any help would be greatly appreciated.

    //My NOTES Table
    private static final String ADD_NOTES_QUERY = "CREATE TABLE "+
            userAssignment.NewNotesInfo.TABLE_NAME+"("+
            userAssignment.NewNotesInfo.SUBJECT+" TEXT,"+
            userAssignment.NewNotesInfo.NOTES+" TEXT);";





    //notes activity
public class notes extends ActionBarActivity {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DbHelper dbHelper;
    Cursor cursor;
    NotesListDataAdapter notesListDataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes);
        listView = (ListView) findViewById(R.id.listView);
        notesListDataAdapter = new NotesListDataAdapter(getApplicationContext(), R.layout.row_layout_notes_listview);
        listView.setAdapter(notesListDataAdapter);
        dbHelper = new DbHelper(getApplicationContext());
        sqLiteDatabase = dbHelper.getReadableDatabase();
        cursor = dbHelper.getNotes(sqLiteDatabase);
        if(cursor.moveToFirst()){
            do{
                String  subject, notes;
                subject = cursor.getString(0);
                notes = cursor.getString(1);
                DataProvider dataProvider = new DataProvider(subject,notes);
                notesListDataAdapter.add(dataProvider);

            }while(cursor.moveToNext());
        }
        else{
            showMessage("No Notes found", "Get started by pressing the create button!");
        }
    }
    public void showMessage(String title, String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }

Aucun commentaire:

Enregistrer un commentaire