0

あなたが元気であることを願っています&あなたの仕事おめでとうございます!シナリオ:Pagegauchefragment、PageMilieufragment、PageDroiteFragmentの3つのフラグメントファイルがあり、2つのファイルFragmentsSliderActivityとMyPagerAdapterによって管理されています。1つ目と2つ目は、ユーザーが入力したEditTextを含むデータフィールドです。最後の1つは、データユーザーファイルが保存されるListViewです。リストビュー(最後のフラグメント)から、ユーザーは3つの可能性でファイルをクリックします:1。ファイルのインポート/2。ファイルの削除/3。キャンセル(アクションバープロセスを使用してデータを保存します)。ここで問題:データを(setTextを使用して)更新するために他のフラグメントに渡すことができません。ログ:タグ:IIputConnectionWrapper /テキスト:非アクティブなInputConnectionではgetSelectedText、非アクティブなInputConnectionではsetComposedText。

  View W =inflater.inflate(R.layout.page_droite_layout,container, false);
     this.mListView = (ListView) W.findViewById(R.id.ListView01);
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, tabRevues);
     this.mListView.setAdapter(adapter);

     this.mListView.setOnItemClickListener(new OnItemClickListener() {           

        @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
             View WW =inflater.inflate(R.layout.page_gauche_layout,container, false);
             file_name = (EditText) WW.findViewById(R.id.label_LiquidBase);


             //int item = position;
             item_name = ((TextView)view).getText().toString();
             AlertDialog.Builder ad = new AlertDialog.Builder(getActivity());               

             ad.setTitle("Selection Recettes : " + item_name);
             ad.setMessage("Que souhaitez-vous faire ?");

             ad.setPositiveButton("Importer", 
                     new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int id) {                           
                            try {

ここで、テキストをsetTextに渡します(たとえば):

file_name.setText("blahblah");

私はVoidメソッドを使用していると思いますが、変数を渡す方法がわかりません...どんな助けでも大歓迎です!クリストファー

4

1 に答える 1

2

フラグメント間でデータを直接渡すことは決してお勧めしません。代わりに、Fragment XからFragmentsSliderActivityにデータを渡します。FragmentsSliderActivityは、このデータをFragment Yに渡します。これは、フラグメントクラスで定義されたインターフェイスを介して行い、onAttach()で定義されたコールバックをインスタンス化します。

これを行う方法の詳細については、 他のフラグメントとの通信をご覧ください。

簡単な例として、フラグメントAとフラグメントBについて考えてみます。フラグメントAはリストフラグメントであり、アイテムが選択されるたびに、フラグメントBに表示される内容が変更されます。

最初に、フラグメントAをそのように定義します。

 public class FragmentA extends ListFragment{

   //onCreateView blah blah blah

}

そしてこれがフラグメントBです

public class FragmentB extends Fragment{

 //onCreateView blah blah blah

}

そして、これが両方を管理する私のFragmentActivityです

public class MainActivity extends FragmentActivity{

//onCreate 
//set up your fragments

}

おそらくあなたはすでにこのようなものを持っているでしょう、今ここにあなたがFragmentA(私たちがいくつかのデータを取得する必要があるリストフラグメント)を変更する方法があります。

    public class FragmentA extends ListFragment implements onListItemSelectedListener, onItemClickListener{

OnListItemSelectedListener mListener;

   //onCreateView blah blah blah



 // Container Activity must implement this interface
    public interface OnListItemSelectedListener {
    public void onListItemSelected(int position);
}


}


  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mListener = (OnListItemSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnListItemSelectedListener");
    }
}


  @Override 
 public void onItemClick(AdapterView<?> parent, View view, int position, long id){


 //Here's where you would get the position of the item selected in the list, and  pass    that position(and whatever other data you need to) up to the activity 
//by way of the interface you defined in onAttach

  mListener.onListItemSelected(position);


}

ここで最も重要な考慮事項は、親アクティビティがこのインターフェイスを実装することです。そうしないと、例外が発生します。正常に実装されると、リストフラグメント内のアイテムが選択されるたびに、アクティビティにその位置が通知されます。明らかに、任意の数またはタイプのパラメーターを使用してインターフェースを変更できます。この例では、整数位置を渡すだけです。これが少しの人、幸運を明らかにすることを願っています。

于 2013-02-06T23:13:32.077 に答える