richard houw
richard houw

Reputation: 97

Convert List<?> to List<String> in Android

public List<Contact> getContacts()
{
    SQLiteDatabase db;
    String cmd;
    List<Contact> name = new ArrayList<Contact>();
    db = myDb.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM contact;" , null);
    while(cursor.moveToNext())
    {
        int contactID       = Integer.parseInt(cursor.getString(0));
        String familyName   = cursor.getString(1);
        String firstName    = cursor.getString(2);
        int houseNumber     = Integer.parseInt(cursor.getString(3));
        String street       = cursor.getString(4);
        String town         = cursor.getString(5);
        String country      = cursor.getString(6);
        String postcode     = cursor.getString(7);
        int telephoneNumber = Integer.parseInt(cursor.getString(8));
        Contact contact     = new Contact(contactID,familyName,firstName,houseNumber,street,town,country,postcode,telephoneNumber);
    }
    Intent intent = new Intent(Database.this,MainActivity.class);
    intent.putStringArrayListExtra("contacts", name);
    startActivity(intent);

    return name;
}

The method putStringArrayListExtra(String, ArrayList<String>) in the type Intent is not applicable for the arguments (String, List<Contact>)

in order to use putStringArrayListExtra(); method, i need to convert List<contact> name to List name, How do i convert it? thank you

Upvotes: 0

Views: 2036

Answers (3)

Paresh
Paresh

Reputation: 6857

Seems like you are trying to pass your List<Contacts> to via Intent -

No need to use putStringArrayListExtra() methods as there is putParcelableArrayList() method using which you can pass ArrayList<Contact> directly.

Simply use,

putParcelableArrayList("KEY", your ArrayList<Contact>); // Make sure Contact implementes Parcelable 

You can get the ArrayList<Contact> in Receiver Activity -

Bundle b = getIntent().getExtras();
ArrayList<Contact> data = b.getParcelableArrayList("KEY");

However, You can use Serializable instead of Parcelable but Parcelable is recommended as it is like Flash. Here is why?

Upvotes: 0

Remario
Remario

Reputation: 3863

First Declare a new Array Of Strings.

List<String> newList = new ArrayList<String>(list.size());
newList.addAll(list);

This adds all elements into the new String List. Where list is your previous container.

Also to pass objects between components use Parceble, Android uses Binder to facilitate such communication in a highly optimized way. The Binder communicates with Parcels, which is a message container. The Binder marshals the Parcel to be sent, sends and receives it, and then unmarshals it on the other side to reconstruct a copy of the original Parcel.Please read up on it here.

Upvotes: 1

Naser Khoshfetrat
Naser Khoshfetrat

Reputation: 152

because list of object is actually not a list of strings, the best way as i know is to loop over list and convert each item into a new list of strings :

List<String> stringsList = new ArrayList<>(list.size());
for (Object object : list) {
    stringsList.add(Objects.toString(object, null));
}

Upvotes: 1

Related Questions