Fragmentの中にあるEditTextの値を親であるActivityに渡す場合、ちょっと面倒な仕込みが必要になります。
ReviewActivityというActivityの中に、ReviewReceiverFragmentが複数含まれます。
ReviewReceiverFragmentに、点数をカウントするチェックボックスとコメントを記載する欄があります。
まずはFragmentです。
//ReviewReceiverFragment.java public class ReviewReceiverFragment extends Fragment { private ArrayList<Sender> senderArrayList; public CheckBox unloading_wait_time; private OnGetFromUserClickListener mListener; private int receiver_id; private CheckBox unloading_place_info; private EditText comment_edit_text; private final static String TAG = "ReviewReceiverFragment"; public ReviewReceiverFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.review_receiver_fragment, container, false);
//中略
unloading_wait_time = (CheckBox) v.findViewById(R.id.unloading_wait_time); unloading_place_info = (CheckBox) v.findViewById(R.id.unloading_place_info); comment_edit_text = (EditText) v.findViewById(R.id.comment); unloading_wait_time.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.unloading_wait_time) { sendRatingToActivity(v); } } }); unloading_place_info.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.unloading_place_info) { sendRatingToActivity(v); } } }); comment_edit_text.addTextChangedListener(mTextWatcher); return v; } private TextWatcher mTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { // String comment = comment_edit_text.getText().toString(); Log.d(TAG, "afterTextChanged" + s.toString()); sendCommentToActivity(); // sendCommentToActivity(); } }; /** * 点数を返す * チェックが付いたものを減点 * * @return */ public String getComment() { String comment = comment_edit_text.getText().toString(); return comment; } /** * 点数やコメントを数えるために追加 */ @Override public void onAttach(Context context) { super.onAttach(context); try { mListener = (OnGetFromUserClickListener) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement OnGetFromUserClickListener"); } } public void sendRatingToActivity(View v) { if (mListener != null) { int rate = getRate(); mListener.getRateFromFragment(rate, receiver_id, false); } } public void sendCommentToActivity() { if (mListener != null) { String comment = getComment(); mListener.getCommentFromFragment(comment, receiver_id); } } }
ポイント①はリスナーを作って、チェックボックスを押したり、コメント欄を編集した場合に、それを呼び出すことです。
ポイント②はaddTextChangedListener でTextWatcherを利用することです。
これがないと、クリックだけだと、テキストボックスを編集した後、すぐにActivityで送信ボタンを押されたりする場合、編集した後のテキストが取得できません。
//OnGetFromUserClickListener.java public interface OnGetFromUserClickListener { void getRateFromFragment(int rate, int receiver_id, boolean is_sender); void getCommentFromFragment(String comment, int receiver_id); }
//ReviewActivity.java public class ReviewActivity extends UtilsActivity implements PostJsonTask.PostJsonTaskCallback, OnGetFromUserClickListener { private static final String TAG = "ReviewActivity"; private Button accept; private CheckBox baggage_info; private CheckBox receipt_goods_wait_time; private CheckBox receipt_goods_place_info; private ProgressDialog m_ProgressDialog; private Context context = this; private Offer offer; private String mOfferId; private String mCompanyId; private int mSender_id; private ArrayList<Review> reviewArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_review); //中略 //Offer offerを取得している部分を略 makeView(offer); } public void makeView(Offer offer) { ArrayList<Delivery> deliveryList = offer.getDeliveryList(); FragmentManager fragMan = getFragmentManager(); FragmentTransaction fragTransaction = fragMan.beginTransaction(); reviewArrayList = new ArrayList<>(); mCompanyId = Integer.toString(offer.getCompany_id()); Log.d(TAG, "会社ID:" + mCompanyId); for (int count = 0; count < deliveryList.size(); count++) { Fragment review_fragment = new ReviewFragment(); Delivery delivery = deliveryList.get(count); Bundle bundle = new Bundle(); bundle.putParcelable("delivery", delivery); bundle.putInt("delivery_number", count); review_fragment.setArguments(bundle); fragTransaction.add(R.id.each_delivery, review_fragment, "review" + count); //レビューの初期化 ArrayList<Receiver> receiverArrayList = delivery.getReceiverList(); for (int count_receiver = 0; count_receiver < receiverArrayList.size(); count_receiver++) { Receiver receiver = receiverArrayList.get(count_receiver); ArrayList<Sender> senderList = delivery.getSenderList(); //発荷主は一つと仮定 Sender sender = senderList.get(0); mSender_id = sender.getCompany_id(); Review review = new Review(Integer.parseInt(mOfferId), receiver.getCompany_id(), mSender_id); // reviewArrayList.add(receiver.getCompany_id()); reviewArrayList.add(review); } } fragTransaction.commit(); } @Override public void postJsonCancel() { // プログレスダイアログを閉じる if (m_ProgressDialog != null || m_ProgressDialog.isShowing()) { m_ProgressDialog.dismiss(); } } @Override public void getRateFromFragment(int rate, int receiver_id, boolean is_sender) { Log.d(TAG, "点数:" + rate + " レシーバーID:" + receiver_id); reviewArrayList = ReviewUtils.changeRate(rate, receiver_id, reviewArrayList, is_sender); } @Override public void getCommentFromFragment(String comment, int receiver_id) { Log.d(TAG, "コメント:" + comment + " レシーバーID:" + receiver_id); reviewArrayList = ReviewUtils.changeComment(comment, receiver_id, reviewArrayList); } }