[英]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.我目前正在为 Android 构建一个应用程序,它从 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 java.lang.RuntimeException: 无法启动活动 ComponentInfo{app.name/app.name.retrieve}: java.lang.ClassCastException: android.widget.ListView 无法转换为 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(..){
:在您的onChildAdded(..){
更改此内容:
adapter.add(value);
to this:对此:
child.add(value);
add()
is a method in the ArrayList. add()
是 ArrayList 中的一个方法。
Check this for more info: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html查看更多信息: 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您不能将ListView
为List
, ArrayAdapter构造函数采用Context
实例和resourceId
以及List
或数组
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.