就像标题所说,我有一个带有自定义适配器的ListView,它从firebase数据库获取数据。
这是我的适配器
public class CustomBolichesAdapter extends BaseAdapter {
Context c;
ArrayList<Boliche> boliches;
public CustomBolichesAdapter(ArrayList<Boliche> boliches,Context c) {
this.c = c;
this.boliches = boliches;
}
@Override
public int getCount() {
return boliches.size();
}
@Override
public Object getItem(int position) {
return boliches.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(c).inflate(R.layout.boliches_card_model,parent,false);
}
TextView nombreBoliche =(TextView)convertView.findViewById(R.id.boliches_nombreTxt);
TextView ciudadBoliche = (TextView)convertView.findViewById(R.id.boliches_ciudadTxt);
ImageView cardImageBoliche = (ImageView)convertView.findViewById(R.id.boliches_card_image);
final Boliche boliches = (Boliche)this.getItem(position);
String url = boliches.getUrlCard();
nombreBoliche.setText(boliches.getNombre());
ciudadBoliche.setText(boliches.getCiudad());
Glide
.with(c)
.load(url)
.into((cardImageBoliche));
convertView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
openDetailActivity(boliches.getNombre(),boliches.getDescripcion(),boliches.getUrlHeaderBoliches(),boliches.getUrlViernes(),boliches.getUrlSabado());
}
});
return convertView;
}
这是我的活动
public class BolichesFragment extends Fragment {
DatabaseReference db;
FirebaseHelper helper;
CustomBolichesAdapter adapter;
ListView lv;
public BolichesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.contenido_boliches, container, false);
//Iniciar ListView
lv = (ListView) rootView.findViewById(R.id.lv);
//Iniciar FIREBASE DB
db = FirebaseDatabase.getInstance().getReference("Boliches");
helper = new FirebaseHelper(db);
//ADAPTER
adapter = new CustomBolichesAdapter(helper.retrieve(),getActivity());
lv.setAdapter(adapter);
return rootView;
}
}
,这是我的firebase助手
public class FirebaseHelper {
DatabaseReference db;
ArrayList<Boliche> boliches = new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
boliches.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Boliche boliche = ds.getValue(Boliche.class);
boliches.add(boliche);
}
}
//READ BY HOOKING ONTO DATABASE OPERATION CALLBACKS
public ArrayList<Boliche> retrieve()
{
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return boliches;
}
}
我希望有人可以帮助我,因为在控制台中没有错误,我只是不知道在哪里寻找。
答案 0 :(得分:2)
adapter.notifyDataSetChanged();
此方法通知对适配器的更改并更新ListView。每次将项目添加到列表时都应该调用它:
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
adapter.notifyDataSetChanged();
}