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

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

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

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

Android Studio

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

Q&A

解決済

1回答

1493閲覧

Edit textで入力した文字列を遷移元のフラグメントに返したい

suguru_sato

総合スコア23

Java

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

Android Studio

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

0グッド

0クリップ

投稿2020/02/07 14:58

fragmentA内のListViewのitem(adapterのインスタンスを生成するときに引数として与えているもの)をタップするとfragmentBに遷移してコメントを入力するedit textの画面に遷移します。
fragmentA内のListViewに対してadapterのフィールド変数はitemだけではなくArrayList<String>もあります。遷移先のfragmentB内で入力したテキストをボタン押下後にアダプタ内のArrayList<String>に加え、ListViewに反映させたいです

onActivityResultをフラグメントAに作ればfragmentBから遷移元にデータを引き継げるかと思いましたが、fragmentAはオーバーライドできないようでした。
現在、どう手を付ければよいかわからない状態です

fragmentA

1public class UploadRouteFragment extends Fragment implements OnMapReadyCallback, LocationListener, 2 View.OnClickListener, CompoundButton.OnCheckedChangeListener, GoogleMap.OnMarkerClickListener { 3 4途中省略 5 6 public void onClick(View view) { 7 switch (view.getId()) { 8 case R.id.button: 9 10 break; 11 12 case R.id.button2: 13 14 final BaseAdapter adapter = new EditListAdapter(getContext(), R.layout.list_items, mMarkerList, commentList); 15 // ListViewにadapterをセット 16 listView.setAdapter(adapter); 17 //リストビューを押したら 18 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 19 @Override 20 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 21 mEditWindowFragment=new EditWindowFragment(); 22 FragmentTransaction transaction1=getActivity().getSupportFragmentManager().beginTransaction(); 23 transaction1.add(R.id.frameLayout,mEditWindowFragment); 24 transaction1.commit(); 25 } 26 }); 27 } 28

fragmentB

1public class EditWindowFragment extends Fragment { 2 private EditText edit; 3 private UploadRouteFragment mUpfragment; 4 static String textComment; 5 6 7 @Override 8 public View onCreateView(LayoutInflater inflater, ViewGroup container, 9 Bundle savedInstanceState) { 10 // Inflate the layout for this fragment 11 return inflater.inflate(R.layout.memo_add, container, false); 12 } 13 14 @Override 15 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 16 super.onActivityCreated(savedInstanceState); 17 edit=getActivity().findViewById(R.id.editText); 18 Button button = getActivity().findViewById(R.id.editfinish); 19 20 button.setOnClickListener(new View.OnClickListener() { 21 @Override 22 public void onClick(View v) { 23 // エディットテキストのテキストを取得 24 textComment = edit.getText().toString(); 25 26 } 27 }); 28 29 } 30 31} 32

adapter

1public class EditListAdapter extends BaseAdapter { 2 private LayoutInflater inflater; 3 private int layoutID; 4 private ArrayList<Marker> mMarkerList; 5 private ArrayList<String> mBaseCommentList; 6 7 8 static class ViewHolder { 9 TextView text; 10 TextView text2; 11 } 12 13 //コンストラクタ 14 //context,itemLayoutIDというリスト要素の番号,アップロードするためのマーカーリスト,マップに対するコメント 15 EditListAdapter(Context context, int itemLayoutId, 16 ArrayList<Marker> MarkerList, ArrayList<String> BaseCommentList){ 17 inflater = LayoutInflater.from(context); 18 layoutID = itemLayoutId; 19 mMarkerList= MarkerList; 20 mBaseCommentList=BaseCommentList; 21 } 22 23 @Override 24 public View getView(int position, View convertView, ViewGroup parent) { 25 26 ViewHolder holder; 27 28 if (convertView == null) { 29 convertView = inflater.inflate(layoutID, null); 30 holder = new ViewHolder(); 31 holder.text = convertView.findViewById(R.id.text_view); 32 holder.text2=convertView.findViewById(R.id.text_view2); 33 convertView.setTag(holder); 34 } else { 35 holder = (ViewHolder) convertView.getTag(); 36 } 37 holder.text.setText(mMarkerList.get(position).getTitle()); 38 39 return convertView; 40 } 41 42 43 @Override 44 public int getCount() { 45 return mMarkerList.size(); 46 } 47 48 @Override 49 public Object getItem(int position) { 50 return position; 51 } 52 53 @Override 54 public long getItemId(int position) { 55 return position; 56 } 57 58} 59

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

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

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

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

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

hoshi-takanori

2020/02/08 05:50

フラグメントはアクティビティではないので、onActivityResultは使えません。 以下の記事のようにフラグメントBからアクティビティに結果を渡して、アクティビティ側でListViewのデータを更新するか、アクティブティからフラグメントAに結果を転送する必要があると思います。 http://y-anz-m.blogspot.com/2012/06/fragment-activity.html
guest

回答1

0

ベストアンサー

サンプルになると良いのですが.
何処かに不具合があるかもしれないのは予めご了承ください.

MainActivity.java

java

1import androidx.appcompat.app.AppCompatActivity; 2import androidx.fragment.app.FragmentTransaction; 3 4import android.content.Context; 5import android.os.Bundle; 6import android.view.*; 7import android.widget.*; 8import java.util.*; 9 10class Item { 11 long id; 12 String fixed, comment; 13} 14class Adapter extends BaseAdapter { 15 private class ViewHolder { 16 TextView text1; 17 TextView text2; 18 } 19 private LayoutInflater inflater; 20 private List<Item> list = new ArrayList<>(); 21 Adapter(Context context, List<String> fixedData) { 22 inflater = LayoutInflater.from(context); 23 long id = 0; 24 for(String data : fixedData) { 25 Item item = new Item(); 26 item.id = id++; 27 item.fixed = data; 28 item.comment = ""; 29 list.add(item); 30 } 31 } 32 void setComment(long id, String text) { 33 Item item = getItem(id); 34 if(item != null) { 35 item.comment = text; 36 notifyDataSetChanged(); 37 } 38 } 39 Item getItem(long id) { 40 for(Item item : list) if(item.id == id) return item; 41 return null; 42 } 43 @Override 44 public int getCount() { 45 return list.size(); 46 } 47 @Override 48 public Object getItem(int position) { 49 return list.get(position); 50 } 51 @Override 52 public long getItemId(int position) { 53 return list.get(position).id; 54 } 55 @Override 56 public View getView(int position, View convertView, ViewGroup parent) { 57 ViewHolder holder = null; 58 if(convertView == null) { 59 convertView = inflater.inflate(R.layout.list_item_layout, null); 60 holder = new ViewHolder(); 61 holder.text1 = convertView.findViewById(R.id.text1); 62 holder.text2 = convertView.findViewById(R.id.text2); 63 convertView.setTag(holder); 64 } else { 65 holder = (ViewHolder)convertView.getTag(); 66 } 67 holder.text1.setText(list.get(position).fixed); 68 holder.text2.setText(list.get(position).comment); 69 return convertView; 70 } 71}; 72public class MainActivity extends AppCompatActivity implements ListFragment.ListModel, ItemFragment.Regist { 73 private Adapter adapter; 74 @Override 75 protected void onCreate(Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 setContentView(R.layout.activity_main); 78 adapter = new Adapter(this, Arrays.asList("AAA","BBB","CCC","DDD")); 79 showListFragment(); 80 } 81 void showListFragment() { 82 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 83 transaction.replace(R.id.flagmentContainer, ListFragment.createInstance()); 84 transaction.commit(); 85 } 86 void showItemFragment(long id, String fixed, String comment) { 87 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 88 transaction.replace(R.id.flagmentContainer, ItemFragment.createInstance(id, fixed, comment)); 89 transaction.addToBackStack(null); 90 transaction.commit(); 91 } 92 @Override 93 public ListAdapter getAdapter() { 94 return adapter; 95 } 96 @Override 97 public void onSelect(long id) { 98 Item item = adapter.getItem(id); 99 showItemFragment(id, item.fixed, item.comment); 100 } 101 @Override 102 public void onRegist(long id, String text) { 103 adapter.setComment(id, text); 104 showListFragment(); 105 } 106}

ListFragment.java

java

1import android.content.Context; 2import android.os.Bundle; 3import android.view.*; 4import android.widget.*; 5 6import androidx.annotation.NonNull; 7import androidx.annotation.Nullable; 8import androidx.fragment.app.Fragment; 9 10public class ListFragment extends Fragment { 11 static ListFragment createInstance() { 12 return new ListFragment(); 13 } 14 interface ListModel { 15 ListAdapter getAdapter(); 16 void onSelect(long id); 17 } 18 private ListModel listModel; 19 private ListView listView; 20 @Nullable 21 @Override 22 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 23 View v = inflater.inflate(R.layout.list_fragment, container, false); 24 listView = v.findViewById(R.id.listView); 25 listView.setAdapter(listModel.getAdapter()); 26 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 27 @Override 28 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 29 listModel.onSelect(id); 30 } 31 }); 32 return v; 33 } 34 @Override 35 public void onAttach(Context context) { 36 super.onAttach(context); 37 if(context instanceof ListModel) { 38 listModel = (ListModel)context; 39 } 40 } 41}

ItemFragment.java

java

1import android.content.Context; 2import android.os.Bundle; 3import android.view.*; 4import android.widget.*; 5 6import androidx.annotation.NonNull; 7import androidx.annotation.Nullable; 8import androidx.fragment.app.Fragment; 9 10public class ItemFragment extends Fragment { 11 private static final String ARGS_ID = "id"; 12 private static final String ARGS_FIXED = "fixed"; 13 private static final String ARGS_COMMENT = "comment"; 14 static ItemFragment createInstance(long id, String fixed, String comment) { 15 ItemFragment fragment = new ItemFragment(); 16 Bundle args = new Bundle(); 17 args.putLong(ARGS_ID, id); 18 args.putString(ARGS_FIXED, fixed); 19 args.putString(ARGS_COMMENT, comment); 20 fragment.setArguments(args); 21 return fragment; 22 } 23 interface Regist { 24 void onRegist(long id, String text); 25 } 26 @Override 27 public void onCreate(@Nullable Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 } 30 private Regist regist; 31 @Nullable 32 @Override 33 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 34 View v = inflater.inflate(R.layout.item_fragment, container, false); 35 TextView fixed = v.findViewById(R.id.fixed); 36 final EditText editText = v.findViewById(R.id.editText); 37 Bundle args = getArguments(); 38 if(args != null) { 39 fixed.setText(args.containsKey(ARGS_FIXED) ? args.getString(ARGS_FIXED) : ""); 40 editText.setText(args.containsKey(ARGS_COMMENT) ? args.getString(ARGS_COMMENT) : ""); 41 } 42 Button button = v.findViewById(R.id.button); 43 button.setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View v) { 46 Bundle args = getArguments(); 47 if(args != null && args.containsKey(ARGS_ID)) { 48 regist.onRegist(args.getLong(ARGS_ID), editText.getText().toString()); 49 } 50 } 51 }); 52 return v; 53 } 54 @Override 55 public void onAttach(final Context context) { 56 super.onAttach(context); 57 if(context instanceof Regist) { 58 regist = (Regist)context; 59 } 60 } 61}

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity" 7 android:orientation="horizontal" 8 android:id="@+id/flagmentContainer"/>

