计算每个RecyclerView项目的整数值,并在活动结束时将所有值相加。

huangapple go评论70阅读模式
英文:

Calculate int values of each recyclerview item and sum all together when activity finish

问题

以下是您提供的代码的翻译部分:

public class Rvadapter extends RecyclerView.Adapter<Rvadapter.MyViewHolder> {

    // ...(其他代码部分略)

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        // ...(其他代码部分略)

        holder.bta1true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer1 == null) {
                    answer1 = "T";
                    // ...(其他代码部分略)
                } else if (answer1.equals("F")) {
                    answer1 = "T";
                    // ...(其他代码部分略)
                } else {
                    answer1 = null;
                    // ...(其他代码部分略)
                }
            }
        });

        holder.bta1false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer1 == null) {
                    answer1 = "F";
                    // ...(其他代码部分略)
                } else if (answer1.equals("T")) {
                    answer1 = "F";
                    // ...(其他代码部分略)
                } else {
                    answer1 = null;
                    // ...(其他代码部分略)
                }
            }
        });

    }

    private void checkmarks() {
        // ...(其他代码部分略)

        if (marks < 0) {
            marks = 0;
        }

    }

    // ...(其他代码部分略)

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvquestiontitle, tvquestion1, tvquestion2, tvquestion3, tvquestion4, tvquestion5, tvquestionid, tvmarks;
        Button bta1true, bta1false, bta2true, bta2false, bta3true, bta3false, bta4true, bta4false, bta5true, bta5false;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            // ...(其他代码部分略)
        }
    }
}

请注意,由于您要求仅返回翻译部分,因此我已将代码中的其他部分省略。如果您有任何关于这些代码部分的问题或需要更多帮助,请随时提问。

英文:

I am making a multiple choice app in which the questions are displayed in recyclerview with arraylist. And also the buttons to choose your answer. And get Right answers from sqlite and do if condition with the user input answers to calculate marks in each question. like this....

计算每个RecyclerView项目的整数值,并在活动结束时将所有值相加。

My problem is i have no idea how to calculate marks with the user input (clicked buttons bta1 true, bta1 false, etc....) when clicked finish button (that is in main activity).
i want to calculate marks in each question, total 5marks for each question. If i clicked finish button and if there is 10 questions and all user input answers are right, the total marks should be 50marks.

This is my recyclerview adapter class

