将数据从Firebase Real数据库加载到Gridview

时间:2018-09-28 16:06:29

标签: android android-gridview

当我想从Firebase检索数据并将其加载到GridView时遇到一些问题,但是直到我将手机从纵向旋转为横向时,数据才出现:

启动应用程序时此操作(数据未显示)

this when I start the app ( data doesn't appear )

当我将手机旋转到横向视图时,数据开始出现

when I rotate my phone to landscape view data start to appear

最后,当我将手机重新旋转到纵向视图时,数据加载成功

Finally, when I re-rotate my phone to portrait view, data loaded successfully

我尝试了一些方法来解决它(刷新网格视图gridview.notifyalldatachanged()),但是没有一个解决这个问题。

我的代码非常简单:创建新的自定义适配器并将其设置为gridview

public class ReceipesActivity extends AppCompatActivity {

GridView gridView;
ArrayList<String> RecipeImageUrl=new ArrayList<String>();
ArrayList<String> RecipeTitle=new ArrayList<String>();
DatabaseReference mdata;
MyCustomAdapter myCustomAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receipes);
    gridView = (GridView) findViewById(R.id.GridviewID);
    mdata = FirebaseDatabase.getInstance().getReference("Categories").child("انواع الخبز");
    LoadDataFromFirebase();
    myCustomAdapter = new MyCustomAdapter(RecipeImageUrl,RecipeTitle);
    gridView.setAdapter(myCustomAdapter);
}

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

//My Custom Adapter for GridView

public class MyCustomAdapter extends BaseAdapter {

    ArrayList<String> RecipeImageUrl;
    ArrayList<String> RecipeTitle;

    MyCustomAdapter(ArrayList<String> RecipeImageUrl,ArrayList<String> RecipeTitle) {
        Toast.makeText(ReceipesActivity.this, "hey to start", Toast.LENGTH_SHORT).show();
        this.RecipeImageUrl = RecipeImageUrl;
        this.RecipeTitle = RecipeTitle;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.grid_view_custom, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.GridRecipeImageID);
        TextView textView = (TextView) view.findViewById(R.id.GridRecipeTitleID);
        Toast.makeText(ReceipesActivity.this, "sdfgsfg" + RecipeTitle.get(position), Toast.LENGTH_SHORT).show();
        textView.setText(RecipeTitle.get(position));            Picasso.with(getApplicationContext()).load(RecipeImageUrl.get(position)).into(imageView);
        return view;
    }

}

public void LoadDataFromFirebase() {
    mdata.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                RecipeTitle.add(ds.child("title").getValue().toString());
                RecipeImageUrl.add(ds.child("image").getValue().toString());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
      });
    }
}

2 个答案:

答案 0 :(得分:1)

您是否在填充RecipeTitle和RecipeImageUrl列表之后尝试添加myCustomAdapter.notifyDataSetChanged()?

这里是一个例子:

public void LoadDataFromFirebase() {
    mdata.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                RecipeTitle.add(ds.child("title").getValue().toString());
                RecipeImageUrl.add(ds.child("image").getValue().toString());
            }
            myCustomAdapter.notifyDataSetChanged();  // THIS WILL NOTIFY YOUR ADAPTER WHEN DATA IS CHANGED
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

答案 1 :(得分:0)

尝试更改适配器以从活动类中进行单独的数据更新,然后将MyCustomAdapter类从ReceipesActivity类中移除:

  public class MyCustomAdapter extends BaseAdapter {

    ArrayList<String> RecipeImageUrlList;
    ArrayList<String> RecipeTitleList;

    public void addRecipeImageUrl(String RecipeImageUrl) {
        RecipeImageUrl.add(RecipeImageUrl);
        notifyDataSetChanged();
    }

    public void addRecipeTitle(String RecipeTitle) {
        RecipeTitleList.add(RecipeTitle);
        notifyDataSetChanged();
    }
    ...
}

并将您的LoadDataFromFirebase方法更改为:

 public void LoadDataFromFirebase() {
    mdata.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                myCustomAdapter.addRecipeTitle(ds.child("title").getValue().toString());
                myCustomAdapter.addRecipeImageUrl(ds.child("image").getValue().toString());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}