英文:
Adding data from firebase to listview in android
问题
以下是你提供的内容的翻译部分:
我正在尝试将Firebase中的数据添加到Android中的ListView中。然而,在向ListView中添加数据时遇到了问题,并且当我打开页面时应用崩溃了。我试图显示的数据是用户在当前日期所吃的食物,就像这张图片中的用户吃了5个苹果和1个白面包。
代码如下:
public class Breakfast extends AppCompatActivity {
// ...(省略其他声明)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breakfast);
// ...(省略其他初始化代码)
listView = (ListView)findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, list);
listView.setAdapter(arrayAdapter);
// ...(省略其他代码)
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) {
String value = dataSnapshot.getValue(UserFoodInfo.class).toString();
list.add(value);
arrayAdapter.notifyDataSetChanged();
}
// ...(省略其他监听事件的方法)
});
}
}
用户食物类:
public class UserFoodInfo {
public String calories;
public String food_amount;
// ...(省略其他构造方法和方法)
public String toString(){
return this.calories + "." + food_amount;
}
}
崩溃日志:
E/ArrayAdapter: 必须为TextView提供资源ID
D/AndroidRuntime: 正在关闭虚拟机
E/AndroidRuntime: 致命异常: main
进程: com.example.test1, PID: 7639
java.lang.IllegalStateException: ArrayAdapter要求资源ID为TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
// ...(省略其他堆栈跟踪信息)
英文:
I'm trying to add data from firebase to listview in android. However, I'm having trouble adding data to the Listview and it crashes when I open the page. The data I'm trying to show is the food that was eaten by the user on the current date, like in this picture the user ate 5 apples and 1 white bread.
code here:
public class Breakfast extends AppCompatActivity {
private Button button1;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private ListView listView;
private ArrayList<String> list = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breakfast);
calendar = Calendar.getInstance();
final String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid()).child("food").child(currentDate);
listView = (ListView)findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, list);
listView.setAdapter(arrayAdapter);
final FirebaseUser user=firebaseAuth.getCurrentUser();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) {
String value = dataSnapshot.getValue(UserFoodInfo.class).toString();
list.add(value);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
user food class:
public class UserFoodInfo {
public String calories;
public String food_amount;
public UserFoodInfo() {
}
public UserFoodInfo(String calories, String food_amount){
this.calories = calories;
this.food_amount = food_amount;
}
public String getCalories() {
return calories;
}
public String getFood_amount() {
return food_amount;
}
public String toString(){
return this.calories + "." + food_amount;
}
crash log:
E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test1, PID: 7639
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
at android.widget.AbsListView.obtainView(AbsListView.java:2491)
at android.widget.ListView.makeAndAddView(ListView.java:2018)
at android.widget.ListView.fillDown(ListView.java:721)
at android.widget.ListView.fillFromTop(ListView.java:782)
at android.widget.ListView.layoutChildren(ListView.java:1750)
at android.widget.AbsListView.onLayout(AbsListView.java:2249)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.widget.ScrollView.onLayout(ScrollView.java:1562)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:751)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2698)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2371)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6960)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:644)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:388)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371) 
at android.widget.AbsListView.obtainView(AbsListView.java:2491) 
at android.widget.ListView.makeAndAddView(ListView.java:2018) 
at android.widget.ListView.fillDown(ListView.java:721) 
at android.widget.ListView.fillFromTop(ListView.java:782) 
at android.widget.ListView.layoutChildren(ListView.java:1750) 
at android.widget.AbsListView.onLayout(AbsListView.java:2249) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:321) 
at android.widget.ScrollView.onLayout(ScrollView.java:1562) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:321) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:321) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:321) 
at com.android.internal.policy.DecorView.onLayout(DecorView.java:751) 
at android.view.View.layout(View.java:17838) 
at android.view.ViewGroup.layout(ViewGroup.java:5754) 
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2698) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2371) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6960) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907) 
at android.view.Choreographer.doCallbacks(Choreographer.java:709) 
at android.view.Choreographer.doFrame(Choreographer.java:644) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893) 
at android.os.Handler.handleCallback(Handler.java:836) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:208) 
at android.app.ActivityThread.main(ActivityThread.java:6267) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
答案1
得分: 0
问题出在当您初始化您的arraAdapter
先生时,您需要进行更改:
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
因为R.layout.simple_list_item_2
需要两个文本视图来填充,而R.layout.simple_list_item_1
只需要一个文本视图。
英文:
the issue is in when you initliaze your arraAdapter sir, you need to uchange this
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
becasue R.layout.simple_list_item_2 require two textviews to populate whereas R.layout.simple_list_item_1 requires one textview.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论