public class Rvadapter extends RecyclerView.Adapter &lt;Rvadapter.MyViewHolder&gt; {
private Context context;
Activity activity;
MyDatabaseHelper myDB;
String questiontitle, question1, question2, question3, question4, question5, answer1, answer2, answer3, answer4, answer5;
String answerdb1, answerdb2, answerdb3, answerdb4, answerdb5;
Cursor cursor;
ArrayList&lt;ItemList&gt; arlist;
int marks, totalmark;
private ArrayList arquestiontitle, arquestion1, arquestion2, arquestion3, arquestion4, arquestion5, aranswer1, aranswer2, aranswer3, aranswer4, aranswer5, armarks;
String idtosee, orderid, orderdate, customername, customeraddress, customerphone, delifee, advance, tax, discount, itemname, itemcount, itemprice, mark;
int a;
Rvadapter(Activity activity, Context context, ArrayList arquestiontitle, ArrayList arquestion1, ArrayList arquestion2, ArrayList arquestion3, ArrayList arquestion4, ArrayList arquestion5, ArrayList aranswer1, ArrayList aranswer2, ArrayList aranswer3, ArrayList aranswer4, ArrayList aranswer5, ArrayList armarks){
this.activity = activity;
this.context = context;
this.arquestiontitle = arquestiontitle;
this.arquestion1 = arquestion1;
this.arquestion2 = arquestion2;
this.arquestion3 = arquestion3;
this.arquestion4 = arquestion4;
this.arquestion5 = arquestion5;
this.aranswer1 = aranswer1;
this.aranswer2 = aranswer2;
this.aranswer3 = aranswer3;
this.aranswer4 = aranswer4;
this.aranswer5 = aranswer5;
this.armarks = armarks;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view =   inflater.inflate(R.layout.cardlayoutquestion, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.tvquestiontitle.setText(String.valueOf(arquestiontitle.get(position)));
holder.tvquestion1.setText(String.valueOf(arquestion1.get(position)));
holder.tvquestion2.setText(String.valueOf(arquestion2.get(position)));
holder.tvquestion3.setText(String.valueOf(arquestion3.get(position)));
holder.tvquestion4.setText(String.valueOf(arquestion4.get(position)));
holder.tvquestion5.setText(String.valueOf(arquestion5.get(position)));
holder.tvquestionid.setText(String.valueOf(position+1+&quot;. &quot;));
marks = Integer.parseInt(String.valueOf(armarks.get(position)));
answerdb1 = String.valueOf(aranswer1.get(position));
answerdb2 = String.valueOf(aranswer2.get(position));
answerdb3 = String.valueOf(aranswer3.get(position));
answerdb4 = String.valueOf(aranswer4.get(position));
answerdb5 = String.valueOf(aranswer5.get(position));
holder.bta1true.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1 == null){
answer1 = &quot;T&quot;;
holder.bta1true.setBackgroundResource(R.drawable.selected120);
holder.bta1false.setBackgroundResource(R.drawable.unselected120);
}else if ( answer1.equals(&quot;F&quot;)){
answer1 = &quot;T&quot;;
holder.bta1true.setBackgroundResource(R.drawable.selected120);
holder.bta1false.setBackgroundResource(R.drawable.unselected120);
}else {
answer1 = null;
holder.bta1true.setBackgroundResource(R.drawable.unselected120);
}
}
});
holder.bta1false.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(answer1 == null){
answer1 = &quot;F&quot;;
holder.bta1false.setBackgroundResource(R.drawable.selected120);
holder.bta1true.setBackgroundResource(R.drawable.unselected120);
}else if ( answer1.equals(&quot;T&quot;)){
answer1 = &quot;F&quot;;
holder.bta1true.setBackgroundResource(R.drawable.unselected120);
holder.bta1false.setBackgroundResource(R.drawable.selected120);
}else {
answer1 = null;
holder.bta1false.setBackgroundResource(R.drawable.unselected120);
}
}
});
}
private void checkmarks() {
if(answer1!=null){
if(answer1.equals(answerdb1)){
marks++;
}else {
marks--;
}
}
if(answer2!=null){
if(answer2.equals(answerdb2)){
marks++;
}else{
marks--;
}
}
if(answer3!=null){
if(answer3.equals(answerdb3)){
marks++;
}else{
marks--;
}
}
if(answer4!=null){
if(answer4.equals(answerdb4)){
marks++;
}else{
marks--;
}
}
if(answer5!=null){
if(answer5.equals(answerdb5)){
marks++;
}else{
marks--;
}
}
if(marks&lt;0){
marks=0;
}
}
@Override
public int getItemCount() {
return arquestiontitle.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvquestiontitle, tvquestion1, tvquestion2, tvquestion3, tvquestion4, tvquestion5, tvquestionid, tvmarks;
Button bta1true, bta1false, bta2true, bta2false, bta3true, bta3false, bta4true, bta4false, bta5true, bta5false;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvquestiontitle = itemView.findViewById(R.id.tvquestiontitle);
tvquestion1 = itemView.findViewById(R.id.tvquestion1);
tvquestion2 = itemView.findViewById(R.id.tvquestion2);
tvquestion3 = itemView.findViewById(R.id.tvquestion3);
tvquestion4 = itemView.findViewById(R.id.tvquestion4);
tvquestion5 = itemView.findViewById(R.id.tvquestion5);
tvquestionid = itemView.findViewById(R.id.tvquestionid);
tvmarks = itemView.findViewById(R.id.tvmarks);
bta1true = itemView.findViewById(R.id.bta1true);
bta1false = itemView.findViewById(R.id.bta1false);
bta2true = itemView.findViewById(R.id.bta2true);
bta2false = itemView.findViewById(R.id.bta2false);
bta3true = itemView.findViewById(R.id.bta3true);
bta3false = itemView.findViewById(R.id.bta3false);
bta4true = itemView.findViewById(R.id.bta4true);
bta4false = itemView.findViewById(R.id.bta4false);
bta5true = itemView.findViewById(R.id.bta5true);
bta5false = itemView.findViewById(R.id.bta5false);
}
}
}

Please help me... how to calculate marks of each question (recycler cardview items) and Sum together all of those.. I can manage to get them with only one question at a time with next button, but this time i want to show all questions within the recyclerview..

答案1

得分: 0

<h2>使用复选框替代</h2>

