0

im still new in android developement. I want to refresh my fragment and getting new imageButton background in that fragment but i keep failing to do that.

This is my main activity

public class MainActivity extends AppCompatActivity {

    public int QuestionNum =0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MainActivityFragment MainFrag = new MainActivityFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.content, MainFrag).commit();

    }

    public void Answer1(View view){

        QuestionNum++;
        Stage StageFrag = new Stage();
        getSupportFragmentManager().beginTransaction()
        .replace(R.id.content,StageFrag).commit();
    }

and this is my fragment

public class Stage extends Fragment {

    public Stage() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_stage, container, false);

        MainActivity Main = new MainActivity();
        String QuestionInd[] = getResources().getStringArray(R.array.Qind);
        Integer AnswerAnimal[]={R.drawable.anjing,R.drawable.kucing....};

        int QNum = Main.QuestionNum;

        TextView Question = (TextView) view.findViewById(R.id.Questions);
        Question.setText(QuestionInd[Main.QuestionNum]);

        ImageButton Ans1 = (ImageButton) view.findViewById(R.id.Choice1);
        ImageButton Ans2 = (ImageButton) view.findViewById(R.id.Choice2);
        ImageButton Ans3 = (ImageButton) view.findViewById(R.id.Choice3);
        ImageButton Ans4 = (ImageButton) view.findViewById(R.id.Choice4);


        switch(QNum) {
            case 0:
                Ans1.setBackgroundResource(AnswerAnimal[0]);
                Ans2.setBackgroundResource(AnswerAnimal[1]);
                Ans3.setBackgroundResource(AnswerAnimal[2]);
                Ans4.setBackgroundResource(AnswerAnimal[3]);
                break;
            case 1:
                Ans1.setBackgroundResource(AnswerAnimal[8]);
                Ans2.setBackgroundResource(AnswerAnimal[9]);
                Ans3.setBackgroundResource(AnswerAnimal[5]);
                Ans4.setBackgroundResource(AnswerAnimal[4]);
                break;
        }

        return  view;
    }
}

i didnt get error in my logcat. I also try using add but the background didn't change only the fragments keep stacking. Using the attach and retach didnt work too.

Please help me and explain my mistake. i really want to learn about this. Thank you.

Bhargav
  • 8,118
  • 6
  • 40
  • 63
Rafie Gilang
  • 31
  • 1
  • 9

1 Answers1

0

First of all, never do this:

MainActivity Main = new MainActivity();

explained here.

Second: Even if you considered an Android Activity as just a normal Java Class, then new MainActivity() means you are creating new instance of that class, so the instance variables will be what or how you defined them i.e public int QuestionNum =0 will remain the same. Hence the switch statement never comes to case: 1.

What you can do is just call some method like changeImageButtons() on the StageFrag Object you have in the Activity.

Community
  • 1
  • 1
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Thx Archie, i will follow your suggestion. Still trying to find a way to get the changeImageButtons() work. – Rafie Gilang Oct 20 '15 at 10:50
  • `changeImageButtons()` will have the `case 1:` code. But don't call that method soon after the fragment transaction, because it might not have created the UI yet. Just looking at your above code, you don't need anything to change the ImageButtons why don't you just initialize the UI with case 1 resources? – Archie.bpgc Oct 20 '15 at 10:53
  • i want to have 10 different case with different variety of image background when the user click the answer buttons. I want to learn using 1 fragment for multiple times.But im not quite sure how to do that. I'll try my best in solving this. thanks for the help ^^. – Rafie Gilang Oct 20 '15 at 11:12