vendredi 2 octobre 2015

Display data value from DB to screen

I want to display my data from my database to screen in a grip view but it is not working. When i ran it nothing happens no error or log

Here is the code on my MainActivity:

DB db = new DB(this);

    // Reading all contacts
    Log.d("Reading: ", "Reading all contacts..");
    List<Contact> contacts = db.getAllContacts();

    for (Contact cn : contacts) {
        String log = "Id: "+cn.getId()+" ,Name: " + cn.getName() + " ,Time: " + cn.getTime();
        // Writing Contacts to log
        Log.d("Name: ", log);

    }

    db.getAllContacts();

    gridView = (GridView) findViewById(R.id.gridView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, ArrayofName);
    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
        }
    });

And here is the code on my DB:

//Getting All Contacts1
    public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

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

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

    // return contact list
    return contactList;
}

I am not sure where excatly is the problem but I believe it is in my MainActivity, thanks in advance!

Aucun commentaire:

Enregistrer un commentaire