首先,如果用户输入(在这种情况下是答案)是布尔类型,那么你应该使用[复选框][1]而不是按钮(更容易处理)。<br/>
布局将类似于这样:<br/>
<img src="https://i.stack.imgur.com/fgeqm.png" width="400" /><br/>
如果你愿意,仍然可以使用按钮<br/><br/>
<h2>创建一个类来存储答案</h2>
然后创建一个类来存储答案(我们称之为`Answer`)<br/>

    public class Answer {
    
        private int questionNumber;//问题的编号
        private Boolean bta1, bta2, bta3, bta4, bta5;//答案
        public Answer() {
        }
    
        public Answer(int questionNumber, Boolean bta1, Boolean bta2, Boolean bta3, Boolean bta4, Boolean bta5) {
            this.questionNumber = questionNumber;
            this.bta1 = bta1;
            this.bta2 = bta2;
            this.bta3 = bta3;
            this.bta4 = bta4;
            this.bta5 = bta5;
        }
    
        public int getQuestionNumber() {
            return questionNumber;
        }
    
        public void setQuestionNumber(int questionNumber) {
            this.questionNumber = questionNumber;
        }
    
        public Boolean getBta1() {
            return bta1;
        }
    
        public void setBta1(Boolean bta1) {
            this.bta1 = bta1;
        }
    
        public Boolean getBta2() {
            return bta2;
        }
    
        public void setBta2(Boolean bta2) {
            this.bta2 = bta2;
        }
    
        public Boolean getBta3() {
            return bta3;
        }
    
        public void setBta3(Boolean bta3) {
            this.bta3 = bta3;
        }
    
        public Boolean getBta4() {
            return bta4;
        }
    
        public void setBta4(Boolean bta4) {
            this.bta4 = bta4;
        }
    
        public Boolean getBta5() {
            return bta5;
        }
    
        public void setBta5(Boolean bta5) {
            this.bta5 = bta5;
        }
    
    }

<h2>为每个问题创建一个对象</h2>
并在MainActivity中为每个问题创建一个对象<br/>

    public class MainActivity extends AppCompatActivity {
    
    
        ArrayList<Answer> answers = new ArrayList<>();
    	
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    		initializeAnswers();
    		.
    		.
    		.
        }
    	
    	// 在这里,我们为每个问题创建一个对象,并将其默认值全部设置为false
        public void initializeAnswers(){
            for (int i=1;i<=20;i++){//用实际问题数替换20
                answers.add(new Answer(i,false,false,false,false,false));
            }
        }
    	// 这是当用户按下复选框(更改其值)时将被调用的方法
    	public void setQuestionResult(int questionNumber, int optionNumber, boolean value){
            switch (optionNumber){
                case 1:
                    answers.get(questionNumber+1).setBta1(value);
                    break;
                case 2:
                    answers.get(questionNumber+1).setBta2(value);
                    break;
                case 3:
                    answers.get(questionNumber+1).setBta3(value);
                    break;
                case 4:
                    answers.get(questionNumber+1).setBta4(value);
                    break;
                case 5:
                    answers.get(questionNumber+1).setBta5(value);
                    break;
            }
        }
    	
    }

<h2>捕获用户输入</h2>
之后,我们将为回收视图项中的每个按钮(或复选框)设置点击监听器,以便当用户选择答案时,我们将其存储在之前创建的对象中。
我们将在`ViewHolder`中执行此操作<br/>

    class MyViewHolder extends RecyclerView.ViewHolder {
        int questionNumber;
        TextView tvquestiontitle, tvquestion1, tvquestion2, tvquestion3, tvquestion4, tvquestion5, tvquestionid, tvmarks;
        CheckBox bta1, bta2, bta3, bta4, bta5;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvquestiontitle = itemView.findViewById(R.id.tvquestiontitle);
            tvquestion1 = itemView.findViewById(R.id.tvquestion1);
            tvquestion2 = itemView.findViewById(R.id.tvquestion2);
            tvquestion3 = itemView.findViewById(R.id.tvquestion3);
            tvquestion4 = itemView.findViewById(R.id.tvquestion4);
            tvquestion5 = itemView.findViewById(R.id.tvquestion5);
            tvquestionid = itemView.findViewById(R.id.tvquestionid);
            tvmarks = itemView.findViewById(R.id.tvmarks);
            bta1 = itemView.findViewById(R.id.bta1);
            bta2 = itemView.findViewById(R.id.bta2);
            bta3 = itemView.findViewById(R.id.bta3);
            bta4 = itemView.findViewById(R.id.bta4);
            bta5 = itemView.findViewById(R.id.bta5);
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 查看当前视图是否被选中?
                    boolean checked = ((CheckBox) v).isChecked();
                    int optionNumber;// 例如,"1代表A","2代表B",等等...
                    // 从视图ID获取选项编号
                    switch (v.getId()) {
                        case R.id.bta1:
                            optionNumber = 1;
                            break;
                        case R.id.bta2:
                            optionNumber = 2;
                            break;
                        case R.id.bta3:
                            optionNumber = 3;
                            break;
                        case R.id.bta4:
                            optionNumber = 4;
                            break;
                        case R.id.bta5:
                            optionNumber = 5;
                            break;
                        default:
                            optionNumber = 0;
                    }
                    // 在这里,我们将更改传递给Activity中的setQuestionResult以进行处理
                    ((MainActivity)context).setQuestionResult(questionNumber,optionNumber,checked);
                }
            };
            bta1.setOnClickListener(onClickListener);
            bta2.setOnClickListener(onClickListener);
            bta3.setOnClickListener(onClickListener);
            bta4.setOnClickListener(onClickListener);
            bta5.setOnClickListener(onClickListener);
        }
    }
