无法实时更改片段的TextView

问题描述:

所以我得到了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个片段。

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