从Firebase实时数据库中读取数据

时间:2018-04-02 17:18:57

标签: android firebase firebase-realtime-database

我的数据库具有以下Json结构。我想在我的UI中阅读并显示学生的详细信息。我使用下面的代码来读取数据。但它没有用。有些人可以帮助我在学生数据发生变化或添加新学生时阅读和显示数据。

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get Firebase database reference
    this.mDatabase = FirebaseDatabase.getInstance().getReference().child("users");

    // Init user list
    ListView list = (ListView) this.findViewById(R.id.dataList);
    this.listAdapter = new DataListAdapter(this, R.layout.list_view_cell);
    list.setAdapter(listAdapter);
}

@Override
protected void onStart() {
    super.onStart();

    // Add listener
    mDatabase.addChildEventListener(new FirebaseEventListener(this.listAdapter));
}

FirebaseEventListener.java:

public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
    User newUser = new User((String) dataSnapshot.getValue(), (String) dataSnapshot.getKey());
    if (!(userList.getUserList().contains(newUser))) {
        userList.addUser(newUser);
        userList.notifyDataSetChanged();
    }
}

JSON:

 "users":{
  "Student1":{  
     "name":"Ada Lovelace",
     "id":"123"
  },
  "Student2":{  
     "name":"smith",
     "id":"870"
  }
  }

1 个答案:

答案 0 :(得分:0)

使用firebase list adapter。

CultureInfo cc = System.Threading.Thread.CurrentThread.CurrentCulture;
if (char.IsDigit(e.KeyChar) || e.KeyChar.ToString() == cc.NumberFormat.NumberDecimalSeparator || e.KeyChar.ToString() == cc.NumberFormat.NumberGroupSeparator)
{
    e.Handled = false;
}
else if (char.IsControl(e.KeyChar))
{
    e.Handled = false;
}
else
{
    e.Handled = true;
}

并在主要活动中称之为

 public class CompaniesFbAdapter extends FirebaseListAdapter<Company> {


/**
 * @param activity    The activity containing the ListView
 * @param modelClass  Firebase will marshall the data at a location into
 *                    an instance of a class that you provide
 * @param modelLayout This is the layout used to represent a single list item.
 *                    You will be responsible for populating an instance of the corresponding
 *                    view with the data from an instance of modelClass.
 * @param ref         The Firebase location to watch for data changes. Can also be a slice of a location,
 *                    using some combination of {@code limit()}, {@code startAt()}, and {@code endAt()}.
 */
public CompaniesFbAdapter(Activity activity, Class<Company> modelClass, int modelLayout, Query ref) {
    super(activity, modelClass, modelLayout, ref);
}

@Override
protected void populateView(View v, Company model, int position) {
    TextView name = (TextView) v.findViewById(R.id.name_txt);
    TextView category = (TextView) v.findViewById(R.id.category_txt);
    final CircleImageView imageView=v.findViewById(R.id.comp_img);
    name.setText(model.getName());
    category.setText(model.getCategory());
    Glide.with(mActivity)
            .load(model.getLogoUrl())
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    imageView.setImageBitmap(resource);
                }
            });
    //category.setText(model.getCategory());

}

 }