有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java For循环只打印最后一个值

我要打印的列表中有TextView。在巴顿的帮助下

我这样定义

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());
      }
    }

  }

它只打印最后一个值DbQuery。我想打印DbQuery的所有值。g_quesListin my layout file my layout XML file

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


        安卓:text="question1"
        安卓:textStyle="normal"
        安卓:scrollbars="vertical"

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

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

        安卓:textStyle="bold"
        安卓:textColor="@color/black">

    </TextView>

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

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

        安卓:textStyle="bold"
        安卓:textColor="@color/black">

    </TextView>
   

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

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

        安卓:textStyle="bold"
        安卓:textColor="@color/black">

    </TextView>
 

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

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

        安卓:textStyle="bold"
        安卓:textColor="@color/black">

    </TextView>
   

</LinearLayout>
  

如果我做错了,那么建议我用beast的方式打印列表的所有值


共 (1) 个答案

  1. # 1 楼答案

    这是因为,您正在使用for循环迭代所有问题和答案。 如果i = 0,它会打印相应文本视图的问题和答案。 下一个i将是1,然后用下一个问题及其选项替换所有文本视图。 等等 最后,for循环到达最后一个问题,并显示问题和选项

    所以,你不应该在那个地方使用for循环