ArrayAdapter 未从 Firebase 读取数据(我的应用程序关闭)

ArrayAdapter is not reading data from Firebase (my app closes)

我正在使用 Firebase 制作一个 android 应用程序,并且可以在数据库中写入数据,但是读取我在数据库中写入的数据时我被卡住了(我对 ArrayAdapter 没有太多经验, Firebase 而不是 Android)。

当我从 phone 这个包含从 Firebase 读取的片段打开时(注意:写入数据在另一个片段中),我的应用程序关闭。不确定是否与此有关 - 我如何实施和使用 ArrayAdapter - 我如何实施 Firebase 读取 - 或者我已经更新到 Android studio(最新版本 SDK 26.0.0) - 或任何其他我看不到的原因。

我遇到了一些错误: - 我得到了下面的错误,我用下面的代码修复了错误 (configurations.all...)。当我从 Firebase 添加依赖项时出现此错误。

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0) from [com.android.support:design:26.0.0] AndroidManifest.xml:28:13-35
    is also present at [com.android.support:cardview-v7:25.4.0] AndroidManifest.xml:25:13-35 value=(25.4.0).
    Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}

所以这是我在需要显示读取数据的片段中的代码:

public class NewFlinderFragment extends Fragment {

    private DatabaseReference mMyFlindersDatabaseReference;
    private ChildEventListener mChildEventListener;
    private MyFlinderAdapter mMyFlinderAdapter;
    private ListView mMyFlinderListView;
    private MyFlinder myFlinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.new_flinder, container, false);
        mMyFlinderListView = (ListView) v.findViewById(R.id.my_flinders_list_view);

        //instantiate the reference to MyFlinders in Firebase
        mMyFlindersDatabaseReference = MainActivity.mFirebaseDatabase.getReference().child("MyFlinders");

        List<MyFlinder> myFlindersList = new ArrayList<>();
        mMyFlinderAdapter = new MyFlinderAdapter(getContext(), R.layout.my_flinder, myFlindersList);

        MyFlinder myFlinder = new MyFlinder(1,"flinderName",100,1,"Url");
        mMyFlinderAdapter.add(myFlinder);


        mMyFlinderListView.setAdapter(mMyFlinderAdapter);
        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Toast.makeText(getActivity(), "Boo", Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(), "difszmfcresg", Toast.LENGTH_SHORT).show();
                MyFlinder myFlinder = dataSnapshot.getValue(MyFlinder.class);
                mMyFlinderAdapter.add(myFlinder);
            }

            @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) {

            }
        };
        mMyFlindersDatabaseReference.addChildEventListener(mChildEventListener);

        return v;
    }

}

我在 MainActivity 中为整个应用程序初始化的数据库(再次注意,写入数据库是有效的,并且在此处未列出的另一个片段中)。

public class MainActivity extends AppCompatActivity  {
    public static FirebaseDatabase mFirebaseDatabase;
    // variables for authentication with Firebase
    public static final int RC_SIGN_IN = 1;
    static FirebaseAuth mFirebaseAuth;
    static FirebaseAuth.AuthStateListener mAuthStateListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        //instantiate the authentication from Firebase
        mFirebaseAuth = FirebaseAuth.getInstance();
    .....

这是包含列表视图的展开布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="24dp"
        android:paddingTop="24dp"
        android:text="@string/choose_a_flinder_to_grow"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold" />


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

        <ListView
            android:id="@+id/my_flinders_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@android:color/transparent"
            android:stackFromBottom="true"
            android:transcriptMode="alwaysScroll"
            tools:listitem="@layout/my_flinder" />
    </LinearLayout>

</LinearLayout>

这是列表视图的独立部分 (my_flinder):

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_flinder_linear_layout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="24dp"
    android:gravity="center">

    <ImageView
        android:id="@+id/my_flinder_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_no_text_white"
        android:scaleType="centerInside" />
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/my_flinder_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/Blue"
            android:paddingStart="18dp"
            android:paddingTop="20dp"
            android:paddingEnd="6dp"
            android:paddingBottom="6dp"
            android:textSize="20dp"/>

        <TextView
            android:id="@+id/my_flinder_points"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:paddingStart="18dp"
            android:paddingTop="6dp"
            android:paddingEnd="6dp"
            android:paddingBottom="6dp"
            android:textSize="20dp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/my_flinder_money"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/Pink"
        android:paddingStart="20dp"
        android:paddingTop="20dp"
        android:paddingEnd="6dp"
        android:paddingBottom="6dp"
        android:textSize="20dp"/>

</LinearLayout>

这是我的 MyFlinderAdapter class:

public class MyFlinderAdapter extends ArrayAdapter<MyFlinder>{
    public MyFlinderAdapter (Context context, int resource, List<MyFlinder> objects){
        super(context, resource, objects);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.my_flinder, parent, false);
        }

        ImageView myFlinderImage = (ImageView) convertView.findViewById(R.id.my_flinder_image);
        TextView myFlinderName = (TextView) convertView.findViewById(R.id.my_flinder_name);
        TextView myFlinderPoints = (TextView) convertView.findViewById(R.id.my_flinder_points);
        TextView myFlinderMoney = (TextView) convertView.findViewById(R.id.my_flinder_money);

        MyFlinder myFlinder = getItem(position);

        myFlinderImage.setVisibility(View.VISIBLE);
        myFlinderName.setVisibility(View.VISIBLE);
        myFlinderPoints.setVisibility(View.VISIBLE);
        myFlinderMoney.setVisibility(View.VISIBLE);

        myFlinderName.setText(myFlinder.getFlinderName());
        myFlinderPoints.setText(myFlinder.getFlinderRequiredPoints());
        myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());

        return convertView;
    }
}

myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());
该代码段可能会导致问题。由于您提供 int,setText 方法假定您提供的 int 是资源 ID。所以它寻找该资源但找不到。你应该把 String 放在那里。因此,它给出了以下错误:

07-29 14:27:42.552 4631 4631 E AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x64

希望对您有所帮助!