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); } });
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/30 10:57