如何将实时Firebase数据检索到片段

时间:2018-04-06 07:01:21

标签: android firebase firebase-realtime-database

enter image description here

我的片段如下 public class SearchBuyAndSellFragment extends Fragment {     private static final String TAG =" SearchBuyAndSellFragmen";

private static final int NUM_GRID_COLUMNS = 3;
private static final int GRID_ITEM_MARGIN = 5;

//widgets
private RecyclerView mRecyclerView;
private FrameLayout mFrameLayout;
private FirebaseRecyclerAdapter<BuyAndSell, BuyAndSellViewHolder>mBuyandsellAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search_buy_and_sell, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewbns);
    initialiseScreen();
    return view;
}

private void initialiseScreen() {
   mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
   setupAdapter();
}

private void setupAdapter() {
    mBuyandsellAdapter= new FirebaseRecyclerAdapter<BuyAndSell, BuyAndSellViewHolder>() {
        @Override
        protected void onBindViewHolder(BuyAndSellViewHolder holder, int position, BuyAndSell model) {

        }

        @Override
        public BuyAndSellViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    };
}

public static class BuyAndSellViewHolder extends RecyclerView.ViewHolder{
    public BuyAndSellViewHolder(View itemView) {
        super(itemView);
    }
}

}

模型是

公共类BuyAndSell {     private String catagory;

private String post_id;
private String user_id;
private String image;
private String title;
private String description;
private String price;
private String country;
private String state_province;
private String city;
private String contact_number;

public BuyAndSell(String catagory, String post_id, String user_id, String image, String title, String description,
                  String price, String country, String state_province, String city, String contact_number) {
    this.catagory = catagory;
    this.post_id = post_id;
    this.user_id = user_id;
    this.image = image;
    this.title = title;
    this.description = description;
    this.price = price;
    this.country = country;
    this.state_province = state_province;
    this.city = city;
    this.contact_number = contact_number;
}

public BuyAndSell() {

}

public String getCatagory() {
    return catagory;
}

public void setCatagory(String catagory) {
    this.catagory = catagory;
}

public String getPost_id() {
    return post_id;
}

public void setPost_id(String post_id) {
    this.post_id = post_id;
}

public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getState_province() {
    return state_province;
}

public void setState_province(String state_province) {
    this.state_province = state_province;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getContact_number() {
    return contact_number;
}

public void setContact_number(String contact_number) {
    this.contact_number = contact_number;
}

@Override
public String toString() {
    return "buy_and_sell{" +
            "catagory='" + catagory + '\'' +
            "post_id='" + post_id + '\'' +
            ", user_id='" + user_id + '\'' +
            ", image='" + image + '\'' +
            ", title='" + title + '\'' +
            ", description='" + description + '\'' +
            ", price='" + price + '\'' +
            ", country='" + country + '\'' +
            ", state_province='" + state_province + '\'' +
            ", city='" + city + '\'' +
            ", contact_number='" + contact_number  + '\'' +
            '}';
}

}

我的适配器是

公共类BuyAndSellListAdapter扩展了RecyclerView.Adapter {     private static final String TAG =&#34; BuyAndSellListAdapter&#34 ;;     private static final int NUM_GRID_COLUMNS = 3;

private ArrayList<BuyAndSell> mbuyandsells;
private Context mContext;

public class ViewHolder extends RecyclerView.ViewHolder{

    SquareImageView mPostImage;

    public ViewHolder(View itemView) {
        super(itemView);
        mPostImage = (SquareImageView) itemView.findViewById(R.id.post_image);

        int gridWidth = mContext.getResources().getDisplayMetrics().widthPixels;
        int imageWidth = gridWidth/NUM_GRID_COLUMNS;
        mPostImage.setMaxHeight(imageWidth);
        mPostImage.setMaxWidth(imageWidth);
    }
}

public BuyAndSellListAdapter(Context context, ArrayList<BuyAndSell> buyAndSell) {
    mbuyandsells = buyAndSell;
    mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    UniversalImageLoader.setImage(mbuyandsells.get(position).getImage(), holder.mPostImage);

    final int pos = position;
    holder.mPostImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: selected a post");
            //TODO

            //view the post in more detail
            Fragment fragment = (Fragment)((BuyAndSellActivity)mContext).getSupportFragmentManager()
                    .findFragmentByTag("android:switcher:" + R.id.viewpager_container1 + ":" +
                            ((BuyAndSellActivity)mContext).mViewPager.getCurrentItem());
            if(fragment != null){
                //SearchFragment (AKA #0)
                if(fragment.getTag().equals("android:switcher:" + R.id.viewpager_container1 + ":0")){
                    Log.d(TAG, "onClick: switching to: " + mContext.getString(R.string.fragment_view_post));

                    SearchBuyAndSellFragment searchFragment = (SearchBuyAndSellFragment) ((BuyAndSellActivity)mContext).getSupportFragmentManager()
                            .findFragmentByTag("android:switcher:" + R.id.viewpager_container1 + ":" +
                                    ((BuyAndSellActivity)mContext).mViewPager.getCurrentItem());

                    searchFragment.viewPost(mbuyandsells.get(pos).getPost_id());
                }
                //WishList Fragment (AKA #1)
                else if(fragment.getTag().equals("android:switcher:" + R.id.viewpager_container1 + ":1")){
                    Log.d(TAG, "onClick: switching to: " + mContext.getString(R.string.fragment_wish_list));

                    WishListFragment watchListFragment = (WishListFragment) ((BuyAndSellActivity)mContext).getSupportFragmentManager()
                            .findFragmentByTag("android:switcher:" + R.id.viewpager_container1 + ":" +
                                    ((BuyAndSellActivity)mContext).mViewPager.getCurrentItem());

                    watchListFragment.viewPost(mbuyandsells.get(pos).getPost_id());
                }
            }
        }
    });
}

@Override
public int getItemCount() {
    return mbuyandsells.size();
}

}

0 个答案:

没有答案