0

2 つのフラグメント (両方とも動的に作成) を含むアクティビティがあり、そのうちの 1 つは ListFragment です。アクティビティに onListItemClick ハンドラを実装しました。アイテムがクリックされたら、両方のフラグメントを他のフラグメントに置き換えて、TextView に入力したいと考えています。ただし、フラグメントを置き換えた後、View オブジェクトを取得できないようです。新しい Details フラグメントで TextView を操作する必要があります。null が返されます。関連するコードを次に示します (onListItemSelected は、メイン アクティビティで onListItemClick を処理するハンドラーです)。

@Override
public void onListItemSelected(int index) {
    inflateCheckinFragment();
    FragmentManager fm = getFragmentManager();

    FragmentTransaction fragmentTransaction = fm.beginTransaction();
        cif = new checkInFragment();
        fragmentTransaction.replace(R.id.action_container, cif, "ACTIONS");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit()

    FragmentTransaction fragmentTransaction2 = fm.beginTransaction();
        gdf = new GeolocDetailsFragment();
        fragmentTransaction2.replace(R.id.fragment_container, gdf, "DETAILS");
        fragmentTransaction2.addToBackStack(null);
        fragmentTransaction2.commit();

    View gdfView = gdf.getView();

    TextView tv = (TextView)  gdfView.findViewById(R.id.textPOI);
    tv.setText(printPOI(poiList.get(index)));
}
4

2 に答える 2

1

onListItemSelected メソッドでデータを設定するだけになりました。selectedPOI はプライベート クラス メンバーです。

public void onListItemSelected(int index) {
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.action_container, cif, TAG_CHECKIN);
        fragmentTransaction.replace(R.id.fragment_container, gdf, TAG_DETAILS);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        selectedPOI = poiList.get(index);
    }

次に、GeolocDetailsFragment クラスで、Fragment の onCreateView メソッドの Activity で呼び出されるハンドラーをセットアップして、TextView 値を設定します。

public class GeolocDetailsFragment extends Fragment {
    private TextSetter textSetter;

    public interface TextSetter {
        public String getActivityText();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.geoloc_details_fragment, container, false);
        TextView detailsText = (TextView) view.findViewById(R.id.textPOI);

        detailsText.setText(textSetter.getActivityText());

        return view;
    }

    public void onAttach(Activity activity) {

        super.onAttach(activity);

        try {
            textSetter = (TextSetter) activity;
        }
        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnGetPOIListener");
        }
    }

}

最後に、メイン アクティビティに getActivityText() を実装して、TextView に渡す文字列を取得します。

    @Override
    public String getActivityText() {

        return printPOI(selectedPOI);
    }
于 2012-11-21T01:58:28.347 に答える
0

このような方法でフラグメントを使用する必要があります。フラグメントでパブリックを作成すると、フラグメントに直接アクセスするのにTextView役立ち、したがって値が変更されます。Activity

Class GeolocDetailsFragment extends Fragments{

public TextView tv;

onCreateView(){

    tv= (TextView)  rootView.findViewById(R.id.textPOI);

    }


}

//あなたの活動で

@Override
public void onListItemSelected(int index) {

      gdf = new GeolocDetailsFragment();
      gdf.tv.setText(printPOI(poiList.get(index)));

}
于 2012-11-20T12:18:38.887 に答える