无法解决 ‘FirebaseRecyclerOptions’ 中的 ‘getCurrentList’ 方法。

huangapple go评论84阅读模式
英文:

Cannot resolve method 'getCurrentList' in 'FirebaseRecyclerOptions'

问题

我正在开发一个大学搜索应用程序但我无法解决这个错误

以下是代码

```java
public class searchEngineActivity extends AppCompatActivity {
    // ... (其他代码部分未翻译)
                        **databaseReference = adapter.getRef(position);**
                        databaseReference = databaseReference.child(queryCourse);
                        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String queryURL = dataSnapshot.getValue(String.class);
                                queryURL = queryURL.replaceAll("state", queryState);
                                webView.loadUrl(queryURL);

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                    }
                });
                return viewHolder;
            }
        };
        searchRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerPagingAdapter<Object, RecyclerView.ViewHolder> firebaseRecyclerAdapter = null;
        firebaseRecyclerAdapter.startListening();
    } //UPDATING RECYCLER VIEW REGULARLY FROM THE START OF FRAGMENT
}
英文:

I am working on a college search app and I am unable to solve this error.

Here is the code:

public class searchEngineActivity extends AppCompatActivity {
    public String stateSelected;
    public String courseSelected;
    public RecyclerView searchRecyclerView;
    public WebView webView;
    public FirebaseRecyclerOptions adapter;
    public DatabaseReference databaseReference;
    String queryState, queryCourse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_engine);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
        Bundle bundle = getIntent().getExtras();
        stateSelected = bundle.getString(&quot;stateSelected&quot;);
        courseSelected = bundle.getString(&quot;courseSelected&quot;);
        Log.e(&quot;arguments&quot;,stateSelected+courseSelected );
        makeQuery(stateSelected, courseSelected);
        searchRecyclerView = (RecyclerView) findViewById(R.id.searchRecyclerView);
        webView = (WebView) findViewById(R.id.webView);
        searchRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        setAdapter();
        searchRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
    }

    public void makeQuery(String q, String c) {
        queryState = q.replaceAll(&quot; &quot;, &quot;-&quot;);
        queryState = queryState.toLowerCase();
        queryCourse = c.toLowerCase();

    }

    public void setAdapter() {
        Query query = FirebaseDatabase.getInstance().getReference().child(&quot;searchengines&quot;);
        FirebaseRecyclerOptions&lt;searchModelClass&gt; options = new FirebaseRecyclerOptions.Builder&lt;searchModelClass&gt;().setQuery(query, searchModelClass.class).build();
        adapter = new FirebaseRecyclerPagingAdapter&lt;searchModelClass, androidx.recyclerview.widget.RecyclerView.ViewHolder&gt;(options) {

            //@Override
            protected void onBindViewHolder(@NonNull searchRecyclerViewHolder holder, final int position, @NonNull searchModelClass model) {
                holder.setSiteImage(model.getImage());
                holder.setSiteName(model.getName());
                holder.setSiteURL(model.getUrl());
            }

            @Override
            protected void onBindViewHolder(@androidx.annotation.NonNull @NonNull RecyclerView.ViewHolder viewHolder, int position, @androidx.annotation.NonNull @NonNull searchModelClass model) {
            }

            @Override
            protected void onLoadingStateChanged(@androidx.annotation.NonNull @NonNull LoadingState state) {
            }

            @NonNull
            //@Override
            public searchRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.search_recycler_view, parent, false);

                final searchRecyclerViewHolder viewHolder = new searchRecyclerViewHolder(view);
                viewHolder.setOnClickListener(new searchRecyclerViewHolder.ClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        **databaseReference = adapter.getCurrentList(position);**
                        databaseReference = databaseReference.child(queryCourse);
                        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String queryURL = dataSnapshot.getValue(String.class);
                                queryURL = queryURL.replaceAll(&quot;state&quot;, queryState);
                                webView.loadUrl(queryURL);

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                    }
                });
                return viewHolder;
            }
        };
        searchRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerPagingAdapter&lt;Object, RecyclerView.ViewHolder&gt; firebaseRecyclerAdapter = null;
        firebaseRecyclerAdapter.startListening();
    } //UPDATING RECYCLER VIEW REGULARLY FROM THE START OF FRAGMENT
}

Error is:

> Cannot resolve method 'getCurrentList' in 'FirebaseRecyclerOptions':101

I tried to use other library for recycler view in place of firebase ui library, but then I have to change my whole code. What else I can do?

答案1

得分: 1

你的 adapter 对象是一个 FirebaseRecyclerPagingAdapter 类型的对象。当你在这样的对象上尝试调用 getCurrentList(position) 时,得到以下错误是预期的行为:

在 'FirebaseRecyclerOptions' 中无法解析方法 'getCurrentList':101

因为在你的适配器类中不存在 getCurrentList() 方法。而且,如果存在这样一个方法,如其名字所示,它应该返回一个列表而不是一个 DatabaseReference 对象。所以根据你的代码,产生该错误的那行代码应该被移除。

英文:

Your adapter object is an object of type FirebaseRecyclerPagingAdapter. When you try to call getCurrentList(position) on such an object, it's the expected behavior to get the following error:

Cannot resolve method &#39;getCurrentList&#39; in &#39;FirebaseRecyclerOptions&#39;:101

Since there is no getCurrentList() method inside your adapter class. Moreover, if such a method existed, as the name says, it would have returned a list and not a DatabaseReference object. So seeing your code, that line of code that produces that error should be removed.

答案2

得分: 0

你是否正在寻找getRef()?它将是:

databaseReference = adapter.getRef(position);

我强烈建议在这种情况下保持你所使用的类的源代码随手可得,因为整个FirebaseAdapter.java文件只有47行,可以快速帮助你找到存在和不存在的内容。

英文:

Are you looking for getRef()? That'd be:

databaseReference = adapter.getRef(position);

I highly recommend keeping the source code for the class you're using handy in cases like this, as the entire FirebaseAdapter.java file is only 47 lines long and can quickly help you find what does and what doesn't exist.

huangapple
  • 本文由 发表于 2023年4月4日 12:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定