lundi 29 février 2016

How to make list view items clickable? Android SQL Lite DB

i will attempt to keep this short and concise. I am currently developing an android application, and am stuck on a particular element.

The below is an example code i have been reading through, from my understanding it is taking a list of values inserting them in to the db / table, then cycling through them with the cursor and displaying with the list view. On running the application all items are displayed in a list as expected.

I would like it so that when an item in the list is clicked a new page is opened via intent, or a different set of database values is displayed on the current page.

How could this be achieved

Thank you!

public class SQLiteCrudExample  extends ListActivity {
//DB name
private final String dbName = "Android";
//Table name
private final String tableName = "Versions";
//String array has list Android versions which will be populated in the list
private final String[] versionNames= new String[]{"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "Kitkat"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<String> results = new ArrayList<String>();
    //Declare SQLiteDatabase object
    SQLiteDatabase sampleDB = null;

    try {
        //Instantiate sampleDB object
        sampleDB =  this.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
        //Create table using execSQL
        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName +    " (versionname VARCHAR);");
        //Insert Android versions into table created
        for(String ver: versionNames){
            sampleDB.execSQL("INSERT INTO " + tableName + " Values ('"+ver+"');");
        }

        //Create Cursor object to read versions from the table
        Cursor c = sampleDB.rawQuery("SELECT versionname FROM " + tableName, null);
        //If Cursor is valid
        if (c != null ) {
            //Move cursor to first row
            if  (c.moveToFirst()) {
                do {
                    //Get version from Cursor
                    String firstName = c.getString(c.getColumnIndex("versionname"));
                    //Add the version to Arraylist 'results'
                    results.add(firstName);
                }while (c.moveToNext()); //Move to next row
            } 
        }

        //Set the ararylist to Android UI List
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

    } catch (SQLiteException se ) {
        Toast.makeText(getApplicationContext(), "Couldn't create or open the database", Toast.LENGTH_LONG).show();
    } finally {
        if (sampleDB != null) {
            sampleDB.execSQL("DELETE FROM " + tableName);
            sampleDB.close();
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire