我尝试使用SearchView过滤Firebase数据库中的数据。当我在SearchView中填充文本时,不会过滤数据。代码有什么问题?我一直在寻找解决这个问题的方法,但我没有找到它。
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Declare Variables
private ListView listView;
ArrayList<Data> dataList;
DatabaseReference databaseReference;
SearchView seachView;
private String temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseReference = FirebaseDatabase.getInstance().getReference("Data");
listView = (ListView) findViewById(R.id.list_view);
dataList = new ArrayList<>();
seachView = (SearchView) findViewById(R.id.search);
final ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,R.layout.list_item,dataList);
listView.setAdapter(adapter);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshoot : dataSnapshot.getChildren()) {
Data data = dataSnapshoot.getValue(Data.class);
dataList.add(data);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
seachView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return true;
}
});
}}
Datalist.java
public class DataList extends ArrayAdapter<Data> {
private Activity context;
private List<Data> dataList;
public DataList(Activity context,List<Data>dataList)
{
super(context,R.layout.list_item,dataList);
this.context = context;
this.dataList = dataList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_item,null,true);
TextView textSite = (TextView) listViewItem.findViewById(R.id.siteName);
Data data = dataList.get(position);
textSite.setText(data.getDataSite());
return listViewItem;
}}
Data.java
public class Data {
String dataPid;
String dataSite;
String dataSid;
public Data(){}
public Data(String dataPid,String dataSite,String dataSid)
{
this.dataPid = dataPid;
this.dataSite = dataSite;
this.dataSid = dataSid;
}
public String getDataPid(){
return dataPid;
}
public String getDataSite(){
return dataSite;
}
public String getDataSid(){
return dataSid;
} @Override
public String toString() {
return dataSite;
}}
希望有人能帮我解决这个问题