简体   繁体   中英

How can I hide some content from showing to recyclerview

I am working on code which I copied from somewhere as a learner. The app displays data in recyclerview, this data is coming from firebase database. All is working well, the data is displaying nicely in recyclerview.

My worry now is: i have added another recyclerview in the same activity. So now I have two recyclerviews with these IDs: recyclerview and recyclerview2. They are in one activity but in different layouts, and only one layout is visible at a time, and by clicking the button it hides one layout and shows another. For now the recyclerviews are showing same data.

What i want now is this: In these recyclerviews I have a TextView which changes text time by time. It sometimes shows this text 'CLOSED'. So i want all the data which has a textview which shows 'CLOSED' to be shown on recyclerview2 and will be unavailable on the first recyclerview.

So my issue is to make the data not to display on recyclerview (first one) if the textview is showing 'CLOSED' and i want the recyclerview2 to show only data which has a textview showing this text 'CLOSED'

I do not know where to start from. Below is my MainActivity.java where i want the code to be added.

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView, recyclerView2;
    private DatabaseReference mDatabase, vDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;


    TextView rtrratio, rtrratiopercentage, tppipss, slpipss, commenting, entry, tp, sl, tv1, tv2, tv3;
    RelativeLayout rSignals, rCalendar, rLessons, rServices, rContact;
    ImageButton imsignals, imcalendar, imlessons, imservices, imcontact;


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

        //defining main buttons (Image buttons)
        imsignals = (ImageButton) findViewById(R.id.im_signals_id);
        imcalendar = (ImageButton) findViewById(R.id.im_calendar_id);
        imlessons = (ImageButton) findViewById(R.id.im_lessons_id);
        imservices = (ImageButton) findViewById(R.id.im_services_id);
        imcontact = (ImageButton) findViewById(R.id.im_contact_id);


        //defining textviews
        entry = (TextView) findViewById(R.id.entry);
        tppipss = (TextView) findViewById(R.id.tpp);
        slpipss = (TextView) findViewById(R.id.slp);
        rtrratio = (TextView) findViewById(R.id.riskreward);
        rtrratiopercentage = (TextView) findViewById(R.id.rrewardp);
        commenting = (TextView) findViewById(R.id.comment);
        tp = (TextView) findViewById(R.id.tp);
        sl = (TextView) findViewById(R.id.sl);


        //bottom navigation
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        //button definitions
        Button btnRunning = (Button) findViewById(R.id.sRunning);
        Button btnPending = (Button) findViewById(R.id.sPending);
        Button btnClosed = (Button) findViewById(R.id.sClosed);

        //layouts definitions for bottom navigation
        rSignals = (RelativeLayout) findViewById(R.id.rlsignals);
        rCalendar = (RelativeLayout) findViewById(R.id.rlcalendar);
        rLessons = (RelativeLayout) findViewById(R.id.rllessons);
        rServices = (RelativeLayout) findViewById(R.id.rlservices);
        rContact = (RelativeLayout) findViewById(R.id.rlcontact);

        //layouts definitions for signals layout
        final RelativeLayout rRunning = (RelativeLayout) findViewById(R.id.rlRunning);
        final LinearLayout rPending = (LinearLayout) findViewById(R.id.rlHistory);
        final LinearLayout rClosed = (LinearLayout) findViewById(R.id.rlRating);


        //initialize recyclerview and FIrebase objects
        //for running signals
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        //for closed signals
        recyclerView2 = (RecyclerView) findViewById(R.id.recyclerview2);
        recyclerView2.setLayoutManager(new LinearLayoutManager(this));
        recyclerView2.setHasFixedSize(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        //for running signals
        recyclerView.setLayoutManager(linearLayoutManager);
        //for closed signals
        recyclerView.setLayoutManager(linearLayoutManager);

        //for signals get child as Blogzone
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blogzone");
        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (mAuth.getCurrentUser() == null) {
                    Intent loginIntent = new Intent(MainActivity.this, RegisterActivity.class);
                    loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(loginIntent);
                }
            }
        };


    /////////////////////////////////////////////////////////////////////////////////////////////
    //signals
    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
        FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder> FBRA = new FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder>(
                Blogzone.class,
                R.layout.card_items,
                BlogzoneViewHolder.class,
                mDatabase

        ) {
            @Override
            protected void populateViewHolder(BlogzoneViewHolder viewHolder, Blogzone model, int position) {
                final String post_key = getRef(position).getKey().toString();
                viewHolder.setPair(model.getPair());
                viewHolder.setBuySell(model.getBuysell());
                viewHolder.setOpenPrice(model.getOpenprice());
                viewHolder.setTakeProfit(model.getTakeprofit());
                viewHolder.setStopLoss(model.getStoploss());
                viewHolder.setProfitPips(model.getProfitpips());
                viewHolder.setLossPips(model.getLosspips());
                viewHolder.setComment(model.getComment());
                viewHolder.setResult(model.getResult());
                viewHolder.setRewarding(model.getRewarding());
                viewHolder.setRewardingP(model.getRewardingP());
                //viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());
                //viewHolder.setUserName(model.getUsername());
                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //   Intent singleActivity = new Intent(MainActivity.this, SinglePostActivity.class);
                        //   singleActivity.putExtra("PostID", post_key);
                        //   startActivity(singleActivity);
                    }
                });
            }
        };
        //running signals
        recyclerView.setAdapter(FBRA);
        //closed signals
        recyclerView2.setAdapter(FBRA);
    }

    public static class BlogzoneViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public BlogzoneViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setPair(String pair) {
            TextView post_pair = mView.findViewById(R.id.quote);
            post_pair.setText(pair);
        }

        public void setBuySell(String buySell) {
            TextView post_buysell = mView.findViewById(R.id.type);
            post_buysell.setText(buySell);
        }

        public void setOpenPrice(String openPrice) {
            TextView post_openprice = mView.findViewById(R.id.entry);
            post_openprice.setText(openPrice);
        }

        public void setTakeProfit(String takeProfit) {
            TextView post_takeprofit = mView.findViewById(R.id.tp);
            post_takeprofit.setText("Tp: " + takeProfit);
        }

        public void setStopLoss(String stopLoss) {
            TextView post_stoploss = mView.findViewById(R.id.sl);
            post_stoploss.setText("Sl: " + stopLoss);
        }

        public void setProfitPips(String profitPips) {
            //nothing
        }

        public void setLossPips(String lossPips) {
            //nothing
        }

        public void setRewardingP(String comment) {
            //nothing
        }

        public void setComment(String comment) {
        //nothing
        }

        public void setRewarding(String rewarding) {
        //nothing
        }

        public void setResult(String result) {
            TextView post_result = mView.findViewById(R.id.status);
            post_result.setText("result");
            if (!post_result.getText().toString().trim().matches("Take Profit Hit")) {
                post_result.setTextColor(Color.parseColor("#ffcc0000"));
            } else if (!post_result.getText().toString().trim().matches("Stop Loss Hit")) {
                post_result.setTextColor(Color.parseColor("#ff669900"));
            } else if (!post_result.getText().toString().trim().matches("Pending...")) {
                post_result.setTextColor(Color.parseColor("#000"));
            } else {
                post_result.setTextColor(Color.parseColor("#ffffff"));
            }
        }
    }}

Help I am stack!

List<Blogzone> openedZones = new ArrayList<>();
List<Blogzone> closedZones = new ArrayList<>();

openedZones.addAll(mDatabase.stream().filter(zone -> zone.isOpened()).collect(Collectors.toList()));
closedZones.addAll(mDatabase.stream().filter(zone -> zone.isClosed()).collect(Collectors.toList()));

FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder> FBRAopened = new FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder>(
    Blogzone.class,
    R.layout.card_items,
    BlogzoneViewHolder.class,
    openedZones
) {
    ...
}
        
FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder> FBRAclosed = new FirebaseRecyclerAdapter<Blogzone, BlogzoneViewHolder>(
    Blogzone.class,
    R.layout.card_items,
    BlogzoneViewHolder.class,
    closedZones
) {
    ...
}
        
//running signals
recyclerView.setAdapter(FBRAopened);
//closed signals
recyclerView2.setAdapter(FBRAclosed);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM