0
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

richard houw
  • 97
  • 1
  • 1
  • 7
  • 1
    The thing is, you need everything in your `ArrayList` to be a `String`, which in this case it is clearly not. Ultimately, the answer to your question depends on what you want the `String` for each `Contact` to look like. – Joe C Mar 26 '17 at 15:15
  • Maybe better to use Serializable? See http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android – Dmitry Gorkovets Mar 26 '17 at 15:16
  • @JoeC, I mean you can create your own class wrapping List implementing Serializable and use it. – Dmitry Gorkovets Mar 26 '17 at 15:30
  • why would you want to pass the entire list when the target activity could simply read it from your sqlite db? – pskink Mar 26 '17 at 15:31

3 Answers3

1

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));
}
1

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.

Remario
  • 3,813
  • 2
  • 18
  • 25
0

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?

Community
  • 1
  • 1
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • Serializable is not recommended on android, Therefore i am certain you meant Parceble? – Remario Mar 26 '17 at 15:32
  • Yeah, use `Parcelable`. This is why http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable – Paresh P. Mar 26 '17 at 15:41