質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

1552閲覧

2Bundleの値の受け取り方

masaakitsuyoshi

総合スコア102

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/03/30 09:11

編集2016/03/30 09:23

Bundleでつまずいています。カスタムリストビューを作っていて、タップした時に別のintentに値を渡し、表示させたいです。

bundleを使っていて、
Bundleに入れた値を別のintentに渡したいのですが、受け取る側はどう記述したらいいのでしょうか?

Bundleのインスタンスを作って、その中に入れたいデータを入れました。(テキスト、画像ソース)

次のintentで取り出したいのですがgetBundle?みたいなメソッドを使えばいいのでしょうか?

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MemberProfile memberProfile = (MemberProfile)listView.getItemAtPosition(position); Bundle bundle = new Bundle(); bundle.putString("list_names", memberProfile.getName()); bundle.putString("list_jobs", memberProfile.getJob()); bundle.putInt("list_images", memberProfile.getImage()); Intent sub = new Intent(); sub.setClassName("com.example.tsuyoshiokaprofile", "com.example.tsuyoshiokaprofile.SubActivity"); startActivity(sub); } });
public class SubActivity extends AppCompatActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub_activity); TextView text = (TextView) findViewById(R.id.textView); TextView text2 = (TextView) findViewById(R.id.textView2); TextView text3 = (TextView) findViewById(R.id.textView3); ImageView imageView = (ImageView) findViewById(R.id.imageView); Intent sub = getIntent(); // Bundle bundle = ???? String name = bundle.getString("list_names"); String job = bundle.getString("list_jobs"); int image = bundle.getInt("list_images"); text.setText(name); text2.setText(job); imageView.setImageResource(image); } }

追記

public class SubActivity extends AppCompatActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub_activity); TextView text = (TextView) findViewById(R.id.textView); TextView text2 = (TextView) findViewById(R.id.textView2); TextView text3 = (TextView) findViewById(R.id.textView3); ImageView imageView = (ImageView) findViewById(R.id.imageView); Intent sub = getIntent(); String name = sub.getStringExtra("list_names"); String job = sub.getStringExtra("list_jobs"); int image = sub.getParcelableExtra("list_images"); text.setText(name); text2.setText(job); imageView.setImageResource(image); } }
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MemberProfile memberProfile = (MemberProfile)listView.getItemAtPosition(position); Intent sub = new Intent(); sub.putExtra("list_names", memberProfile.getName()); sub.putExtra("list_jobs",memberProfile.getJob()); sub.putExtra("list_images",memberProfile.getImage()); sub.setClassName("com.example.tsuyoshiokaprofile", "com.example.tsuyoshiokaprofile.SubActivity"); startActivity(sub); } });

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

参考
https://teamtreehouse.com/community/how-to-pass-an-integer-between-activities

int型のデータをintentで受け渡しするには

getIntExtra("キー",初期数値);

初期数値を必ず?入れないといけないのがキモ
今回は、画像を表示したいので、初期数値には「getIntExtraをしない場合」に表示させたいリソースIDをかけばOK

getIntExtra("list_images",R.drawable.human);

ーーーー
String型のデータ受け渡しはシンプルに
getIntExtra("キー");

投稿2016/03/30 10:47

編集2016/03/30 10:50
masaakitsuyoshi

総合スコア102

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yona

2016/03/30 10:57

getIntExtraの第二引数は初期数値ではなくキーがIntent内にセットされていない時に返却される値です。
guest

0

遷移前の画面でBundleには入れていますが、Intentには入れていないので取得できません。
入れ方によって取得の仕方が異なるのでコードを修正してください。

投稿2016/03/30 09:14

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

masaakitsuyoshi

2016/03/30 09:23

追記しましたが、アプリ強制終了になって仕舞います。
yona

2016/03/30 09:27

アプリ強制終了と書くだけでは情報の提供にはなっていません。 ログキャットを追記してください。
yona

2016/03/30 09:31

memberProfile.getImage()の型はintですか、それともParcelableですか? 遷移前と後で型が異なるとエラーになります。
masaakitsuyoshi

2016/03/30 10:49

自己解決できました! 回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問