我的应用程序从Firebase检索数据,然后应该在片段中显示它。在Firebase中有一个数据,但是当我启动我的应用程序时,这个重新为空,但是当我按下一个按钮然后返回时,应用程序会填充并显示数据的相关性。
主要活动使用片段,对于数据,我使用带有arraylist的自定义适配器
这是一个示例video
这是代码
class CustomAdapter extends BaseAdapter {
private Context c;
private ArrayList<Item> items;
CustomAdapter(Context c, ArrayList<Item> items) {
super();
this.c = c;
this.items = items;
Log.e("Hee",items.size()+"");
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
convertView= inflater.inflate(R.layout.model,parent,false);
}
final TextView titleTxt= (TextView) convertView.findViewById(R.id.titleTxt);
TextView priceTxt= (TextView) convertView.findViewById(R.id.priceTxt);
TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);
ImageView image = (ImageView) convertView.findViewById(imageView);
final Item s = (Item) this.getItem(position);
titleTxt.setText(s.getSale());
priceTxt.setText(s.getPrice()+"€");
descTxt.setText(s.getDescription());
String url = s.getUrl();
if(url != null){
Glide.with(c).load(url).into(image);
}else{
Glide.with(c).load("http://www.video-furn.com/images/icons/AVFI-Image-not-available-icon.jpg").into(image);
}
//ONITECLICK
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = "Select option";
CharSequence [] itemlist = {"Chiama", "Nuovo messaggio",};
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle(title);
builder.setItems(itemlist, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int witch) {
switch(witch){
case 0:
Intent phone_intent = new Intent(Intent.ACTION_DIAL);
phone_intent.setData(Uri.parse("tel:"+s.getPhone()));
c.startActivity(phone_intent);
break;
case 1:
Intent send_intent = new Intent(Intent.ACTION_VIEW);
send_intent.setData(Uri.parse("sms:"+s.getPhone()));
c.startActivity(send_intent);
break;
default:
break;
}
}
});
AlertDialog alert = builder.create();
alert.setCancelable(true);
alert.show();
}
});
return convertView;
}}
片段:
public class MainActivity_Fragment extends Fragment {
private DatabaseReference mDatabase;
private FirebaseHelper helper;
private ListView listView;
private CustomAdapter adapter;
private View view;
private Snackbar snackbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(mDatabase);
view = inflater.inflate(R.layout.activity_main_fragment, container, false);
adapter = new CustomAdapter(getActivity(), helper.retrieve());
FloatingActionButton fabButton = (FloatingActionButton) view.findViewById(R.id.fabButton);
listView = (ListView) view.findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
connectionState();
fabButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
addItem();
}
});
Log.e("list adapter--",""+adapter.getCount());
return view; }
答案 0 :(得分:0)
这可能是由于异步电话造成的。 所有firebase调用都是在后台进行的,并且您的视图创建在后台处理仍在进行时继续进行。
由于你的适配器依赖于一个项目列表,它确实创建了一个空列表。
实现此目的的一个好方法是在firebase处理完成后更新您的视图。这可以使用委托模式来完成。
https://www.tutorialspoint.com/design_pattern/business_delegate_pattern.htm
祝你好运