【发布时间】:2021-09-29 13:30:15
【问题描述】:
我是使用 Android Studio 和 Firestore DB 的新手。 我正在尝试将几行文本从 firestore 加载到我的 android 应用程序中。我在调试时注意到 whereEqualto 字段名称的代码行没有读取所需的数据。我不知道在获取数据时哪里出错了。任何帮助,将不胜感激。这是我的代码。
ViewActivity.class
public class ViewActivity extends AppCompatActivity implements View.OnClickListener {
private static String topic_name;
ImageButton next, previous;
TextView text_area;
private Information infodb;
public static List<String> mFragments;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewtext);
FetchDetails();
text_area= (TextView)findViewById(R.id.tf_text);
next = (ImageButton) findViewById(R.id.next);
next.setOnClickListener(this);
previous = (ImageButton) findViewById(R.id.previous);
previous.setOnClickListener(this);
}
public void FetchDetails(){
FirestoreUtil.getCollection("Information Text").whereEqualTo("topic_name",topic_name).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Information ip = document.toObject(Information.class);
mFragments=(ip.getFragments());
text_area.setText(ip.getFragments().get(0));
}
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next:
text_area.setText(infodb.getFragments().get(1));
break;
case R.id.previous:
text_area.setText(infodb.getFragments().get(2));
break;
}
}
}
信息类
public class Information {
public List<String> fragments;
private int topic_id;
private String topic_name;
private String game;
private String link;
private int level;
public Information() {
}
public Information(String topic_name, List<String> fragments,String game, String link, int level,int topic_id)
{
this.fragments = fragments;
this.topic_name= topic_name;
this.game=game;
this.topic_id=topic_id;
this.level = level;
this.link=link;
}
public Information(List<String> fragments) {
this.fragments=fragments;
}
public String getTopic_name() {return topic_name;}
public void setTopic_name(String topic_name) {
this.topic_name = topic_name;
}
public void setFragments(List<String> fragments) {
this.fragments = fragments;
}
public List<String> getFragments() {
return fragments;
}
public String getGame() { return game; }
public void setGame(String game) { this.game = game; }
public String getLink() { return link; }
public void setLink(String link) { this.link = link; }
public void setLevel(int level) {
this.level = level;
}
public int getLevel() {
return level;
}
public void setTopic_id(int topic_id) {this.topic_id=topic_id;}
public int getTopic_id() {
return topic_id;
}
}
【问题讨论】:
-
您的参考资料中
topic_name的值是多少?还请编辑您的问题并分享您的Information课程的内容。 -
嗨,Alex,我刚刚更新了 :) 感谢您抽出宝贵时间回复
-
.getCollection("Information Text")返回什么,topic_name在您的参考中的值是什么? -
问题是它根本没有进入这个集合。 topic_name 是我在同一个类中创建的字符串对象。
-
刚刚编辑了整个文档以获得更清晰的图片
标签: android android-studio google-cloud-firestore