如何填充listview表单firebase

时间:2016-11-12 05:16:40

标签: android listview firebase firebase-realtime-database

我是Firebase的新手,我已经按照本教程进行操作了 Firecasts #2

我有这样的错误

FATAL EXCEPTION: main
Process: com.xenolang.xenolang, PID: 20453
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
    ...
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
    ...

这是我的 object_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="8dp"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/object_card"
        android:layout_width="100dp"
        android:layout_height="130dp"
        app:cardElevation="2dp"
        app:cardMaxElevation="4dp"
        app:cardBackgroundColor="@color/colorPrimary"
        app:cardCornerRadius="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <TextView
                android:id="@+id/text_native"
                style="@style/TextAppearance.AppCompat.Large"
                android:textStyle="bold"
                android:textIsSelectable="false"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="@color/colorText"/>

            <TextView
                android:id="@+id/text_romanization"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="false"
                android:gravity="center"
                android:textColor="@color/colorText"/>

            <TextView
                android:id="@+id/text_translation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="false"
                android:gravity="center"
                android:textColor="@color/colorText"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

Object.class

public class Object {
    private String nat;
    private String rom;
    private String trans;

    public Object() {}

    public Object(String nat, String rom, String trans) {
        this.nat = nat;
        this.rom = rom;
        this.trans = trans;
    }

    public String getNat() {
        return nat;
    }

    public String getRom() {
        return rom;
    }

    public String getTrans() {
        return trans;
    }
}

Fragment.java

public class VowelsFragment extends Fragment {

    public VowelsFragment() {}

    private final FirebaseDatabase dbInstance = FirebaseDatabase.getInstance();
    private final DatabaseReference mRootRef = dbInstance.getReference();
    private final DatabaseReference mVowelRef = mRootRef.child(Urls.VOWEL_URL);
    private ArrayList<Object> mObjects = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View vowelView = inflater.inflate(R.layout.fragment_basic, container, false);
        final ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(getContext(), R.layout.object_layout, mObjects);
        GridView gridVowels = (GridView) vowelView.findViewById(R.id.grid_basic);
        gridVowels.setAdapter(adapter);

        mVowelRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Object objectValue = dataSnapshot.getValue(Object.class);
                mObjects.add(objectValue);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return vowelView;
    }
}

那么,你能告诉我在哪里陷入困境吗?因为我觉得我已经彻底遵循了指示。

1 个答案:

答案 0 :(得分:1)

将firebase-ui添加到您的项目(https://github.com/firebase/FirebaseUI-Android#installation),然后使用:FirebaseListAdapter或FirebaseRecyclerViewAdapter