list_fragment.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <ListView 7 android:id="@+id/listView" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" /> 10</LinearLayout>

list_item_layout.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/text1" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content"/> 11 <TextView 12 android:id="@+id/text2" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"/> 15</LinearLayout>

item_fragment.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <TextView 9 android:id="@+id/fixed" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_weight="0" 13 app:layout_constraintTop_toTopOf="parent" 14 app:layout_constraintLeft_toLeftOf="parent" 15 app:layout_constraintRight_toRightOf="parent"/> 16 <EditText 17 android:id="@+id/editText" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:ems="10" 22 android:gravity="start|top" 23 android:inputType="textMultiLine" 24 app:layout_constraintTop_toBottomOf="@id/fixed" 25 app:layout_constraintBottom_toTopOf="@id/button" 26 app:layout_constraintLeft_toLeftOf="parent" 27 app:layout_constraintRight_toRightOf="parent"/> 28 <Button 29 android:id="@+id/button" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_weight="0" 33 android:text="Regist" 34 app:layout_constraintBottom_toBottomOf="parent" 35 app:layout_constraintLeft_toLeftOf="parent" 36 app:layout_constraintRight_toRightOf="parent"/> 37</androidx.constraintlayout.widget.ConstraintLayout>

投稿2020/02/08 13:09

