java 选项卡布局中的Firebase数据显示回收视图

dy1byipe  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(119)

我是android firebase的新手。我的问题是。第一个是将来自Activity的数据分发到几个Fragment。到目前为止我已经解决了这个问题。但我有一个要求,即在每个Fragment中显示Firebase数据库中的数据。但问题是,在将数据输入到firebase中后,应用程序停止。

标准收件人活动

public class stdAttnDisplay extends AppCompatActivity {

    private ViewPager pager;
    private TabLayout tabs;

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

        pager=(ViewPager) findViewById(R.id.vpager);
        tabs=(TabLayout) findViewById(R.id.tablayout1);
        tabs.setupWithViewPager(pager);
        tabs.setTabGravity(TabLayout.GRAVITY_FILL);
        tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        setupViewPager(pager);

    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        //bundle
        Bundle bundle =new Bundle();
        bundle.putString("DATE","01-05-2023");

        IABF_Attnfrag frag_iabf = new IABF_Attnfrag();
        DABF_Attnfrag frag_dabf = new DABF_Attnfrag();
        CMA_Attnfrag frag_cma = new CMA_Attnfrag();
        CHARTERED_Attnfrag frag_chartered = new CHARTERED_Attnfrag();
        ENGLISH_Attnfrag frag_english = new ENGLISH_Attnfrag();
        AAT1_Attnfrag frag_aat1 = new AAT1_Attnfrag();
        AAT2_Attnfrag frag_aat2 = new AAT2_Attnfrag();
        AAT3_Attnfrag frag_aat3 = new AAT3_Attnfrag();

        frag_iabf.setArguments(bundle);
        frag_dabf.setArguments(bundle);
        frag_cma.setArguments(bundle);
        frag_chartered.setArguments(bundle);
        frag_english.setArguments(bundle);
        frag_aat1.setArguments(bundle);
        frag_aat2.setArguments(bundle);
        frag_aat3.setArguments(bundle);

        adapter.addFrag(frag_iabf,"IABF");
        adapter.addFrag(frag_dabf,"DABF");
        adapter.addFrag(frag_cma,"CMA");
        adapter.addFrag(frag_chartered,"CHARTERED");
        adapter.addFrag(frag_english,"ENGLISH");
        adapter.addFrag(frag_aat1,"AAT-i");
        adapter.addFrag(frag_aat2,"AAT-ii");
        adapter.addFrag(frag_aat3,"AAT-iii");

        viewPager.setAdapter(adapter);

    }

}

碎片

public class IABF_Attnfrag extends Fragment {

    String dateTxt;
    private Bundle bundle;
    myadapter adapter;
    RecyclerView recViewiabf;
    private DatabaseReference propertyRef;

    public IABF_Attnfrag() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_i_a_b_f__attnfrag, container, false);

        recViewiabf = (RecyclerView) view.findViewById(R.id.recViewiabf);
        bundle = this.getArguments();
        dateTxt = bundle.getString("DATE");
        Log.e("DATE_FRAG_INBOUND",""+dateTxt);

        propertyRef = FirebaseDatabase.getInstance()
                .getReference()
                .child("attendance")
                .child("IABF")
                .child(dateTxt);
        propertyRef.keepSynced(true);

        recViewiabf.setLayoutManager(new LinearLayoutManager(getContext()));
        FirebaseRecyclerOptions<modelAttn> options =
                new FirebaseRecyclerOptions.Builder<modelAttn>()
                        .setQuery(propertyRef, modelAttn.class)
                        .build();

        adapter = new myadapter(options);
        recViewiabf.setAdapter(adapter);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

适配器

public class myadapter extends FirebaseRecyclerAdapter<modelAttn,myadapter.myviewholder>
{

    public myadapter(@NonNull FirebaseRecyclerOptions<modelAttn> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull modelAttn model) {
        holder.stdFulName.setText(model.getName());
        holder.stdidtv.setText(model.getSt_id());

    }

    @NonNull
    @Override
    public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.attn_item,parent,false);
        return new myviewholder(view);
    }

    public class myviewholder extends RecyclerView.ViewHolder {
        TextView stdFulName, stdidtv;
        public myviewholder(@NonNull View itemView) {
            super(itemView);
            stdidtv = itemView.findViewById(R.id.indextext);
            stdFulName = itemView.findViewById(R.id.nametext);

        }
    }

}

型号联系人

public class modelAttn {
    String Name, st_id;

    public modelAttn() {
    }

    public modelAttn(String name, String st_id) {
        this.Name = name;
        this.st_id = st_id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getSt_id() {
        return st_id;
    }

    public void setSt_id(String st_id) {
        this.st_id = st_id;
    }
}

FireBase数据库屏幕截图

Database Screenshot
我需要的是将数据从Activity分发到Fragment,然后使用Firebase数据库中的数据在RecyclerView中显示相关数据。

jyztefdp

jyztefdp1#

如果你创建了一个普通的适配器,然后传递给它一个你从firebase获取的自定义对象列表。你可以在你的MainActivity上创建一个静态列表,可以在任何地方引用,包括在片段中。

相关问题