您可能注意到我在顶部添加了一个参数(`int questionNumber;`),这将用于标识我们正在处理的问题。<br/>


<details>
<summary>英文:</summary>

&lt;h2&gt;Use checkboxes instead&lt;/h2&gt;

First of all. If the user inputs (answers in this case) are Boolean type then you should use [checkboxes][1] instead of buttons (much easier to deal with).&lt;br/&gt;
The layout will looks something like that:&lt;br/&gt;
&lt;img src=&quot;https://i.stack.imgur.com/fgeqm.png&quot; width=&quot;400&quot; /&gt;&lt;br/&gt;
You can stick with buttons if you want&lt;br/&gt;&lt;br /&gt;
&lt;h2&gt;Create a class to store answers&lt;/h2&gt;
Then make a class to store answers in it (let call it `Answer`)&lt;br /&gt;

    public class Answer {
    
        private int questionNumber;//number of the question
        private Boolean bta1, bta2, bta3, bta4, bta5;//answers
        public Answer() {
        }
    
        public Answer(int questionNumber, Boolean bta1, Boolean bta2, Boolean bta3, Boolean bta4, Boolean bta5) {
            this.questionNumber = questionNumber;
            this.bta1 = bta1;
            this.bta2 = bta2;
            this.bta3 = bta3;
            this.bta4 = bta4;
            this.bta5 = bta5;
        }
    
        public int getQuestionNumber() {
            return questionNumber;
        }
    
        public void setQuestionNumber(int questionNumber) {
            this.questionNumber = questionNumber;
        }
    
        public Boolean getBta1() {
            return bta1;
        }
    
        public void setBta1(Boolean bta1) {
            this.bta1 = bta1;
        }
    
        public Boolean getBta2() {
            return bta2;
        }
    
        public void setBta2(Boolean bta2) {
            this.bta2 = bta2;
        }
    
        public Boolean getBta3() {
            return bta3;
        }
    
        public void setBta3(Boolean bta3) {
            this.bta3 = bta3;
        }
    
        public Boolean getBta4() {
            return bta4;
        }
    
        public void setBta4(Boolean bta4) {
            this.bta4 = bta4;
        }
    
        public Boolean getBta5() {
            return bta5;
        }
    
        public void setBta5(Boolean bta5) {
            this.bta5 = bta5;
        }
    
    }

&lt;h2&gt;Create an object for each question&lt;/h2&gt;
 and create object for each question in the MainActivity&lt;br/&gt;

    public class MainActivity extends AppCompatActivity {
    
    
        ArrayList&lt;Answer&gt; answers = new ArrayList&lt;&gt;();
    	
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    		initializeAnswers();
    		.
    		.
    		.
        }
    	
    	// here we create object for each question and make it default value all false
        public void initializeAnswers(){
            for (int i=1;i&lt;=20;i++){//replace 20 with how many questions you have
                answers.add(new Answer(i,false,false,false,false,false));
            }
        }
    	// this is the methode that will be called when the user press a checkbox(change its value)
    	public void setQuestionResult(int questionNumber, int optionNumber, boolean value){
            switch (optionNumber){
                case 1:
                    answers.get(questionNumber+1).setBta1(value);
                    break;
                case 2:
                    answers.get(questionNumber+1).setBta2(value);
                    break;
                case 3:
                    answers.get(questionNumber+1).setBta3(value);
                    break;
                case 4:
                    answers.get(questionNumber+1).setBta4(value);
                    break;
                case 5:
                    answers.get(questionNumber+1).setBta5(value);
                    break;
            }
        }
    	
    }

