简体   繁体   中英

Android Studio Fails to Build Without Cast, But App Crashes With Cast

I'm currently building an app for Android which retrieves data from Firebase. It displays children from other children . Either it fails to build when I remove the cast or the app crashes with the cast. Here is the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{app.name/app.name.retrieve}: java.lang.ClassCastException: android.widget.ListView cannot be cast to java.util.List

This is my code:

package app.name;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.List;

public class datab extends AppCompatActivity {
        private ArrayList<String> child = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        @Override
        protected void onStart() {
            super.onStart();
            setContentView(R.layout.activity_datab);
            DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("child");
            final ListView view = findViewById(R.id.l);


            final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.list_content, (List) view);
//Here is the cast
            view.setAdapter(child);

            database.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    String value = dataSnapshot.getValue(String.class);
                    adapter.add(value);
                    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) {

                }
            });
        }
    }

How can I remove the cast without the build failing?

Change this:

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.list_content, (List) view);

to this:

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, child);
view.setAdapter(adapter);

Change this inside your onChildAdded(..){ :

  adapter.add(value);

to this:

  child.add(value);

add() is a method in the ArrayList.

Check this for more info: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

you can't cast ListView to List , ArrayAdapter constructor takes Context instance and resourceId and List or array

you should pass the list to your adapter , and set the adapter to the listview

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 , child);
view.setAdapter(adapter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM