2015-03-03 98 views
0

所以我得到了2個片段和一個活動。這兩個片段通過一個接口相互通信。 問題是,我無法更改Activity中的第二個片段中的TextView,這些片段來自fragment1中的某些文本。無法實時更改片段的TextView

還有同樣的問題(How to change fragment's textView's text from activity),但沒有真正的解決方案,所以我想我應該打開它。

片段1:

public class test_layout extends Fragment { 

DearListener activityCommander; 
TextView custom_question_bar, list_number; 
String selectedCustomQuestion; 
boolean buttonClicked; 
Button yiyecekButton; 
Context c; 

public interface DearListener { 
    public void getMyText(String theQuestion); 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     activityCommander = (DearListener) activity; 
     Log.i("MYLOG", "onAttach"); 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString()); 
    } 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.new_test_layout, container, false); 
    //custom_question_bar= (TextView) v.findViewById(R.id.custom_question_bar); 
    //list_number= (TextView) v.findViewById(R.id.list_number); 
    yiyecekButton = (Button) v.findViewById(R.id.yiyecekButton); 
    Log.i("MYLOG", "Button referenced"); 
    yiyecekButton.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View v){ 
        Log.i("MYLOG", "onCLickListener Called"); 
        onClickYiyecek(v); 

       } 
    } 
    ); 
    return v; 
} 

public void onClickYiyecek(View v){ 
    Log.i("MYLOG", "OnClickYiyecek"); 
    activityCommander.getMyText("En sevdiğim yiyecek ne?"); 
} 

public void onClickQuestionReady(View v){ 
    if(custom_question_bar == null) 
    { 
     Toast.makeText(c, "Boşluğu doldurmadınız", Toast.LENGTH_SHORT).show(); 
    } 
    activityCommander.getMyText(custom_question_bar.getText().toString()); 
} 

}

,活性:

public class NewTest extends Activity implements test_layout.DearListener { 

FragmentManager manager; 
test_layout test_layout_frag; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_holder); 
    manager=getFragmentManager(); 
    test_layout_frag = new test_layout(); 
    FragmentTransaction transaction = manager.beginTransaction(); 
    transaction.add(R.id.the_frame_layout, test_layout_frag); 
    transaction.commit(); 
} 
@Override 
    public void getMyText(String theQuestion) { 
     manager=getFragmentManager(); 
     answer_layout answer_layout_frag = new answer_layout(); 
     answer_layout answer_layout_obj = (answer_layout)getFragmentManager().findFragmentById(R.id.fragment2); 
     answer_layout_obj.changeText(theQuestion); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.replace(R.id.the_frame_layout, answer_layout_frag); 
     transaction.commit(); 
    Log.i("MYLOG", "Transaction Called"); 
    //transaction.addToBackStack(null); 
} 

}

而第二個片段:

public class answer_layout extends Fragment { 
TextView yourSelectedQuestion; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.new_answer_layout, container, false); 
    yourSelectedQuestion= (TextView) v.findViewById(R.id.yourSelectedQuestion); 
    return v; 
} 
public void changeText(String a) { 
    Log.i("MYLOG", "changeText"); 
    Log.i("MYLOG", a); 
    yourSelectedQuestion.setText(a); 
} 

}

也不例外給定,但是在第二個片段TextView的沒有改變。

因爲我沒有在這裏寫xml,所以如果需要的話。 使用FrameLayout,它包含2個片段。

回答

0

myFragment類寫一個靜態方法:

public static myFragment newInstance(String txt) { 
     myFragment myfragment = new myFragment(); 

     Bundle args = new Bundle(); 
     args.putString("TEXT", txt); 
     myfragment.setArguments(args); 

     return myfragment; 
    } 

後,在myFragmentOnCreate方法inflatetextView tv

​​

在主FragmentActivity類做:

myFragment fragment = myFragment.newInstance(textToBeSent); 

replace之前,

fragmentTransaction.replace(R.id.fr_dynamic, fragment); 
fragmentTransaction.commit(); 
+0

現在的另一個項目工作,但感謝你的答案先生!我會看看它。 – MyHeartsECO 2015-03-25 18:02:40