如何向setSuggestions中添加一组建议

时间:2018-08-09 11:57:13

标签: java android firebase firebase-realtime-database

我正在使用库SearchView提供的材料'com.miguelcatalan:materialsearchview:1.4.0'。该库使用setSuggestions在搜索视图中设置建议,如下面的代码所示。但是setSuggestions仅允许String[]。我想从firebase加载项目列表,并放入建议中。问题是setSuggestion不允许ArrayList

materialSearchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {
            //Do some magic
            mProgress.setMessage("Loading...");
            mProgress.show();
            mDatabasePlaces.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot data:dataSnapshot.getChildren()){


                        Place models=data.getValue(Place.class);

                        placelist.add(models.getName());

                    }
                    mProgress.dismiss();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });



            materialSearchView.setSuggestions(placelist);
        }

        @Override
        public void onSearchViewClosed() {
            //Do some magic
        }
    });



}

在代码位置列表中是ArrayList所不允许的materialSearchView.setSuggestions(placelist);,所以我如何将该位置列表放入setSuggestions

2 个答案:

答案 0 :(得分:3)

您可以像这样将其解析为String[]

 String[] parsedArray = placelist.toArray(new String[placelist.size()])

答案 1 :(得分:1)

由于onDataChange方法的异步行为,要完成工作,请移动以下代码行:

materialSearchView.setSuggestions(placelist);
在此代码行之后,

onDataChange方法内:

mProgress.dismiss();

正如阿图尔·阿赫诺扬(Artur Akhnoyan)在回答中提到的那样,请将参数从placelist更改为:

placelist.toArray(new String[placelist.size()])