Android Firebase无法解析方法getRef(position) [英] Android Firebase cannot resolve method getRef(position)

查看:219
本文介绍了Android Firebase无法解析方法getRef(position)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串postKey = getRef(position)在github中有很多例子,人们使用这样的方法: ).getKey(); 

但是在我的Android Studio中并没有解析 getRef(int) code>方法。这里是代码:

  @Override 
public void onDataChange(DataSnapshot dataSnapshot){

articleModel = new ArrayList<>(); (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 $ b $ 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);
}
});

$ / code>

有谁知道为什么发生这种情况,以及如何解决这个问题?

解决方案

实际上,在Firebase API中没有像 getRef 这样的函数。



到目前为止,我已经知道你正试图把 onItemClickListener 放到 ListView ,并且您正试图将一些传递给 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);

//找到键
String postKey = articleModel.get(position).getKey();

intent.putExtra(ArticleDetailsActivity.EXTRA_POST_KEY,postKey);
getActivity ().startActivity(intent);
}
});


I see there are a lot of examples in github where people are using such method:

String postKey = getRef(position).getKey();

But in my Android Studio it does not resolve getRef(int) method. Here is the code:

@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);
        }
    });
}

Does anyone know why this happens and how to solve this problem?

解决方案

There is no such function as getRef actually in Firebase API.

So far I've understood that you're trying to put an onItemClickListener to your ListView and you're trying to pass some key to the ArticleDetailsActivity. So I think, as you've populated the articleModel list you can find the key in there.

So the modified code might look like this.

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);
    }
});

这篇关于Android Firebase无法解析方法getRef(position)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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