0

I want to use fragment in my activity so that on button press it will update the textView. I am storing that data in ArrayList.I want to show that data one by one in text view on button press. If ArrayList contains five data then I want to update the text in text view five times using fragment

      @Override
    protected void onPostExecute(Void args) {
        //aero();

        TextView txtview = (TextView) findViewById(R.id.question);         

        questionList = new ArrayList<String>();     



                if (!questionList.contains(mcq.get(currentPosition).getPs())
                        ) {
                    questionList.add(mcq.get(currentPosition).getPs());
                    txtview.setText(mcq.get(currentPosition).getPs().toString());

                    Button btnnext = (Button) findViewById(R.id.next);
                    btnnext.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            currentPosition = currentPosition + 1;
                            if (currentPosition < mcq.size()) {
                                txtView.setText(mcq.get(currentPosition).getPs());


                            }
                        }
                    });

Right now only first data is displayed in the text view.

Agniveer Kranti
  • 117
  • 2
  • 12

1 Answers1

2

just maintain the current position.

private currentPosition=0;
ArrayList<Quiz_MCQ>  mcq = new ArrayList<Quiz_MCQ>();

//initially textview will be set to 0th index's data
textView.setText(mcq.get(currentPosition).getPs);

Button btnnext = (Button) findViewById(R.id.next);
          btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
currentPosition = currentPosition + 1;
if (currentPosition < mcq.size()) {
textView.setText(mcq.get(currentPosition).getPs);
}
            }
        });
sanedroid
  • 1,036
  • 4
  • 16
  • 27
  • Do I have to use fragments as right now getting NullPointer error@Pooja Gaikward – Agniveer Kranti Apr 27 '16 at 11:17
  • java.lang.NullPointerException at com.example.Main_Activity_For_Quiz$DownloadJSON$2.onClick(Main_Activity_For_Quiz.java:172) at android.view.View.performClick(View.java:4654) at android.view.View$PerformClick.run(View.java:19438) – Agniveer Kranti Apr 27 '16 at 11:36
  • Can you explain why you are using for loop in the code? all your views will be set with the last index's data from the list.. – sanedroid Apr 27 '16 at 11:39
  • Please Update the question with your entire stackTrace for better understanding.. – sanedroid Apr 27 '16 at 11:43
  • You also dont require this for loop --> for (Quiz_MCQ bean : mcq).. instead of using bean.getPs use "mcq.get(currentPosition).getPs()" or mcq.get(currentPosition).getOp1() and so on where ever needed – sanedroid Apr 27 '16 at 11:48
  • I am fetching some JSON data and storing them in arraylist.Let 's say I have only one ArrayList which has some data in it.I don't want to display all data at a time.I want to display data one by one .So on button press, I want to move forward and update my textview which is showing the data at position 0.So on one click it will show the data at position 1 and so on till last data in arraylist. – Agniveer Kranti Apr 27 '16 at 11:49
  • yes and that arraylist is this one --> ArrayList mcq = new ArrayList(); ? right? – sanedroid Apr 27 '16 at 11:50
  • No that is questionList which is present inside that list – Agniveer Kranti Apr 27 '16 at 11:54
  • okay, got it.. you are populating questionlist and other such list from this Quiz_MCQ list? right? – sanedroid Apr 27 '16 at 11:55
  • yes, but this code is working..Thanks above code in question)now and I was thinking of using fragments...can you explain your answer .How to modify it to work smoothly. – Agniveer Kranti Apr 27 '16 at 11:58
  • Why do you wish to use fragments? http://developer.android.com/guide/components/fragments.html you will get a good understanding after reading this. – sanedroid Apr 27 '16 at 12:01
  • No, I thought that my textview is getting updated so I have to use fragment inside the activity. – Agniveer Kranti Apr 27 '16 at 12:02
  • That is not required. you can process this in both activity or fragment. Try searching more of stackoverflow or Google to understand the significance of fragments. – sanedroid Apr 27 '16 at 12:08