So I want to make a list of students and courses, modify their data, add/remove courses to a student, etc.
So I create my students using parcelable:
package com.example.exempleparcelable;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable
{
private String mFirstName;
private String mLastName;
private String mPseudo;
private String mMail;
private String mTwitter;
private String mGooglePlus;
public User(String firstName, String lastName, String pseudo, String mail, String twitter, String googlePlus)
{
super();
this.mFirstName = firstName;
this.mLastName = lastName;
this.mPseudo = pseudo;
this.mMail = mail;
this.mTwitter = twitter;
this.mGooglePlus = googlePlus;
}
/*
* Constructeur qui permet de créer l'objet à partir d'un Parcel
*/
public User(Parcel in) {
this.mFirstName = in.readString();
this.mLastName = in.readString();
this.mPseudo = in.readString();
this.mMail = in.readString();
this.mTwitter = in.readString();
this.mGooglePlus = in.readString();
}
/*
* Cette classe implémente Parcelable qui l'oblige à implémenter:
* describeContents : pour décrire le contenu du Parcel
* writeToParcel : pour écrire l’objet dans un Parcel.
*/
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(mFirstName);
dest.writeString(mLastName);
dest.writeString(mPseudo);
dest.writeString(mMail);
dest.writeString(mTwitter);
dest.writeString(mGooglePlus);
}
/*
* Le CREATOR permet d’indiquer comment l'objet de type Parcelable sera créé.
*/
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>()
{
@Override
public User createFromParcel(Parcel source)
{
return new User(source);
}
@Override
public User[] newArray(int size)
{
return new User[size];
}
};
public String getmFirstName()
{
return mFirstName;
}
public String getmLastName()
{
return mLastName;
}
public String getmPseudo()
{
return mPseudo;
}
public String getmMail()
{
return mMail;
}
public String getmTwitter()
{
return mTwitter;
}
public String getmGooglePlus()
{
return mGooglePlus;
}
public static Parcelable.Creator<User> getCreator()
{
return CREATOR;
}
}
I have a class to add students and I want to put them into an arraylist so I could put it into an spinner.
package com.example.exempleparcelable;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class ActivitePrincipale extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activite_principale);
final ArrayList<Object> listeEtudiants = new ArrayList<Object>();
final ArrayAdapter<Object> adapterEleves;
adapterEleves = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, listeEtudiants);
Button connectBtn = (Button) findViewById(R.id.connect);
connectBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int i = 0;
User[] user = new User[999];
user[i] = new User("Tom", "Sawyer", "Tom_Sawyer", "tom@sawyer.com", "@tom_Sawyer", "Tom_Sawyer");
Intent intent = new Intent(ActivitePrincipale.this, ResultActivity.class);
listeEtudiants.add(user[i]);
adapterEleves.notifyDataSetChanged();
intent.putExtra("user", user[i]);
i++;
startActivity(intent);
}
});
Spinner spinnerEtudiants = (Spinner) findViewById(R.id.spinnerEtudiants);
spinnerEtudiants.setAdapter(adapterEleves);
adapterEleves.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
}
Adding the student to the spinner is ok but it shows something like this:
com.example.exempleparcelable.User@...
I am sure it's normal because of the object, but I wonder how could I show the spinner with using the Firs Name and Last Name or pseudo, etc. ?
Thank you in advance! :)