gpt4 book ai didi

java - 如何在 Android 中使用 MVC 模式从 firebase 检索数据?

转载 作者:太空狗 更新时间:2023-10-29 13:50:08 25 4
gpt4 key购买 nike

<分区>

我正在开发一个简单的 Android 项目,它可以从 Firebase 插入和检索数据。插入功能正常,说明项目已经成功连接到Firebase数据库。但是,检索部分不起作用。我做了很多测试,认为问题出在 FirebaseHelper 中,因为当我尝试在 Activity 中打印“firebasehelper.retrieveMajor()”的结果时,它什么也没显示。但是在FirebaseHelper中打印数据时确实显示了数据。您可以看到如下代码。

型号:

@IgnoreExtraProperties
public class Major {

public String major_id;
public String major_name;

public Major() {
}

public Major(String major_id, String major_name) {
this.major_id = major_id;
this.major_name = major_name;
}

public String getMajor_id() {
return major_id;
}

public String getMajor_name() {
return major_name;
}
}

适配器:

public class MajorListAdapter extends BaseAdapter {
Context context;
ArrayList<Major> majors;

public MajorListAdapter(Context context, ArrayList<Major> majors) {
this.context = context;
this.majors = majors;
}

@Override
public int getCount() {
return majors.size();
}

@Override
public Object getItem(int pos) {
return majors.get(pos);
}

@Override
public long getItemId(int pos) {
return pos;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {

if(convertView==null)
{
convertView= LayoutInflater.from(context).inflate(R.layout.model,viewGroup,false);
}

TextView tv_majorid= (TextView) convertView.findViewById(R.id.tx_majorid);
TextView tv_majorname= (TextView) convertView.findViewById(R.id.tx_majorname);

final Major major= (Major) this.getItem(position);

tv_majorid.setText(major.getMajor_id());
tv_majorname.setText(major.getMajor_name());

return convertView;
}

}

FirebaseHelper:

public class FirebaseHelper {

DatabaseReference db;
Boolean saved=null;
ArrayList<Major> majors = new ArrayList<>();

public FirebaseHelper(DatabaseReference db) {
this.db = db;
}

//Save the Major info. into db
public Boolean saveMajor(Major major)
{
if(major==null)
{
saved=false;
}else
{
try
{
db.child("Major").push().setValue(major);
saved=true;

}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}

return saved;
}

private void fetchDataFromMajor(DataSnapshot dataSnapshot) {
majors.clear();

for (DataSnapshot ds : dataSnapshot.getChildren()) {
Major major = ds.getValue(Major.class);
majors.add(major);
}

}

public ArrayList<Major> retrieveMajor() {

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

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

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


return majors;
}
}

检索数据并将数据与 ListView 绑定(bind)的 Activity:

public class MajorListActivity extends AppCompatActivity {

DatabaseReference db;
FirebaseHelper firebasehelper;
MajorListAdapter adapter;
ListView lv_MajorList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_major_list);

lv_MajorList = (ListView) findViewById(R.id.lv_MajorList);

db= FirebaseDatabase.getInstance().getReference();
firebasehelper=new FirebaseHelper(db);

//ADAPTER
adapter = new MajorListAdapter(getApplicationContext(),firebasehelper.retrieveMajor());

lv_MajorList.setAdapter(adapter);
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com