将Firebase数据显示在“片段”选项卡中的自定​​义适配器视图中

时间:2017-08-21 22:51:46

标签: java android firebase android-recyclerview

这是我的主要活动中我的Fragment类的编辑版本。有一些错误 - 它没有显示任何内容,它不再将数据保存到数据库中。

 public static class DummyFragment extends Fragment {
    int color;

    public DummyFragment() {
    }

    @SuppressLint("ValidFragment")
    public DummyFragment(int color) {
        this.color = color;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);
        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);
        final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
        frameLayout.setBackgroundColor(color);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        DatabaseReference mDatabaseGig;
        final List<Dessert> dessertList;
        // get the gig database
        mDatabaseGig = FirebaseDatabase.getInstance().getReference("Gig Posts");
        dessertList = new ArrayList<>();

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

               // dessertList.clear();

                for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
                    Dessert dessert = gigSnapshot.getValue(Dessert.class);
                    dessertList.add(dessert);
                }
                DessertAdapter adapter = new DessertAdapter(getContext());
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });




        // possible to put progress dialogue



        return view;
    }
}

我已经完全保存了firebase并且我使用了MVC模型 - 我创建了一个适配器,一个模型并将其连接到主要活动但我的问题是如何将此模型用于onAddValueEventChangedListener( );

 public class DessertAdapter extendsRecyclerView.Adapter<DessertAdapter.DessertVh> {

private List<Dessert> desserts = new ArrayList<>();
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;

private Context context;

 // TODO: placeholder stuff here

@Override
public int getItemViewType(int position) {
    if (desserts.isEmpty()) {
        return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
    } else {
        return VIEW_TYPE_OBJECT_VIEW;
    }
}
public DessertAdapter(Context context) {
    this.context = context;
    this.desserts = desserts;

     desserts = Dessert.prepareDesserts(
            context.getResources().getStringArray(R.array.dessert_names),
            context.getResources().getStringArray(R.array.dessert_descriptions),
            context.getResources().getStringArray(R.array.dessert_amounts));
}

// TODO: another placeholder stuff here
@Override
public DessertVh onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_dessert, parent, false);
    return new DessertAdapter.DessertVh(view);
}

@Override
public void onBindViewHolder(DessertVh holder, int position) {
    Dessert dessert = desserts.get(position);

    holder.mName.setText(dessert.getName());
    holder.mDescription.setText(dessert.getDescription());
    holder.mFirstLetter.setText(String.valueOf(dessert.getFirstLetter()));
    holder.mPrice.setText(String.valueOf(dessert.getAmount()));

}

@Override
public int getItemCount() {
    // if nothing, return null,
    // else return the number of items in the list
    return desserts == null ? 0 : desserts.size();
}

public static class DessertVh extends RecyclerView.ViewHolder {

    private TextView mName;
    private TextView mPrice;
    private TextView mDescription;
    private TextView mFirstLetter;

    public DessertVh(View itemView) {
        super(itemView);

        mName = (TextView) itemView.findViewById(R.id.txt_name);
        mPrice = (TextView) itemView.findViewById(R.id.txt_price);
        mDescription = (TextView) itemView.findViewById(R.id.txt_desc);
        mFirstLetter = (TextView) itemView.findViewById(R.id.txt_firstletter);
    }
}
}

显示标签的主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs_header);
    // get the gig database
    mDatabaseGig = FirebaseDatabase.getInstance().getReference("Gig Posts");
    dessertList = new ArrayList<>();

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this , this )
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

这处理数据的显示

@Override
public void onStart(){
    super.onStart();
    mFirebaseAuth.addAuthStateListener(firebaseAuthListener);

    // load the data from database here
    mDatabaseGig.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            dessertList.clear();

            for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
                Dessert dessert = gigSnapshot.getValue(Dessert.class);
                dessertList.add(dessert);
            }
            // maybe this will work?
            DummyFragment dummyFragment = new DummyFragment();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

这里缺少一些代码,但仍然是mainactivity的一部分 这是主要活动中的Dummy Fragment Class

public static class DummyFragment extends Fragment {
    int color;

    public DummyFragment() {
    }

    @SuppressLint("ValidFragment")
    public DummyFragment(int color) {
        this.color = color;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);

        final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
        frameLayout.setBackgroundColor(color);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        // possible to put progress dialogue
        DessertAdapter adapter = new DessertAdapter(getContext());
        recyclerView.setAdapter(adapter);

        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

将Firebase代码放在Fragment中,而不是Activity中,并将dessertList作为参数传递给DessertAdapter。

示例:

void DisplayFileSystemWatcherInfo(System.IO.WatcherChangeTypes watcherChangeTypes, string name, string oldName = null)
        {    
            Dispatcher.BeginInvoke(new Action(() => { txtContent.Text= System.IO.File.ReadAllText("filePath"); }));    
        }