如何将数组的更新值保存回Firestore数据库? [英] How to save the updated value of an array back to the firestore database?

查看:48
本文介绍了如何将数组的更新值保存回Firestore数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for (QueryDocumentSnapshot snapshot : Objects.requireNonNull(task.getResult())) {
                    String value1 = snapshot.getString("QuestionName");
                    q_tv1.setText("Question: "+value1);
                    ArrayList<String> description = (ArrayList<String>) snapshot.get("Description");
                    Log.d(Tag,"output for des: "+description);
                    List<String> noOfOptions = (List<String>) snapshot.get("Options");
                    Log.d(Tag,"output for option: "+noOfOptions);
                    ArrayList<String> finalCount=(ArrayList<String>) snapshot.get("CountValue");
                    int size=noOfOptions.size();
                    Log.d(Tag,"size: "+finalCount);
                    for(i=0;i<=size-1;i++)
                    {
                        String op=noOfOptions.get(i);
                        String n=finalCount.get(i);

                        CardView cardview=new CardView(PublicVote.this);
                        LayoutParams layoutparams = new LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT
                        );
                        LayoutParams layoutparams2 = new LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT
                        );
                        cardview.setLayoutParams(layoutparams);
                        cardview.setRadius(25);
                        layoutparams.setMargins(10,10,10,10);
                        layoutparams2.setMargins(10,100,100,100);
                        cardview.setPadding(25, 25, 25, 25);
                        cardview.setCardBackgroundColor(Color.TRANSPARENT);
                        cardview.setBackgroundTintList(ColorStateList.valueOf(Color.YELLOW));
                        cardview.setMaxCardElevation(30);
                        cardview.setMaxCardElevation(6);

                        TextView textview = new TextView(PublicVote.this);
                        textview.setLayoutParams(layoutparams);
                        textview.append("o " +noOfOptions.get(i));
                        textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
                        textview.setTextColor(Color.BLACK);
                        textview.setPadding(25,25,25,25);
                        textview.setGravity(Gravity.LEFT);
                        textview.setLayoutParams(layoutparams);


                        TextView textview1 = new TextView(PublicVote.this);
                        textview1.setLayoutParams(layoutparams);
                        textview1.append("Description: "+description.get(i));
                        textview1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
                        textview1.setTextColor(Color.BLACK);
                        textview1.setPadding(25,25,25,25);
                        textview1.setGravity(Gravity.LEFT);
                        textview1.setLayoutParams(layoutparams2);

                        cardview.addView(textview);
                        cardview.addView(textview1);
                        l1.addView(cardview);
                        cardview.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //Toast.makeText(PublicVote.this, n, Toast.LENGTH_SHORT).show();
                                n1 = Integer.parseInt(String.valueOf(n));
                                n1 = n1 + 1;
                                Log.d(Tag,"i->>: "+n1);
                            }
                        });

                    }
                }

这是我上面的代码,我的n1是countValue,每次用户单击它不断增加的cardview时都会更新.现在我想将这个增加的值保存起来,即countValue(此处为n1)返回到下面给出的数据库中.

This is my code above here my n1 is the countValue which is updated everytime the user clicks the cardview it keeps on incrementing. Now I want to save this incremented value i.e. the countValue (n1 here)back to the database which is given below.

现在,这里的选项121和122分别具有计数值10和5,当单击任一选项时,计数值将增加1,可以说单击121,因此新值是11和5(保持不变).这11和5需要更新回数据库,对此需要帮助!

Now here the options 121 and 122 have count value 10 and 5 respectively which gets incremented by 1 when any one option is clicked, lets say 121 is clicked so new value is 11 and 5(remains the same). This 11 and 5 needs to be updated back to the database, need help with this!

推荐答案

哼,我建议在XML布局中设置设置,而不是在类中进行设置,这样更易​​于可视化.如果我理解应该是这样,除非是另一个变量.

Hum, I suggest to set the settings in the XML layout instead of the setting in the class, it's more easy to visualize. If I understood should be like this, unless is another variable.

如果变量n1正在计数数字

If the variable n1 is counting the numbers

Log.d(Tag,"n1->> ::" + n1);

Log.d(Tag,"n1->>: "+ n1);

尝试执行以下操作:

Log.d(Tag,"n1->">:"+(n1 + 1));

Log.d(Tag,"n1->>: "+ (n1 + 1));

或在操作之外设置数字

n1 = n1 +1;

n1 = n1 + 1;

这样,他们每次点击都会设置数字+1,尝试执行我对xml中的设置所说的话.

This way every time they click on is going to set the number +1, try to do what i said about the settings in the xml.

这篇关于如何将数组的更新值保存回Firestore数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