jimbe

総合スコア12632

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

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

suguru_sato

2020/02/09 08:45

ありがとうございます。サンプル通り実装しました。jimbeさんの場合アクティビティ内のonCreateでadapterの定義を行っていますが、私の場合、アクティビティはメニュータブの切り替え機能とフラグメントの入れ替えについてのみにしています。したがって、フラグメントにボタンを設置し、押したら adapter = new Adapter(this, Arrays.asList("AAA","BBB","CCC","DDD")); showListFragment(); としました。しかし、 listView.setAdapter(listModel.getAdapter()); のgetAdapter()でNUllが起きてしまっています。 listModelインタフェースはフラグメントに実装してしまうとgetAdapter()を参照できないのでしょうか?
jimbe

2020/02/09 15:05

どの部分をどう変えられたのかが分かり難いのですが, 「ボタンを設置」したというフラグメントは, 私のコードの ListFragment/ItemFragment より前に表示されているフラグメントでしょうか. そしてそのフラグメントにも listModel を実装されたのでしょうか. それ以外を変更されていないとしますと, ListFragment.listModel の実体は onAttach で渡された context = MainActivity で, 『「ボタンを設置」されたフラグメント』ではありません. フラグメントを渡すのであれば onAttach 以外の方法で渡す必要があります. ただ, あるフラグメントのデータを別のフラグメントが生成するというのはフラグメント間の関係が強すぎるように思います. フラグメントは画面の部品ですので表示のみに関わり, データやその他の部分をアクティビティが管理するほうが纏まりが良いのではないでしょうか.
suguru_sato

2020/02/20 09:38

jimbeさんのとおりフラグメントは部品であり、フラグメント間でのデータのやり取りはできるだけしないほうが良いと判断しました。そのため、リストのあるフラグメントからインテントを作り編集画面のアクティビティにとびSQLiteを用いてローカルデータベースにコメントを保存しました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問