2016-10-11 44 views
0

我看到在github上有很多的例子,人們使用這樣的方法:的Android火力地堡無法解析方法getRef(位置)

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

但在我的Android工作室它不能解決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

你可以給你看到'getRef(position)'工作的鏈接嗎? –

+0

https://github.com/ikotrabakov/FireBlog/blob/017d8fe4df7b61ece00cad3e828e4a7cc8c5adcc/app/src/main/java/com/ivaylok/fireblog/MainActivity.java –

+1

很可能您正在尋找adapter.getRef(position)。 –

回答

1

實際上在Firebase API中沒有getRef這樣的功能。

到目前爲止,我明白,你想放的onItemClickListenerListView和你想傳遞一些keyArticleDetailsActivity。所以我認爲,正如你填寫了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); 
    } 
});