Tomas Maksimavicius
Tomas Maksimavicius

Reputation: 414

Android Firebase cannot resolve method getRef(position)

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?

Upvotes: 0

Views: 4707

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

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

Upvotes: 1

Related Questions