0

I have TextView in the list which I want to print. With the help of Button

I define like this

private TextView Question,optionA,optionB,optionC,optionD;

viewAnsB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        setContentView(R.layout.view_answer);
        Question= (TextView)findViewById(R.id.Question);
        optionA=findViewById(R.id.AOption);
        optionB=findViewById(R.id.BOption);
        optionC=findViewById(R.id.COption);
        optionD=findViewById(R.id.DOption);
   for (int i=0 ; i < DbQuery.g_quesList.size(); i++) {
    
  Question.setText(DbQuery.g_quesList.get(i).getQuestion());
   optionA.setText(DbQuery.g_quesList.get(i).getOptionA());
   optionB.setText(DbQuery.g_quesList.get(i).getOptionB());
  optionC.setText(DbQuery.g_quesList.get(i).getOptionC());
   optionD.setText(DbQuery.g_quesList.get(i).getOptionD());
      }
    }

  }

It print only the last value DbQuery.g_quesList i want to print all the value of DbQuery.g_quesListin my layout file my layout XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 android:layout_margin="10dp"
    android:orientation="vertical">
  
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:id="@+id/Question"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="18sp"


        android:text="question1"
        android:textStyle="normal"
        android:scrollbars="vertical"

        android:inputType="textMultiLine|textNoSuggestions"
        android:elevation="5dp"
        android:translationZ="3dp"
        tools:ignore="TextViewEdits"/>
   
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/AOption"
        android:text="@string/opettion1"
        android:textSize="14sp"
        android:gravity="center"

        android:textStyle="bold"
        android:textColor="@color/black">

    </TextView>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/BOption"
        android:text="@string/opettion1"
        android:textSize="14sp"
        android:gravity="center"

        android:textStyle="bold"
        android:textColor="@color/black">

    </TextView>
   

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/COption"
        android:text="@string/opettion1"
        android:textSize="14sp"
        android:gravity="center"

        android:textStyle="bold"
        android:textColor="@color/black">

    </TextView>
 

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/DOption"
        android:text="@string/opettion1"
        android:textSize="14sp"
        android:gravity="center"

        android:textStyle="bold"
        android:textColor="@color/black">

    </TextView>
   

</LinearLayout>
  

If I am doing wrong then suggest me beast way for this to print all the value of list

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51

2 Answers2

2

This is because, you are iterating your all questions and answers using that for loop. If i = 0, it prints the questions and answers to the respective textviews. And next i will be 1, then it replaces all textviews with next question and its options. and so on. finally, the for loop reaches to the last question, and displays the questions and options.

So, you shouldn't be using for loop in that place.

Paru
  • 21
  • 2
  • What I can do to print the value – Sarthak Tiwari May 30 '21 at 16:30
  • 1
    I am not sure, what you actually want to achieve. Suppose if you have 10 questions and each has 4 options. You have one screen, where you have 5 textviews, 1 for question and 4 others for answers. So, if you want to display all questions in those 5 textviews at the same time, it is not possible. You have to display them one by one... or you have to make many textviews to display all questions and answers. If you have only 5 textviews, then you have to display one question and its options and after clicking a button or choosing an asnwer, you can use same textviews to display next question – Paru May 30 '21 at 16:52
  • I want to print all the questions and options. here user see which question he attempt, the correct answer, the wrong answer given by the user – Sarthak Tiwari May 30 '21 at 17:17
1

Well in your layout.xml file, you only have one TextView component for the question and each option, which means you can only display one question at a time.

So inside your for loop, since there is only one set of TextViews for a single question, each time the loop runs it will overwrite whatever contents were previously there, which is why you only see the last question.

What you need is a way to have multiple copies of your layout.xml displayed on your screen, each with a different value for the question and options.

I suggest you look into Fragments in Android as they are well suited to this case.

The other option is to use a ListView in Android with a custom layout for each entry in the list - This custom layout would be similar to what you currently have in layout.xml

Take a look here for instructions on the ListView method.

Dakshin
  • 437
  • 1
  • 6
  • 16