我正在编写一个代码,其中包含使用片段要问的一系列问题。每次用户单击按钮(顶部片段)时,都应显示一个新问题(底部片段)。不幸的是,我没有从MainActivity(使用顶部片段中的onclicklistener)向问题显示片段发送问题的运气。我在这里做错了吗?
谢谢 主要活动
public class MainActivity extends AppCompatActivity implements widgetFragment.OnMessageRecieveistener, widgetFragment.OnRatingRecieveListener{
String[] list = {"Most lovely bark?", "Prettiest eyes?", "Loves daddy most?","Best behaved?"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////TOP FRAGMENT/////
if(findViewById(R.id.widgetFragment)!=null){
if(savedInstanceState!=null){
return;
}
widgetFragment widgetFragment = new widgetFragment();
getSupportFragmentManager().beginTransaction().add(R.id.widgetFragment, widgetFragment, null).commit();
}
/////BOTTOM FRAGMENT/////
if(findViewById(R.id.question_fragment)!=null){
if(savedInstanceState!=null){
return;
}
questionFlash questionFlash = new questionFlash();
getSupportFragmentManager().beginTransaction().add(R.id.question_fragment, questionFlash, null).commit();
}
}
@Override //THIS CHANGES THE QUESTION
public void onMessageRecieve(float i) {
boolean proceed= true;
Toast.makeText(this, "Click Rating!", Toast.LENGTH_SHORT).show();
questionFlash questionFlash = new questionFlash();
Bundle bundle = new Bundle();
for (int j = 0; j < listQuestion.length; j++) {
if(proceed==true) {
String question = listQuestions[j];//LIST OF QUESTIONS
bundle.putString("key", question);
questionFlash.setArguments(bundle);
proceed=false;
}
}
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction().add(R.id.question_fragment, questionFlash, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
问题类//单击按钮时不接收或更改问题
public class questionFlash extends Fragment {
Bundle bundle = new Bundle();
TextView question;
public questionFlash(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = (View) inflater.inflate(R.layout.question_flash, container, false);
question = view.findViewById(R.id.questions);
if(bundle==null)
{
if(savedInstanceState!=null){
Toast.makeText(getContext(), "ERROR AGAIN", Toast.LENGTH_SHORT).show();
}
String name = bundle.getString("key");
question.setText(name);
}
return view;
}