将Firestore查询结果保存到ArrayList中,并将每个结果添加到CSV文件中。

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

Saving results of Firestore query to ArrayList and adding each result to CSV file

问题

Sure, here's the translation of the code portion you provided:

我想将Firestore查询文档快照的结果保存到CSV文件中,每个新结果都添加到CSV文件的新行。

我目前使用for循环来遍历结果,并为每个String赋值,然后将它们添加到CSV数据列表中。然而,我的CSV文件显示的是最后一个结果的重复,而不是查询的两个当前结果。

我的代码如下:

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            for (QueryDocumentSnapshot document : task.getResult()){

                //Log.d("TAG", document.getId() + " => " + document.getData());

                //CSVWRITER库用于创建CSV文件
                try {
                    CSVWriter writer = new CSVWriter((new FileWriter(csv)));

                    List<String[]> data = new ArrayList<String[]>();
                    data.add(new String[]{"Profession", "CPD_Number", "Summary_Text", "Personal_Statement"});
                    data.add(new String[]{fProfession, fCpd_Number, fSummary_Text, fPersonal_Statement});
                    data.add(new String[]{"Activity_Name", "Activity_Date", "Activity_Hours", "Activity_Mins", "Activity_Type", "Activity_Description", "Activity_Ref1", "Activity_Ref2", "Activity_Ref3", "Activity_Ref4", "Image_Url"});

                    for (int i = 0; i < task.getResult().size(); i++) {

                        fActivity_Name = document.getString("Activity_Name");
                        fActivity_Date = document.getString("Activity_Date");
                        fActivity_Hours = document.getString("Activity_Hours");
                        fActivity_Mins = document.getString("Activity_Mins");
                        fActivity_Type = document.getString("Activity_Type");
                        fActivity_Description = document.getString("Activity_Description");
                        fActivity_Ref1 = document.getString("Activity_Ref1");
                        fActivity_Ref2 = document.getString("Activity_Ref2");
                        fActivity_Ref3 = document.getString("Activity_Ref3");
                        fActivity_Ref4 = document.getString("Activity_Ref4");
                        fImage_Url = document.getString("Image_URL");

                        Log.d("TAG", "activity name is: " + fActivity_Name);
                        data.add(new String[]{fActivity_Name, fActivity_Date, fActivity_Hours, fActivity_Mins, fActivity_Type, fActivity_Description, fActivity_Ref1, fActivity_Ref2, fActivity_Ref3, fActivity_Ref4, fImage_Url});

                    }

                    writer.writeAll(data);
                    writer.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.d("TAG", "Error getting documents: " + task.getException());
        }
    }
});

请问是否还有其他需要翻译的部分?

英文:

I want to save the results of my Firestore query document snapshot to a CSV file with each new result being added to a new line of the CSV file.

I am currently using a for loop to iterate over the results and assign a value to each String from the task result and then add these to the CSV data list. However, my CSV file is showing a duplicate of the last result rather than the two current results of my query.

My code looks like this

query.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                    @Override
                    public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                        if(task.isSuccessful()){
                            for (QueryDocumentSnapshot document : task.getResult()){

                                //Log.d(&quot;TAG&quot;, document.getId() + &quot; =&gt; &quot; + document.getData());

                                //CSVWRITER LIBRARY USED TO CREATED CSV FILE
                                try {
                                    CSVWriter writer = new CSVWriter((new FileWriter(csv)));

                                    List&lt;String[]&gt; data = new ArrayList&lt;String[]&gt;();
                                    data.add(new String[]{&quot;Profession&quot;, &quot;CPD_Number&quot;, &quot;Summary_Text&quot;, &quot;Personal_Statement&quot;});
                                    data.add(new String[]{fProfession, fCpd_Number, fSummary_Text, fPersonal_Statement});
                                    data.add(new String[]{&quot;Activity_Name&quot;, &quot;Activity_Date&quot;, &quot;Activity_Hours&quot;, &quot;Activity_Mins&quot;, &quot;Activity_Type&quot;, &quot;Activity_Description&quot;, &quot;Activity_Ref1&quot;, &quot;Activity_Ref2&quot;, &quot;Activity_Ref3&quot;, &quot;Activity_Ref4&quot;, &quot;Image_Url&quot;});

                                        for (int i = 0; i &lt; task.getResult().size(); i++) {

                                            fActivity_Name = document.getString(&quot;Activity_Name&quot;);
                                            fActivity_Date = document.getString(&quot;Activity_Date&quot;);
                                            fActivity_Hours = document.getString(&quot;Activity_Hours&quot;);
                                            fActivity_Mins = document.getString(&quot;Activity_Mins&quot;);
                                            fActivity_Type = document.getString(&quot;Activity_Type&quot;);
                                            fActivity_Description = document.getString(&quot;Activity_Description&quot;);
                                            fActivity_Ref1 = document.getString(&quot;Activity_Ref1&quot;);
                                            fActivity_Ref2 = document.getString(&quot;Activity_Ref2&quot;);
                                            fActivity_Ref3 = document.getString(&quot;Activity_Ref3&quot;);
                                            fActivity_Ref4 = document.getString(&quot;Activity_Ref4&quot;);
                                            fImage_Url = document.getString(&quot;Image_URL&quot;);

                                            Log.d(&quot;TAG&quot;, &quot;activity name is: &quot; + fActivity_Name);
                                            data.add(new String[]{fActivity_Name, fActivity_Date, fActivity_Hours, fActivity_Mins, fActivity_Type, fActivity_Description, fActivity_Ref1, fActivity_Ref2, fActivity_Ref3, fActivity_Ref4, fImage_Url});

                                        }

                                    writer.writeAll(data);
                                    writer.close();

                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            Log.d(&quot;TAG&quot;, &quot;Error getting documents: &quot; + task.getException());
                        }
                    }
                });

