vendredi 17 juillet 2015

Getting specific data from sqlite database

I need help requesting specific data from my sqlite database for my listview items. You see I need the values for each list item so I can send that data by intent.putExtra, that goes to another activity to perform a command. The code blocks below outline the specific areas that I need help in through comments. I have also attached the sqlite database I use to store values. In case someone needs reference to the database I use.

Also I am quite new to the stacksoverflow community, so let me know how was the quality of my question. If it was specific and to the point, or needs more information.

I appreciate any help received.

ListActivity

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

        db = new DatabaseHandler(this);

            alarms = db.getAllContacts();

//        db.addContact(new TestAlarm(title,totalTime));
        final TextView emptyViewForList = (TextView) findViewById(R.id.emptyTextViewForList);

       listOfAlarms = (ListView) findViewById(R.id.listView);
        alarmArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, alarms);

       listOfAlarms.setAdapter(alarmArrayAdapter);

//        if(listOfAlarms.getCount() <= 0){
//            emptyViewForList.setText("No Alarms Currently Available");
//            listOfAlarms.setEmptyView(emptyViewForList);
//        }

        listOfAlarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                alarms.get(position);



                Intent clockDownActivity = new Intent(ListOfAlarms.this, CountDownAct.class);

                /*

                This is the area I need help with. I need to send the title and time value that comes with each 
                listview item but I don't know how to get those values and send them by putExtras

                 */
                clockDownActivity.putExtra("Title", title);
                clockDownActivity.putExtra("totalTime", totalTime);
                startActivity(clockDownActivity);
            }
        });


    }

Sqlite database

public class DatabaseHandler extends SQLiteOpenHelper{

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_ALARMS = "alarms";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_TOTALTIME = "total_time";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_ALARMS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_TOTALTIME + " TEXT" + ")";
        db.execSQL(CREATE_ALARMS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);

        // Create tables again
        onCreate(db);
    }

    // Adding new contact
    public void addContact(TestAlarm alarm) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, alarm.getTitle()); // Contact Name
        values.put(KEY_TOTALTIME, alarm.getTotalTime()); // Contact Phone Number

        // Inserting Row
        db.insert(TABLE_ALARMS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    public TestAlarm getAlarm(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_ALARMS, new String[] { KEY_ID,
                        KEY_NAME, KEY_TOTALTIME }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        TestAlarm contact = new TestAlarm(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        // return contact
        return contact;
    }

    // Getting All Contacts
    public List<TestAlarm> getAllContacts() {


        List<TestAlarm> contactList = new ArrayList<TestAlarm>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_ALARMS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                TestAlarm contact = new TestAlarm();
                contact.set_id(Integer.parseInt(cursor.getString(0)));
                contact.setTitle(cursor.getString(1));
                contact.setTotalTime(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_ALARMS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

    // Updating single contact
    public int updateContact(TestAlarm alarm) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, alarm.getTitle());
        values.put(KEY_TOTALTIME, alarm.getTotalTime());

        // updating row
        return db.update(TABLE_ALARMS, values, KEY_ID + " = ?",
                new String[] {
                        String.valueOf(alarm.get_id())
                });
    }

    // Deleting single contact
    public void deleteContact(TestAlarm alarm) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_ALARMS, KEY_ID + " = ?",
                new String[] { String.valueOf(alarm.get_id()) });
        db.close();
    }

Aucun commentaire:

Enregistrer un commentaire