2017-09-09 16 views
0

は、ユーザーの情報が表示されるmy activity_profile.xmlです。ログイン活動からウェルカムメッセージをプロファイル活動に設定するにはどうすればよいですか?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg8" 
tools:context="com.example.bencel9314.petcareinformationsystem.ProfileActivity"> 


<ImageView 
    android:id="@+id/ivGallery" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="200dp" 
    app:srcCompat="@android:drawable/ic_menu_gallery" 
    android:layout_alignParentEnd="true" /> 

<ImageView 
    android:id="@+id/ivProfilePicture" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@drawable/catprofile" 
    android:layout_alignParentStart="true" 
    android:layout_above="@+id/welcome_message_layout" /> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/ivGallery" 
    android:id="@+id/welcome_message_layout"> 

    <TextView 
     android:id="@+id/welcome_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Welcome to your Profile" 
     android:textColor="#006" 
     android:textSize="28sp" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <EditText 
     android:id="@+id/etName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:layout_marginTop="13dp" 
     android:layout_below="@+id/welcome_title" 
     android:layout_centerHorizontal="true" /> 

    <EditText 
     android:id="@+id/etUserName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:layout_below="@+id/etName" 
     android:layout_alignStart="@+id/etEmailAddress" /> 

    <EditText 
     android:id="@+id/etEmailAddress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textEmailAddress" 
     android:layout_below="@+id/etName" 
     android:layout_alignStart="@+id/etName" 
     android:layout_marginTop="42dp" /> 

    <EditText 
     android:id="@+id/etContactNumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="phone" 
     android:layout_below="@+id/etEmailAddress" 
     android:layout_alignStart="@+id/etEmailAddress" /> 


</RelativeLayout> 

、ここで私のProfileActivity.java

私は、ユーザーの名前、ユーザー名、電子メールアドレスと連絡先の番号を示すウェルカムメッセージを作るために使用する必要がありますどのようなコード
public class ProfileActivity extends AppCompatActivity { 

ImageView ivGallery, ivProfilePicture; 
GalleryPhoto galleryPhoto; 
final int GALLERY_REQUEST = 12345; 



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


    final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
    final EditText etEmailAddress = (EditText) findViewById(R.id.etEmailAddress); 
    final EditText etContactNumber = (EditText) findViewById(R.id.etContactNumber); 

    galleryPhoto = new GalleryPhoto(getApplicationContext()); 

    ivGallery = (ImageView) findViewById(R.id.ivGallery); 
    ivProfilePicture = (ImageView) findViewById(R.id.ivProfilePicture); 

    ivGallery.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivityForResult(galleryPhoto.openGalleryIntent(), GALLERY_REQUEST); 
     } 
    }); 
} 
/** 
* Dispatch incoming result to the correct fragment. 
* 
* @param requestCode 
* @param resultCode 
* @param data 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == GALLERY_REQUEST) { 
      Uri uri = data.getData(); 
      galleryPhoto.setPhotoUri(uri); 
      String photopath = galleryPhoto.getPath(); 
      try { 
       Bitmap bitmap = ImageLoader.init().from(photopath).requestSize(1200, 800).getBitmap(); 
       ivProfilePicture.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
       Toast.makeText(getApplicationContext(), 
         "Something went wrong while choosing photos", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 

} 

ですprofileactivity.java?

答えて

1

に....他の用途のインテントに1つの活動からのデータを渡すための簡単な方法を私はユーチューブからチュートリアルを試みたが、それらのほとんどは動作しません。ログインアクティビティから は行います

Intent i = new Intent(this, ProfileActivity.class); 
intent.putExtra("WELCOME", "Welcome message goes here!"); 
startActivity(intent); 

そしてプロフィールの活動から、あなたが意図してメッセージにアクセスすることができます。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 
    String welcomeMsg = ""; 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     welcomeMsg = extras.getString("WELCOME"); 
    } 
    else { 
     welcomeMsg = "Default welcome message!"; 
    } 
} 
1

あなたがこれを達成できるいくつかの方法があります。

  1. インテントで値を渡す。

バンドルオブジェクトを作成し、ProfileActivityを開始するための意図にそれを追加します。

Bundle bundle = new Bundle(); 
bundle.putString(WELCOME_MESSAGE, message); 
bundle.putString(USER_NAME, name); 
bundle.putString(FULL_NAME, fullname); 
bundle.putString(EMAIL, email); 
bundle.putString(ADDRESS, address); 
bundle.putString(PHONE, phone); 
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class); 
intent.putExtras(bundle); 
startActivity(intent); 

はその後のonCreateメソッドでは、あなたのプロフィールの活動に、あなたはあなたの値を取得することができます

Bundle bundle = getIntent().getExtras(); 
if(bundle != null) { 
String name = bundle.getString(USER_NAME); 
String fullName = bundle.getString(FULL_NAME, fullName); 
String email = bundle .getString(EMAIL); 
String address = bundle.getString(ADDRESS); 
String address = bundle.getString(PHONE); 
String message = bundle .getString(WELCOME_MESSAGE, "default message"); 
} 
関連する問題