您好,希望有人能看出问题所在。尝试从firebase数据库中获取数据并使用FirebaseRecyclerAdapter显示它,但它仅在我单击操作栏中的searchview后显示数据。只是无法弄清楚原因。
public class CustomerFragment extends Fragment{
private FirebaseRecyclerAdapter<Customer,CustomerViewHolder> customerAdapter;
final String TAG = "CustomerFrag: ";
private EventBus eventBus = EventBus.getDefault();
private DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
private DatabaseReference customerRef = rootRef.child("customers");
private RecyclerView rvCustomer;
public CustomerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
((AppStart)getActivity()).getSupportActionBar().setSubtitle("Customer List");
setHasOptionsMenu(true);
Log.d(TAG," onCreate");
}
// Inflate the menu; this adds items to the action bar if it is present.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
menu.clear();
inflater.inflate(R.menu.toolbar_customermenu, menu);
MenuItem searchItem = menu.findItem(R.id.customerSearch);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Customer");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
Log.d(TAG," onCreateOptionsMenu");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_customer, container, false);
rvCustomer = (RecyclerView)rootView.findViewById(R.id.rvCustomer);
rvCustomer.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
rvCustomer.setLayoutManager(manager);
Log.d(TAG," onCreateView");
customerAdapter = new FirebaseRecyclerAdapter<Customer,CustomerViewHolder>(Customer.class, R.layout.customer_cardview, CustomerViewHolder.class, customerRef) {
@Override
protected void populateViewHolder(CustomerViewHolder viewHolder, final Customer customer, final int position) {
Log.d(TAG," populateViewHolder");
viewHolder.tvCustomerName.setText(customer.getCustomerName());
viewHolder.tvCustomerAddress.setText(customer.getCustomerAddress());
viewHolder.tvCustomerTel.setText(customer.getCustomerTel());
}
};
rvCustomer.setAdapter(customerAdapter);
return rootView;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
AppCompatActivity activity = (AppCompatActivity) getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
Log.d(TAG," onOptionsItemsSelected");
switch (item.getItemId()) {
case R.id.customerSearch:
Log.d(TAG,"Search icon pressed");
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
static class CustomerViewHolder extends RecyclerView.ViewHolder{
private TextView tvCustomerName;
private TextView tvCustomerAddress;
private TextView tvCustomerTel;
private CardView cvCustomer;
public CustomerViewHolder(View itemView) {
super(itemView);
tvCustomerName = (TextView)itemView.findViewById(R.id.tvCustomerName);
tvCustomerAddress = (TextView)itemView.findViewById(R.id.tvCustomerAddress);
tvCustomerTel = (TextView)itemView.findViewById(R.id.tvCustomerTel);
cvCustomer = (CardView)itemView.findViewById(R.id.cvCustomer);
Log.d("CustFragment"," CustomerViewHolder");
}
}
}
答案 0 :(得分:1)
希望这有助于某人。