Home > Net >  How to get the data from the object value in the arraylist in Android?
How to get the data from the object value in the arraylist in Android?

Time:07-29

I am working on the getAllContacts function to storing the list of data in the arraylist with the object values, so I can fetch the firstname data using the object value.

Example:

Chris
Chris
Chris
Chris

When I try this:

for (Object object : getContacts) {
    object = getContacts.get(idx);
    String firstname = object.getClass().getName();
    Log.e("message", "getContacts....."   getContacts);
}

Return:

E/message: firstname.....com.loginpage.database.DBHelper$DBData E/message: firstname.....com.loginpage.database.DBHelper$DBData

Here is the return getContacts:

E/message: getContacts....[[firstname=Chris, lastname=Test8, email=, photo=images/photo_0aea.jpg], [firstname=Chris, lastname=Test7, email=, photo=images/photo_f0d5.jpg], [firstname=Chris, lastname=Test23, email=, photo=images/photo_f198.jpg]

Here is the DBData class:

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDBName.db";
    public static final String CONTACTS_TABLE_NAME = "contacts";
    public static final String ID = "id";
    public static final String CONTACTS_COLUMN_FIRSTNAME = "firstname";
    public static final String CONTACTS_COLUMN_LASTNAME = "lastname";
    public static final String CONTACTS_COLUMN_EMAIL = "email";
    public static final String CONTACTS_COLUMN_PHOTO = "photo";
    //public ArrayList<MyObject> myObjects = new ArrayList();
    private HashMap hp;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String query = "CREATE TABLE "   CONTACTS_TABLE_NAME   " ("   ID   " INTEGER PRIMARY KEY AUTOINCREMENT, "
                  CONTACTS_COLUMN_FIRSTNAME   " TEXT NOT NULL, "
                  CONTACTS_COLUMN_LASTNAME   " TEXT NOT NULL, "
                  CONTACTS_COLUMN_EMAIL   " TEXT NOT NULL, "
                  CONTACTS_COLUMN_PHOTO   " TEXT NOT NULL)";
        db.execSQL(query);
    }


    @SuppressLint("Range")
    //public ArrayList<DBData> getAllContacts() {
    public List<DBData> getAllContacts() {
        //ArrayList<String> array_list = new ArrayList<String>();
        ArrayList<DBData> array_list = new ArrayList<>();

        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from contacts", null);
        res.moveToFirst();

        while (res.isAfterLast() == false) {
            String firstname = res.getString(res.getColumnIndex(CONTACTS_COLUMN_FIRSTNAME));
            String lastname = res.getString(res.getColumnIndex(CONTACTS_COLUMN_LASTNAME));
            String email = res.getString(res.getColumnIndex(CONTACTS_COLUMN_EMAIL));
            String photo = res.getString(res.getColumnIndex(CONTACTS_COLUMN_PHOTO));
            DBData DB_ROWS = new DBData(firstname , lastname, email, photo);
            array_list.add(DB_ROWS);
            res.moveToNext();
        }
        return array_list;
    }

       public class DBData {
            private String firstname;
            private String lastname;
            private String email;
            private String photo;
    
            //Step 2: Creating Constructor of Student class
            public DBData(String firstname, String lastname, String email, String photo) {
                this.firstname = firstname;
                this.lastname = lastname;
                this.email = email;
                this.photo = photo;
            }
    
            //Step 3: Getting and Setting the values
            public String getFirstname() {
                return firstname;
            }
    
            public void setName(String firstname) {
                this.firstname = firstname;
            }
    
            public String getLastname() {
                return lastname;
            }
    
            public void setLastname(String lastname) {
                this.lastname = lastname;
            }
    
            public String getEmail() {
                return email;
            }
    
            public void setEmail(String email) {
                this.email = email;
            }
    
            public String getPhoto() {
                return photo;
            }
    
            public void setPhoto(String photo) {
                this.photo = photo;
            }
    
            @Override
            public String toString() {
    
                return "[firstname="   firstname   ", lastname="   lastname   ", email="   email   ", photo="   photo  "]";
    
            }
        }
    }
}

Here is the mainactivity:

private ArrayList<DBHelper.DBData> getContacts;

protected void onCreate(Bundle savedInstanceState) {

    mydb = new DBHelper(this);
    getContacts = (ArrayList<DBHelper.DBData>) mydb.getAllContacts();

    for (Object object : getContacts) {
        object = getContacts.get(idx);
        String firstname = object.getClass().getName();
        Log.e("message", "getContacts....."   getContacts);
    }
}

Here is what I want to achieve to fetch the firstname data:

Chris
Chris
Chris
Chris

I am unable to find the getFirstName property as it wont show up. So I am unable to find out how to fetch the firstname data using the object value. I guess the code I wrote in DBHelper class is incorrect.

Can you please show me an example how I can fetch the data using the object value firstname??

Thank you.

CodePudding user response:

Your code at:

for (Object object : getContacts) {

Is suppose to iterate through DBData(s).

So I suggest turning your code from that into this:

getContacts = (ArrayList<DBHelper.DBData>) mydb.getAllContacts();
for (DBHelper.DBData data : getContacts) {
    String firstname = data.getFirstname();
    Log.d("message", "This is the firstname: " firstname);
}

The reason behind "why can't I access the method" is because you typed-cast it into an Object, and since everything in Java (except for primitives) can be typed-cast into an Object, you get no compilation errors.

  • Related