无法通过从Firebase数据库获取数据来更新GridView

时间:2017-09-14 07:24:40

标签: java android android-arrayadapter android-gridview

我刚刚开始学习android studio大约一个月。对不起,如果我以错误的格式发布了这个或者问错了问题。我在OverviewPage上有一个GridView,它有一个扩展ArrayAdapter的自定义适配器。我有一个GridItem类,其中包含我希望在每个网格中显示的对象。我想做的是每次在数据库中创建一个新子项时更新我的​​GridView以获得一个新网格。

这是我的GridItem类。

package com.example.android.testlogin;

import java.io.Serializable;


public class GridItem implements Serializable {

int mImageView;
String mTitleHere;
String mPriceHere;

public GridItem(int ImageView, String titleHere, String priceHere) {
    mImageView = ImageView;
    mTitleHere = titleHere;
    mPriceHere = priceHere;
}

public int getmImageView() {
    return mImageView;
}

public void setmImageView(int ImageView) {
    mImageView = ImageView;
}

public String getmTitleHere() {
    return mTitleHere;
}

public void setmTitleHere(String TitleHere) {
    mTitleHere = TitleHere;
}

public String getmPriceHere() {
    return mPriceHere;
}

public void setmPriceHere(String PriceHere) {
   mPriceHere = PriceHere;
}
}

这是我的自定义适配器。

package com.example.android.testlogin;


public class GridAdapter extends ArrayAdapter<GridItem> {

public GridAdapter(Activity context, ArrayList<GridItem> packageName){

    super(context ,0, packageName);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
    }


    GridItem currentItem = getItem(position);


    ImageView putPic = (ImageView) listItemView.findViewById(R.id.imageHere);
    TextView putTitle = (TextView) listItemView.findViewById(R.id.titleHere);
    TextView putPrice = (TextView)listItemView.findViewById(R.id.per_price);

    //Set Text for TextView and ImageResources for ImageView.
    putPic.setImageResource(currentItem.getmImageView());
    putTitle.setText(String.valueOf(currentItem.getmTitleHere()));
    putPrice.setText(String.valueOf("$: " + currentItem.getmPriceHere()));


    // Return the whole list item layout (containing 2 TextViews and an ImageView)
    // so that it can be shown in the ListView
    return listItemView;


}


}

这是我的OverviewPage,在用户登录时充当主页面。用户应该能够看到所有创建的网格。

package com.example.android.testlogin;



public class OverviewPage extends AppCompatActivity {

private GridView lvPackage;
private GridAdapter adapter;
private FirebaseAuth mAuth1;
private FirebaseAuth.AuthStateListener mAuthListener1;
private DatabaseReference mDatabaseReference;




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


    lvPackage = (GridView) findViewById(R.id.grid);
    mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("comPostsCopy");





    mAuth1 = FirebaseAuth.getInstance();
    mAuthListener1 = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        if (firebaseAuth.getCurrentUser() == null) {

            startActivity(new Intent(OverviewPage.this , MainActivity.class));
        }

        }
    };



    //Add sample data for grid;


    final ArrayList<GridItem> elements = new ArrayList<GridItem>();

    elements.add(new GridItem(R.mipmap.ic_launcher_round, "Hello there", "120"));
    elements.add(new GridItem(R.mipmap.ic_launcher_round, "Bare Bears", "50"));
    elements.add(new GridItem(R.mipmap.ic_launcher_round, "Not Fun", "50"));


    mDatabaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //Loop Through Children
            for(DataSnapshot ds : dataSnapshot.getChildren());


            *//Read Data from Firebase Database and get required data to be shown on new grid.*



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    //Initialize Adapter

    adapter = new GridAdapter(this, elements );
    lvPackage.setAdapter(adapter);

    lvPackage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Do something
            Toast.makeText(OverviewPage.this, "Not yet", Toast.LENGTH_SHORT).show();
        }
    });

}


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

    mAuth1.addAuthStateListener(mAuthListener1);
}


}

这是我的Firebase数据库

Firebase Database

我只想得到例如子键&#34; title&#34;和&#34; pricePerGuest&#34;显示在我的新网格上。

我知道我必须遍历comPostsCopy孩子中的每个孩子。但是,我不知道如何从addValueEventListener方法中添加的子节点获取一些特定数据。

0 个答案:

没有答案