FirebaseUI - Incompatible types: String cannot be converted to DatabaseReference

时间:2018-06-20 05:04:26

标签: android firebase firebase-realtime-database firebaseui

I got Incompatible types: String cannot be converted to DatabaseReference error in onItemClick for getting Firebase Database Key.

The code I use to populate ListView from Firebase Database

mListView = (ListView) rootView.findViewById(R.id.listview1);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://my-project.firebaseio.com/name/data");
FirebaseListAdapter<Personnel> adapter = new FirebaseListAdapter<Personnel>(getActivity(),Personnel.class,R.layout.layout_items,databaseReference) {
    @Override
    protected void populateView(View v, Personnel model, int position) {
        TextView textView = (TextView) v.findViewById(R.id.tv_title);
        textView.setText(model.name);
    }
};
mListView.setAdapter(adapter);

onItemClick for the ListView (modified from this)

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        DatabaseReference itemRef = adapter.getItem(position);
        Toast toast = Toast.makeText(getActivity().getApplicationContext(), itemRef.getKey(), Toast.LENGTH_SHORT);
        toast.show();
    }
});

First I got Variable 'adapter' is accessed from within inner class, needs to be declared final error on adapter in adapter.getItem(position); under onItemClick here

So I added final at final FirebaseListAdapter<Personnel> adapter... then this error come up here

I have tried declared adapter as global but still got the same error. Please let me know how can I fix this. Thanks!

1 个答案:

答案 0 :(得分:1)

更改此:

 DatabaseReference itemRef = adapter.getItem(position);

对此:

Personnel itemRef = adapter.getItem(position);