Can anyone spot what might be going on here? Also according to my log, Log.d(&quot;TAG&quot;, &quot;activity name is: &quot; + fActivity_Name); this line prints twice and I'm not sure why it's doing this either.

Log results:

2020-07-31 15:25:35.201 6741-6741/com.example.cpd D/TAG: activity name is: hoir CHANGE AGAIN NEW
2020-07-31 15:25:35.201 6741-6741/com.example.cpd D/TAG: activity name is: hoir CHANGE AGAIN NEW
2020-07-31 15:25:35.203 6741-6741/com.example.cpd D/TAG: activity name is: other CHANGE
2020-07-31 15:25:35.204 6741-6741/com.example.cpd D/TAG: activity name is: other CHANGE

答案1

得分: 2

我终于成功解决了这个问题!解决方法如下:

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            // 使用CSVWRITER库创建CSV文件
            try {
                CSVWriter writer = new CSVWriter(new FileWriter(csv));
    
                List<String[]> data = new ArrayList<String[]>();
                data.add(new String[]{"职业", "CPD编号", "摘要文本", "个人陈述"});
                data.add(new String[]{fProfession, fCpd_Number, fSummary_Text, fPersonal_Statement});
                data.add(new String[]{"活动名称", "活动日期", "活动小时", "活动分钟", "活动类型", "活动描述", "活动引用1", "活动引用2", "活动引用3", "活动引用4", "图片URL"});
    
                for (QueryDocumentSnapshot document : task.getResult()){
    
                    fActivity_Name = document.getString("Activity_Name");
                    fActivity_Date = document.getString("Activity_Date");
                    fActivity_Hours = document.getString("Activity_Hours");
                    fActivity_Mins = document.getString("Activity_Mins");
                    fActivity_Type = document.getString("Activity_Type");
                    fActivity_Description = document.getString("Activity_Description");
                    fActivity_Ref1 = document.getString("Activity_Ref1");
                    fActivity_Ref2 = document.getString("Activity_Ref2");
                    fActivity_Ref3 = document.getString("Activity_Ref3");
                    fActivity_Ref4 = document.getString("Activity_Ref4");
                    fImage_Url = document.getString("Image_URL");
                    //Log.d("TAG", document.getId() + " => " + document.getData());
                    data.add(new String[]{fActivity_Name, fActivity_Date, fActivity_Hours, fActivity_Mins, fActivity_Type, fActivity_Description, fActivity_Ref1, fActivity_Ref2, fActivity_Ref3, fActivity_Ref4, fImage_Url});
                }
    
                writer.writeAll(data);
                writer.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        } else {
            //Log.d("TAG", "获取文档出错: " + task.getException());
        }
    }
});

通过将QueryDocumentSnapshot任务结果移动到写入CSV文件的try/catch块中来完成这个操作。

英文:

I finally managed to solve this! The solution is as follows:

query.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
if(task.isSuccessful()){
//CSVWRITER LIBRARY USED TO CREATED CSV FILE
try {
CSVWriter writer = new CSVWriter((new FileWriter(csv)));
List&lt;String[]&gt; data = new ArrayList&lt;String[]&gt;();
data.add(new String[]{&quot;Profession&quot;, &quot;CPD_Number&quot;, &quot;Summary_Text&quot;, &quot;Personal_Statement&quot;});
data.add(new String[]{fProfession, fCpd_Number, fSummary_Text, fPersonal_Statement});
data.add(new String[]{&quot;Activity_Name&quot;, &quot;Activity_Date&quot;, &quot;Activity_Hours&quot;, &quot;Activity_Mins&quot;, &quot;Activity_Type&quot;, &quot;Activity_Description&quot;, &quot;Activity_Ref1&quot;, &quot;Activity_Ref2&quot;, &quot;Activity_Ref3&quot;, &quot;Activity_Ref4&quot;, &quot;Image_Url&quot;});
for (QueryDocumentSnapshot document : task.getResult()){
fActivity_Name = document.getString(&quot;Activity_Name&quot;);
fActivity_Date = document.getString(&quot;Activity_Date&quot;);
fActivity_Hours = document.getString(&quot;Activity_Hours&quot;);
fActivity_Mins = document.getString(&quot;Activity_Mins&quot;);
fActivity_Type = document.getString(&quot;Activity_Type&quot;);
fActivity_Description = document.getString(&quot;Activity_Description&quot;);
fActivity_Ref1 = document.getString(&quot;Activity_Ref1&quot;);
fActivity_Ref2 = document.getString(&quot;Activity_Ref2&quot;);
fActivity_Ref3 = document.getString(&quot;Activity_Ref3&quot;);
fActivity_Ref4 = document.getString(&quot;Activity_Ref4&quot;);
fImage_Url = document.getString(&quot;Image_URL&quot;);
//Log.d(&quot;TAG&quot;, document.getId() + &quot; =&gt; &quot; + document.getData());
data.add(new String[]{fActivity_Name, fActivity_Date, fActivity_Hours, fActivity_Mins, fActivity_Type, fActivity_Description, fActivity_Ref1, fActivity_Ref2, fActivity_Ref3, fActivity_Ref4, fImage_Url});
}
writer.writeAll(data);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//Log.d(&quot;TAG&quot;, &quot;Error getting documents: &quot; + task.getException());
}
}
});

Did this by moving QueryDocumentSnapshot task result inside of my try/catch when writing the CSV file.

huangapple
  • 本文由 发表于 2020年7月31日 22:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63193833.html
匿名

发表评论

匿名网友

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

确定