如何将项目生成的密钥添加到Firebase

时间:2016-10-26 23:57:52

标签: android listview firebase firebase-realtime-database onclicklistener

使用onItemClickListener

时,尝试获取已添加项目的生成密钥

enter image description here

请参阅我的代码:

ref = new Firebase("https://.......firebaseio.com/Service");
        final FirebaseListAdapter<MainCategory> adapter = new FirebaseListAdapter<MainCategory>(getActivity(), MainCategory.class,
                android.R.layout.simple_list_item_1,
                ref) {
            @Override
            protected void populateView(View view, MainCategory s, int i) {
                TextView tv = (TextView) view.findViewById(android.R.id.text1);
                tv.setText(s.getTitle());

            }
        };
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String itemId = String.valueOf(parent.getId());
                String root = String.valueOf(ref.getRoot().getKey());
                MainCategory object = (MainCategory) parent.getItemAtPosition(position);


                Toast.makeText(getContext(), itemId+"____"+"__"+root, Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:1)

您正在寻找:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String key = adapter.getRef(position).getKey()
}