あるフラグメントからデータを投稿し、ビューを追加して他のフラグメントを更新しています。私はリストビューを使用していません。LinearLayout を使用しています。
私は非常に単純な疑問を持っています。ビューを取得し、別のビューを追加して更新するにはどうすればよいですか?
基本的に、すでにデータを持っているlinearlayoutを膨らませ、フラグメントを追加するメインアクティビティにビューを追加したいと考えています。
編集:
LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
ViewGroup view = (ViewGroup) ll.getParent();
View customView = view;
TextView commentContext = (TextView) customView.findViewById(R.id.commentText);
commentContext.setText("hello");
view.addView(customView);
メイン アクティビティでそれを実行しようとしましたが、ビューで null ポインター例外が発生しました。
編集2:
FragmentManager fm = getSupportFragmentManager();
// You can find Fragments just like you would with a View by using FragmentManager.
android.support.v4.app.Fragment fragment = fm.findFragmentById(R.id.fragmentCommentContent);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.comments_list_item, null);
LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
TextView newText = (TextView) child.findViewById(R.id.commentText);
newText.setText("Important text");
ll.addView(newText);
私はそれを試して得ました:指定された子にはすでに親があります。最初に子の親で removeView() を呼び出します。問題は、ビューの削除を呼び出すと、レイアウトに挿入した以前のデータが失われることです。