|
||
|
|
|
|||||||
| Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android |
![]() |
|
|
Herramientas |
|
#1
|
||||
|
||||
|
Pasar datos a un objeto i mostrar los datod de diccho objeto con bundle
Buenas, tengo els siguiente poblema:
Tengo una actividad en el qual pasaremos los datos de dicha actividad a un objeto. En una segunda actividad recogeremos los datos del objeto. Todo esto lo tengo que hacer con un bundle. Les paso el codigo. PrimeraActividad: Código:
package com.example.jparrot.bundles3;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener{
private EditText tbxNom,tbxCognoms,tbxContra,tbxMail,tbxAdre;
private Button btnEnre;
private User usuari;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbxNom= (EditText)findViewById(R.id.tbxNom);
tbxCognoms= (EditText)findViewById(R.id.tbxCognoms);
tbxContra= (EditText)findViewById(R.id.tbxContra);
tbxMail= (EditText)findViewById(R.id.tbxMail);
tbxAdre= (EditText)findViewById(R.id.tbxAdre);
btnEnre= (Button)findViewById(R.id.btnEnre);
btnEnre.setOnClickListener(this);
}
@Override
public void onClick(View v) {
usuari=new User(tbxNom.getText().toString(), tbxCognoms.getText().toString(), tbxContra.getText().toString(), tbxMail.getText().toString(), tbxAdre.getText().toString());
Intent titleIntent = new Intent(this,Activity2.class);
Bundle bundle= new Bundle();
bundle.putSerializable("codi_usuari", usuari);
titleIntent.putExtras(bundle);
this.startActivity(titleIntent);
titleIntent.putExtras(bundle);
this.startActivity(titleIntent);
}
}
Código:
package com.example.jparrot.bundles3;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String surname;
private String password;
private String email;
private String address;
public User(String name, String surname, String password, String email) {
super();
this.name = name;
this.surname = surname;
this.password = password;
this.email = email;
}
public User(String name, String surname, String password, String email,
String address) {
this(name,surname,password,email);
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
return true;
}
@Override
public String toString() {
return "User [name=" + name + ", surname=" + surname + ", password="
+ password + ", email=" + email + ", address=" + address + "]";
}
}
Código:
package com.example.jparrot.bundles3;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class Activity2 extends Activity {
private TextView lblNom,lblCog,lblCont,lblMail,lblAdre;
private User usuari;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
lblNom= (TextView)findViewById(R.id.lblNom);
lblCog= (TextView)findViewById(R.id.lblCog);
lblCont= (TextView)findViewById(R.id.lblCont);
lblMail= (TextView)findViewById(R.id.lblMail);
lblAdre= (TextView)findViewById(R.id.lblAdre);
Bundle extras = getIntent().getExtras();
if(extras!=null){
lblNom.setText(extras.getString(usuari.getName()));
/*lblCog.setText(extras.getString("clau_text"));
lblCont.setText(extras.getString("clau_text"));
lblMail.setText(extras.getString("clau_text"));
lblAdre.setText(extras.getString("clau_text"));*/
}
}
}
Espero que me puedan aydar. |
|
|
|
#2
|
||||
|
||||
|
El código de la segunda Activity no tiene ni pies ni cabeza.
Para empezar esto te va a dar un NullException: lblNom.setText(extras.getString(usuari.getName())) ; Porque usuari no está iniciado. Y luego, si has metido un objeto Serializable en el Bundle, lo que tendrás que sacar es otro objeto Serializable, el código sería este: usuari = (Usuario)extras.getSerializable("codi_usuari"); if (usuari!=null) { ........ } Y en los .... rellenas los datos de la ventana. Y por último una cosa, para tu caso va a dar un poco igual, porque son pocos datos, pero es muy problemático usar Serializable en lugar de Parcelable. Parcelable es infinitamente más rápido (aunque requiere mayor implementación), y habrá casos en los que una Activity no se te llegue a mostrar porque se produce un Time Out extrayendo los datos del Bundle. Este Time Out varía de un móvil a otro, pero a mi por ejemplo me llegado a pasar pasando Bitmaps (que son Parcelables, para más inri) lo que me ha obligado a buscar una tercera vía. |
![]() |
Estás aquí
|
||||||
|
||||||