0

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! :)


What does the unsigned keyword do here?

I wrote some C++ code to show ASCII characters on a console which I found in a book i was reading. The code looked like this:

#include <iostream>

using namespace std;

int main()  
{  
    for (unsigned char i = 32; i<128; i++)  
       cout << i;  
    int response;  
    cin >> response;  
    return 0;  
}

When I take away the unsigned keyword and use signed instead, the results become infinite and the PC beeps until I shut off the executable file. But when I use an int i variable instead I don't need to unsign the variable. Why is that?

4

1 回答 1

0

只需调用每个对象,即可将其所拥有的sArrayAdapter<Object>调整Object为单个对象。TextViewtoString

这就是你得到这个结果的原因,你没有定义toStringUser所以它返回 java 对象的默认值(即className@objectHashCode

解决方法:toStringUser类中定义一个方法:

public String toString() {
    return mFirstName + ' ' + mLastName;  
}
于 2013-07-05T00:28:15.497 回答