我看到github中有很多例子,人们使用这种方法:
String postKey = getRef(position).getKey();
但在我的Android Studio中,它无法解析getRef(int)
方法。这是代码:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
articleModel = new ArrayList<>();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
articleModel.add(postSnapshot.getValue(ArticleModel.class));
}
adapter = new ArticleAdapter(getActivity().getApplicationContext(), R.layout.fragment_article_list_items, articleModel);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class);
String postKey = getRef(position).getKey();
intent.putExtra(ArticleDetailsActivity.EXTRA_POST_KEY, postKey);
getActivity().startActivity(intent);
}
});
}
有谁知道为什么会发生这种情况以及如何解决这个问题?
答案 0 :(得分:1)
Firebase API中实际上没有getRef
这样的功能。
到目前为止,我已经明白您已经尝试将onItemClickListener
添加到ListView
,并且您尝试将key
传递给ArticleDetailsActivity
articleModel
。所以我认为,当您填充mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class);
// Find the key in the list you've populated earlier.
String postKey = articleModel.get(position).getKey();
intent.putExtra(ArticleDetailsActivity.EXTRA_POST_KEY, postKey);
getActivity().startActivity(intent);
}
});
列表时,您可以在那里找到密钥。
因此修改后的代码可能如下所示。
{{1}}