Android 按按钮单击过滤Firebase数据库返回单个循环视图listItem
我希望ListView仅显示公司名称与搜索按钮上EditText中输入的名称相同的数据单击: 按钮单击事件-Android 按按钮单击过滤Firebase数据库返回单个循环视图listItem,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我希望ListView仅显示公司名称与搜索按钮上EditText中输入的名称相同的数据单击: 按钮单击事件- Button btnSearch = (Button) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EditText objCompany
Button btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText objCompany = (EditText) findViewById(R.id.filterEdit);
mMessagesDatabaseReference.orderByChild("company").equalTo(objCompany.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for(DataSnapshot d1 : dataSnapshot.getChildren()) {
for(DataSnapshot d2 : d1.child("company").getChildren()) {
postAdapterObject.clear();
// Get the value from the DataSnapshot and add it to the item list
post itemObject = d1.getValue(post.class);
//this is where data from database is entered into a list of objects
postAdapterObject.add(itemObject);
postAdapterObject.notifyDataSetChanged();
}
}
Toast.makeText(MainActivity.this, "Data Filtered", Toast.LENGTH_LONG).show();
}
else{
postAdapterObject.clear();
postAdapterObject.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Data Not Found!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
postAdapterObject.clear();
postAdapterObject.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "No Data Found! Try with some other data entry", Toast.LENGTH_LONG).show();
}
});
}
});
看起来您在JSON中的循环级别太深了。这要简单得多,并且应该接近您所需要的:
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postAdapterObject.clear();
for(DataSnapshot d1: dataSnapshot.getChildren()) {
post itemObject = d1.getValue(post.class);
postAdapterObject.add(itemObject);
}
postAdapterObject.notifyDataSetChanged();
if (postAdapterObject.size() > 0) { // TODO: write this condition
Toast.makeText(MainActivity.this, "Data Filtered", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data Not Found!", Toast.LENGTH_LONG).show();
}
}
如果(postAdapterObject.size()>0){,您可能需要修复签入,因为它取决于postAdapterObject
的类型