Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在收集到所有文档后,可以';t向ArrayList、Android Studio、Java、Firebase Firestore添加字段_Java_Android_Firebase_Arraylist_Google Cloud Firestore - Fatal编程技术网

在收集到所有文档后,可以';t向ArrayList、Android Studio、Java、Firebase Firestore添加字段

在收集到所有文档后,可以';t向ArrayList、Android Studio、Java、Firebase Firestore添加字段,java,android,firebase,arraylist,google-cloud-firestore,Java,Android,Firebase,Arraylist,Google Cloud Firestore,大家好,我有一个问题,因为我要从Firebase Firestore获取整个集合,希望在每个文档中获取字段“主题名称”,并将其添加到ArrayList中,然后在ListView中显示此主题。这将是我申请的一部分 但是 它似乎获取了文档,并读取了它们的字段,正如我在日志中看到的那样 Log.d(TAG, document.getId() + " => " + document.get("topic_name")); _ 它不会将它添加到ArrayList中,或者它以某种方式消失了,因为设备

大家好,我有一个问题,因为我要从Firebase Firestore获取整个集合,希望在每个文档中获取字段“主题名称”,并将其添加到ArrayList中,然后在ListView中显示此主题。这将是我申请的一部分

但是

它似乎获取了文档,并读取了它们的字段,正如我在日志中看到的那样

Log.d(TAG, document.getId() + " => " + document.get("topic_name"));
_

它不会将它添加到ArrayList中,或者它以某种方式消失了,因为设备不显示列表

起初我认为这是因为从内部类访问ArrayList时需要声明为final,但事实并非如此,因为我添加了一个测试主题来检查该假设。(测试主题123测试主题125)并正常显示它们,因此ArrayList不是最终结果

我试着将负责ListView的整个代码放在onComplete方法中,但它似乎不起作用,因为适配器充满了错误,如果可能的话,我希望得到一些帮助,或者是关于如何做的其他想法。如果我复制了这个主题,我很抱歉,我试图寻找答案,但找不到

代码如下:

public class Forum extends Activity {

public static final String TAG = "TAG";
FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forum);


    db = FirebaseFirestore.getInstance();
    final ArrayList<String> forumTopics = new ArrayList<>();

    db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                            Log.d(TAG, document.getId() + " => " + document.get("topic_name"));
                        }

                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    String test_string = "Test Topic 123";
    forumTopics.add(0,test_string);
    forumTopics.add(1,"Test Topic 124");
    forumTopics.add(2,"Test Topic 125");
    forumTopics.add(3,"Test Topic 126");

    ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);

    forumListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String topic = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Forum.this, topic, Toast.LENGTH_LONG).show();
                }
            }
    );
}
公共课堂论坛扩展活动{
公共静态最终字符串TAG=“TAG”;
FirebaseFirestore数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_论坛);
db=FirebaseFirestore.getInstance();
final ArrayList forumTopics=新ArrayList();
db.收藏(“论坛主题”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
整数计数=0;
对于(QueryDocumentSnapshot文档:task.getResult()){
add(count,document.getString(“topic_name”);
计数+=1;
Log.d(标记,document.getId()+“=>”+document.get(“主题名称”));
}
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
}
});
字符串测试\u String=“测试主题123”;
添加(0,测试字符串);
增加(1,“测试主题124”);
增加(2,“测试主题125”);
增加(3,“测试主题126”);
ListAdapter forumAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,forumTopics);
ListView forumListView=findViewById(R.id.forumListView);
setAdapter(forumAdapter);
forumListView.setOnItemClickListener(
新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串主题=String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(Forum.this,topic,Toast.LENGTH_LONG).show();
}
}
);
}

非常感谢。

试试这个

db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                             forumTopics.add(document.get("topic_name"));
                        }
                        forumAdapter.notifyDataSetChanged()
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);
db.collection(“论坛主题”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
整数计数=0;
对于(QueryDocumentSnapshot文档:task.getResult()){
add(count,document.getString(“topic_name”);
计数+=1;
forumTopics.add(document.get(“topic_name”);
}
forumAdapter.notifyDataSetChanged()
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
}
});
ListAdapter forumAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,forumTopics);
ListView forumListView=findViewById(R.id.forumListView);
setAdapter(forumAdapter);
试试这个

db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                             forumTopics.add(document.get("topic_name"));
                        }
                        forumAdapter.notifyDataSetChanged()
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);
db.collection(“论坛主题”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
整数计数=0;
对于(QueryDocumentSnapshot文档:task.getResult()){
add(count,document.getString(“topic_name”);
计数+=1;
forumTopics.add(document.get(“topic_name”);
}
forumAdapter.notifyDataSetChanged()
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
}
});
ListAdapter forumAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,forumTopics);
ListView forumListView=findViewById(R.id.forumListView);
setAdapter(forumAdapter);

在您的帮助下,我解决了这个问题。以下是我的解决方案。感谢所有帮助我的人。我非常感谢,祝您度过美好的一天

    db = FirebaseFirestore.getInstance();
    final ArrayList<String> forumTopics = new ArrayList<>();

    final ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);

    db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                        }
                        ((ArrayAdapter) forumAdapter).notifyDataSetChanged();
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    forumListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String topic = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Forum.this, topic, Toast.LENGTH_LONG).show();
                }
            }
    );
db=FirebaseFirestore.getInstance();
final ArrayList forumTopics=新ArrayList();
final ListAdapter forumAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,forumTopics);
ListView forumListView=findViewById(R.id.forumListView);
setAdapter(forumAdapter);
db.集合(“论坛”主题