Android意图在活动之间传递对象

时间:2014-09-28 07:24:41

标签: java android object android-intent

您好我正在尝试在活动之间传递对象,但看起来意图找不到活动。

有obect类:

public class Contacte implements Serializable {

private String nom;
private String cognoms;
private String telefon;
private int horari; // 0 = horari sense definir; 1 = 24h; 2 = Mati; 3 = Tarda

/**
 * Constructor sense parametres que permet crear un objecte contacte amb els
 * valors predefinits
 */
public Contacte(){
    nom = "Nom";
    cognoms = "Cognom";
    telefon = "Telefon";
    horari = 0;
}

/** Gettes and Setters **/

MainActivity:

// I use this cause I need to order the objects by priority
ArrayList<Contacte> contactes = new ArrayList<Contacte>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Create Contactes and add to the ArrayList
    Contacte c1 = new Contacte();
    Contacte c2 = new Contacte();
    Contacte c3 = new Contacte();

    c1.setNom("John");
    c1.setCognoms("Doe");
    c1.setTelefon("632145897"); 

    contactes.add(c1);
    contactes.add(c2);
    contactes.add(c3);


 // This is because I have 6 different buttons
    Button edita1 = (Button)findViewById(R.id.edita1);
    edita1.setOnClickListener(onClickListener);


}


private OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        switch(v.getId()){
            case R.id.edita1:

                // 1. create an intent pass class name or intnet action name
                Intent i = new Intent("com.ioc.eac1.EditaContacte");

                // 2. create person object
                Contacte c = new Contacte();
                c = contactes.get(0);

                // 3. put person in intent data
                i.putExtra("contacte", c);

                // 4. start the activity
                startActivity(i);
            break;
            case R.id.edita2:
                 //DO something
            break;
            case R.id.edita3:
                 //DO something
            break;
        }

  }

};

EditaContacte:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edita_contacte);

    // 1. get passed intent
    Intent intent = getIntent();

    // 2. get person object from intent
    Contacte c = (Contacte) intent.getSerializableExtra("contacte");

    // 3. get reference to person textView
    EditText editaNom = (EditText) findViewById(R.id.editaNom);

    // 4. display name & age on textView
    editaNom.setText(c.getNom());

}

还有Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EditaContacte"
        android:label="@string/title_activity_edita_contacte" >
    </activity>


</application>

我正在尝试调试它,看起来意图找不到活动。但它的活动是在清单上宣布的。我正在尝试在单击按钮时在一个活动中发送和创建对象,并在其他活动上显示对象属性

1 个答案:

答案 0 :(得分:0)

只需使用Intent.SetClass(MainActivity.this,EditaContacte.class)。

如果这两个活动在同一个应用程序中,则无需使用操作,只需设置类。