&lt;h2&gt;Catch user inputs&lt;/h2&gt;
After that we gonna set click listener to each button(or checkbox) in the recyclerview items, so when the user chose an answer we store it in the objects we made before.
We&#39;ll do that in the `ViewHolder`&lt;br/&gt;

    class MyViewHolder extends RecyclerView.ViewHolder {
        int questionNumber;
        TextView tvquestiontitle, tvquestion1, tvquestion2, tvquestion3, tvquestion4, tvquestion5, tvquestionid, tvmarks;
        CheckBox bta1, bta2, bta3, bta4, bta5;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvquestiontitle = itemView.findViewById(R.id.tvquestiontitle);
            tvquestion1 = itemView.findViewById(R.id.tvquestion1);
            tvquestion2 = itemView.findViewById(R.id.tvquestion2);
            tvquestion3 = itemView.findViewById(R.id.tvquestion3);
            tvquestion4 = itemView.findViewById(R.id.tvquestion4);
            tvquestion5 = itemView.findViewById(R.id.tvquestion5);
            tvquestionid = itemView.findViewById(R.id.tvquestionid);
            tvmarks = itemView.findViewById(R.id.tvmarks);
            bta1 = itemView.findViewById(R.id.bta1);
            bta2 = itemView.findViewById(R.id.bta2);
            bta3 = itemView.findViewById(R.id.bta3);
            bta4 = itemView.findViewById(R.id.bta4);
            bta5 = itemView.findViewById(R.id.bta5);
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Is the view now checked?
                    boolean checked = ((CheckBox) v).isChecked();
                    int optionNumber;// e.g &quot;1 is A&quot; &quot;2 is B&quot; etc..
                    //get option number form the view id
                    switch (v.getId()) {
                        case R.id.bta1:
                            optionNumber = 1;
                            break;
                        case R.id.bta2:
                            optionNumber = 2;
                            break;
                        case R.id.bta3:
                            optionNumber = 3;
                            break;
                        case R.id.bta4:
                            optionNumber = 4;
                            break;
                        case R.id.bta5:
                            optionNumber = 5;
                            break;
                        default:
                            optionNumber = 0;
                    }
                    // here we will pass the changes to setQuestionResult to the Activity to process it
                    ((MainActivity)context).setQuestionResult(questionNumber,optionNumber,checked);
                }
            };
            bta1.setOnClickListener(onClickListener);
            bta2.setOnClickListener(onClickListener);
            bta3.setOnClickListener(onClickListener);
            bta4.setOnClickListener(onClickListener);
            bta5.setOnClickListener(onClickListener);
        }
    }
You may realize that I added an parameter in the top (`int questionNumber;`), that&#39;s gonna identify what question we&#39;re dealing with.&lt;br/&gt;
You also need to set a value for this parameter in `onBindViewHolder`.

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
		holder.questionNumber = position+1;//add one because the position of the first item is 0
		.
		.
		.
	}

&lt;h2&gt;Calculate marks of each question&lt;/h2&gt;

Finally. when you want calculate marks of a question, the result is stored in `answers` to get it just call

    answers.get(questioNumber+1)//add one because the index always start from 0

One Last thing. You may face a problem when changing the state of an item in recyclerview, so when you scroll down the first items became invisible and the recyclerview **recycle** those items, so any changes happened (e. g checking a checkbox) will be lost, and the items will reset to the first state.&lt;br/&gt;
If that happened check this [solution](https://stackoverflow.com/questions/50328655/recyclerview-items-values-reset-when-scrolling-down)



&lt;h1&gt; Update &lt;/h1&gt;
As I mentioned before, you can still do it with buttons if you want or any input type you find it better&lt;br/&gt;
The only thing you need to change is the way of catching user inputs.&lt;br/&gt;
E.g. if you used buttons, you&#39;ll have 3 possible inputs:&lt;br/&gt;

 1. True. If btn1 is pressed and btn2 is not
 2. False. If btn1 in bot pressed bur btn2 in pressed
 3. Neither. If btn1 and btn2 are not pressed&lt;br/&gt;

You can sill use Boolean type to store the input by putting `null` when the value is *Neither true of false*(e.g. `bta1=null`)

The problem with this method is that it&#39;s pretty complicated and confusing (but still possible)&lt;br/&gt;
So. My suggestion is to use 3 statues checkbox.&lt;br/&gt;
This is a third-party library witch you can add to your preject.&lt;br/&gt;
It&#39;s pretty much a checkbox with three stats

 1. Checked
 2. Unchecked
 3. Indeterminate

You can find it here [GitHub](https://github.com/sephiroth74/Tri-State-Checkbox)&lt;br/&gt;
And another one here [GitHub](https://github.com/sevar83/indeterminate-checkbox)


  [1]: https://developer.android.com/guide/topics/ui/controls/checkbox

</details>



huangapple
  • 本文由 发表于 2020年10月3日 02:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64176654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定