【问题标题】:Firebase ListViewFirebase 列表视图
【发布时间】:2016-09-17 19:20:41
【问题描述】:
n = ref.child(key).child("customer");

n.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        getData(dataSnapshot);
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        getData(dataSnapshot);
    }

    public void onChildRemoved(DataSnapshot dataSnapshot) { }
    public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
    public void onCancelled(FirebaseError firebaseError) { }
});

getData 函数看起来像这样。

private void getData(DataSnapshot ds){
  arrayList.clear();
  for(DataSnapshot data:ds.getChildren()){
    People p = new People();
    p.setName(data.getValue(People.class).getName());
    arrayList.add(p.getName());
  }

  if(arrayList.size()>0){
    ArrayAdapter a = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arrayList);
    listview.setAdapter(a);
  }
}

它正在显示

不幸的错误并关闭程序(应用程序崩溃)

我哪里做错了?在customer key下有多个push生成的唯一key,我想获取ListView里面的数据(名字)。

这是我的日志

PID: 2906 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:‌​185 at com.example.ahmedimam.firebase2.MainActivity.getData(MainAct‌​ivity.java:151)

MainActivity 151 指向

p.setName(data.getValue(People.class).getName());

如果我将其更改为 p.setName(data.getValue().toString); - 它可以正常工作,但它会显示最后添加的带有完整子项的密钥,我只想获取每个自动生成的密钥的名称..

【问题讨论】:

  • 社区最近讨论了the addition of urgent begging to questions,并决定更喜欢没有此类请求的帖子。
  • 你得到什么错误?
  • PID: 2906 com.firebase.client.FirebaseException: 无法在 com.firebase.client.DataSnapshot.getValue (DataSnapshot.java:185 at com.example.ahmedimam.firebase2.MainActivity 处反弹输入) .getData(MainActivity.java:151)
  • MainActivity 151 指向 p.setName(data.getValue(People.class).getName());如果我将其更改为 p.setName(data.getValue().toString);它可以工作,但它显示了最后添加的带有完整子项的密钥,我只想获取每个自动生成的密钥的名称..

标签: android listview firebase firebase-realtime-database


【解决方案1】:

这是在列表视图中显示 firebase 子项的正确且经过测试的代码...

public class MainActivity extends AppCompatActivity {
ListView mListView;
private ArrayList<String> mArraylistSectionLessons = new ArrayList<>();
public static String clickedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Users");

    // List Views
    mListView = (ListView) findViewById(R.id.listView);
    final ArrayAdapter<String> arrayAdapterLessons = new ArrayAdapter<String>(this, R.layout.list_view,
            R.id.label, mArraylistSectionLessons);
    mListView.setAdapter(arrayAdapterLessons);

    // Getting All Keys From Firebase Query
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot child : dataSnapshot.getChildren()) {

                String value = child.getKey();
                mArraylistSectionLessons.add(value);
                arrayAdapterLessons.notifyDataSetChanged();

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }

    });
    // listening to single list item on click
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            clickedItem =(String) (mListView.getItemAtPosition(position));
            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), SecondActivity.class);


            // sending data to new activity
            i.putExtra(clickedItem, clickedItem);

            startActivity(i);
        }
    });
}

}

资源:“Master Android”应用程序

【讨论】:

  • 这个答案可能会回答这个问题,但这更像是从 Firebase 数据库中检索数据的通用答案。请更具体。
  • 这是我真正找到的最佳答案...但要了解更多信息,您可以查看我在其中找到此代码的“Master Android”应用程序。谢谢
【解决方案2】:

您的getData 函数应如下所示。

private void getData(DataSnapshot ds){
  arrayList.clear();
  for(DataSnapshot data : ds.getChildren()){
    People p = new People();
    p = data.getValue(People.class);

    arrayList.add(p.getName());
  }

  if(arrayList.size() > 0){
    ArrayAdapter a = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arrayList);
    listview.setAdapter(a);
  }
}

People 类的属性设为public

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多