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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

Q&A

解決済

1回答

1484閲覧

fragmentで、Bundleから値を取得できない

m_s

総合スコア51

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

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

0グッド

0クリップ

投稿2019/08/25 15:52

下記のように、AdapterからFragmentにBundle経由でデータを送っている(putString)のですが、Fragment側でデータを取得できません。(getStringできない)
原因が不明のため、教えていただけないでしょうか。

Java

1 2import android.content.Context; 3import android.os.Bundle; 4import android.support.v4.app.FragmentManager; 5import android.support.v4.app.FragmentTransaction; 6import android.support.v7.widget.RecyclerView; 7import android.view.LayoutInflater; 8import android.view.View; 9import android.view.ViewGroup; 10import android.widget.ImageView; 11import android.widget.TextView; 12import android.widget.Toast; 13 14import java.util.ArrayList; 15 16 17public class MultiFriendAdapter extends RecyclerView.Adapter<MultiFriendAdapter.ViewHolder> implements View.OnClickListener { 18 19 private ArrayList<UserDataBean> userDataBeanList; 20 private UserDataBean userDataBean; 21 private Context context; 22 23 public MultiFriendAdapter(ArrayList<UserDataBean> userDataBeanList, Context context) { 24 this.userDataBeanList = userDataBeanList; 25 this.context = context; 26 } 27 28 29 static class ViewHolder extends RecyclerView.ViewHolder { 30 ImageView imageView; 31 TextView textView; 32 TextView messageView; 33 34 ViewHolder(View v) { 35 super(v); 36 imageView = v.findViewById(R.id.image_view); 37 textView = v.findViewById(R.id.text_view); 38 messageView = v.findViewById(R.id.email_view); 39 } 40 } 41 42 @Override 43 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 44 View view = LayoutInflater.from(parent.getContext()) 45 .inflate(R.layout.list_items, parent, false); 46 47 return new ViewHolder(view); 48 49 } 50 51 @Override 52 public void onBindViewHolder(final ViewHolder holder, int position) { 53 userDataBean = userDataBeanList.get(position); 54 holder.imageView.setImageResource(userDataBean.getPhotos()); 55 holder.textView.setText(userDataBean.getUserName()); 56 holder.messageView.setText(userDataBean.getMessage()); 57 58 holder.imageView.setOnClickListener(this); 59 holder.textView.setOnClickListener(this); 60 holder.messageView.setOnClickListener(this); 61 } 62 63 @Override 64 public int getItemCount() { 65 return userDataBeanList.size(); 66 } 67 68 @Override 69 public void onClick(View v) { 70 Bundle bundle = new Bundle(); 71 String name = userDataBean.getUserName(); 72 String message = userDataBean.getMessage(); 73 bundle.putString("name",name); 74 bundle.putString("message",message); 75 TalkFragment talkFragment = new TalkFragment(); 76 talkFragment.setArguments(bundle); 77 FragmentManager fragmentManager = ((MainActivity)context).getSupportFragmentManager(); 78 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 79 fragmentTransaction.addToBackStack(null); 80 fragmentTransaction.replace(R.id.container, talkFragment); 81 fragmentTransaction.commit(); 82 } 83}

Java

1 2import android.app.Activity; 3import android.content.ClipData; 4import android.content.ClipboardManager; 5import android.content.Context; 6import android.os.Bundle; 7import android.support.v4.app.Fragment; 8import android.support.v7.widget.LinearLayoutManager; 9import android.support.v7.widget.RecyclerView; 10import android.view.ContextMenu; 11import android.view.LayoutInflater; 12import android.view.MenuItem; 13import android.view.View; 14import android.view.ViewGroup; 15import android.widget.Button; 16import android.widget.Toast; 17 18import java.util.ArrayList; 19 20 21public class TalkFragment extends Fragment { 22 23 private RecyclerView mRecyclerView; 24 private RecyclerView.Adapter mAdapter; 25 private RecyclerView.LayoutManager mLayoutManager; 26 public static int click_pos; 27 private Activity activity; 28 private UserDataBean userDataBean; 29 30 ArrayList<UserDataBean> userDataBeanList = new ArrayList<UserDataBean>(); 31 32 public TalkFragment() { 33 // Required empty public constructor 34 } 35 36 @Override 37 public void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 } 41 42 @Override 43 public View onCreateView(LayoutInflater inflater, ViewGroup container, 44 Bundle savedInstanceState) { 45 46 View view = inflater.inflate(R.layout.fragment_talk, container, false); 47 48 Bundle bundle = new Bundle(); 49 activity = getActivity(); 50 activity.setTitle(bundle.getString("name")); 51 52 mRecyclerView = view.findViewById(R.id.my_recycler_view); 53 54 mRecyclerView.setHasFixedSize(true); 55 56 mLayoutManager = new LinearLayoutManager(getActivity()); 57 58 mRecyclerView.setLayoutManager(mLayoutManager); 59 60 mAdapter = new MultiAdapter(userDataBeanList,getActivity(),mRecyclerView); 61 mRecyclerView.setAdapter(mAdapter); 62 63 //コンテキストメニューを表示 64 registerForContextMenu(mRecyclerView); 65 66 ((Button)view.findViewById(R.id.button3)).setOnClickListener(new View.OnClickListener() { 67 @Override 68 public void onClick(View view) { 69 UserDataBean userDataBean = new UserDataBean("masa","1121",R.mipmap.ic_launcher_round); 70 userDataBeanList.add(userDataBean); 71 mAdapter.notifyDataSetChanged(); 72 } 73 }); 74 75 return view; 76 } 77 78} 79

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

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

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

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

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

guest

回答1

0

ベストアンサー

渡された Bundle を使わなければならないのに、新しく TalkFragmentBundle を生成しているので、getString できれ

Java

1Bundle bundle = new Bundle(); 2activity = getActivity(); 3activity.setTitle(bundle.getString("name"));

ではなく、

Java

1String name = getArguments().getString("name");

name を取得できるかと思います。

ただ、namenull チェックは忘れないように。

投稿2019/08/25 22:24

s.m_1

総合スコア293

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問