使用Firebase实现自定义列表适配器

时间:2016-11-14 05:30:15

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

我想将包含文字和图片的自定义适配器与Firebase相关联,以便从Firebase中检索listView项目中的(文字,图片)。

这是我目前的代码:

// Get ListView object from xml
    final ListView listView = (ListView) findViewById(R.id.listView);

    // Create a new Adapters
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            R.layout.custom_list_view_item, R.id.text);
    final ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, R.id.text);
    final ArrayAdapter<String> adapter3 = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, R.id.text);


    listView.setAdapter(adapter);



    // Enabling Offline
    if (Firebase.getDefaultConfig().isPersistenceEnabled() == false)
        Firebase.getDefaultConfig().setPersistenceEnabled(true);
    // Use Firebase
    Firebase.setAndroidContext(this);

    new Firebase(firebaseURL)
            .addChildEventListener(new ChildEventListener() {
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    adapter.add((String) dataSnapshot.child("text").getValue());
                    adapter2.add((String) dataSnapshot.child("image0").getValue());
                    adapter3.add((String) dataSnapshot.child("image1").getValue());

                }

                public void onChildRemoved(DataSnapshot dataSnapshot) {
                    adapter.remove((String) dataSnapshot.child("text").getValue());
                    adapter2.remove((String) dataSnapshot.child("image0").getValue());
                    adapter3.remove((String) dataSnapshot.child("image1").getValue());

                }

                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                }

                public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                }

                public void onCancelled(FirebaseError firebaseError) {
                }
            });
    // Open details when items when clicked
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                                final int position, long id) {

            new Firebase(firebaseURL)
                    .orderByChild("text")
                    .equalTo((String) listView.getItemAtPosition(position))
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.hasChildren()) {
                                DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
                                firstChild.getRef();
                                String d = adapter.getItem(position);
                                String image0 = adapter2.getItem(position);
                                String image1 = adapter3.getItem(position);


                                Intent i = new Intent(List_mall.this, Details_malls.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.putExtra("text", d);
                                i.putExtra("image0", image0);
                                i.putExtra("image1", image1);

                                startActivity(i);

                            }
                        }

                        public void onCancelled(FirebaseError firebaseError) {
                        }
                    });
        }
    });

0 个答案:

没有答案