将数据从Firebase添加到Android中的ListView。

huangapple go评论75阅读模式
英文:

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.
将数据从Firebase添加到Android中的ListView。

code here:

public class Breakfast extends AppCompatActivity {
private Button button1;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private ListView listView;
private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
private ArrayAdapter&lt;String&gt; 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(&quot;food&quot;).child(currentDate);
listView = (ListView)findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter&lt;String&gt;(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 + &quot;.&quot; + 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)&#160;
at android.widget.AbsListView.obtainView(AbsListView.java:2491)&#160;
at android.widget.ListView.makeAndAddView(ListView.java:2018)&#160;
at android.widget.ListView.fillDown(ListView.java:721)&#160;
at android.widget.ListView.fillFromTop(ListView.java:782)&#160;
at android.widget.ListView.layoutChildren(ListView.java:1750)&#160;
at android.widget.AbsListView.onLayout(AbsListView.java:2249)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)&#160;
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)&#160;
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)&#160;
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)&#160;
at android.widget.ScrollView.onLayout(ScrollView.java:1562)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)&#160;
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)&#160;
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)&#160;
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)&#160;
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)&#160;
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)&#160;
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)&#160;
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)&#160;
at com.android.internal.policy.DecorView.onLayout(DecorView.java:751)&#160;
at android.view.View.layout(View.java:17838)&#160;
at android.view.ViewGroup.layout(ViewGroup.java:5754)&#160;
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2698)&#160;
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2371)&#160;
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)&#160;
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6960)&#160;
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)&#160;
at android.view.Choreographer.doCallbacks(Choreographer.java:709)&#160;
at android.view.Choreographer.doFrame(Choreographer.java:644)&#160;
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)&#160;
at android.os.Handler.handleCallback(Handler.java:836)&#160;
at android.os.Handler.dispatchMessage(Handler.java:103)&#160;
at android.os.Looper.loop(Looper.java:208)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6267)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)&#160;

答案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&lt;String&gt;(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.

huangapple
  • 本文由 发表于 2020年9月7日 01:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定