0

I have an activity with two buttons Next and Previous and a textview, I would like to update the textview content each time I click Next or Back For example, If I click Next the textview should show me content from the next position or vice versa. I think that I should be using a loop but it gives me an error when I try to do that and when I add 1 to the position (i+1) it works but it only gives me the second position, I want to get all the positions not only the second one. I don't really know if my question is clear, Hope it is :)

onBindViewHolder

@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, @SuppressLint("RecyclerView") final int i) {

    myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String nextContent = listItems.get(i).getContent();
            String previousContent = listItems.get(i).getContent();
            Intent intent = new Intent(v.getContext(), Main2Activity.class);
            intent.putExtra("next", nextContent);
            intent.putExtra("prev", previousContent);
            v.getContext().startActivity(intent);

        }
    });
}

Main2Activity

btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            itemTextView.setText(nextContent);
        }
    });

btnPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            itemTextView.setText(prevContent);
        }
    });
Basi
  • 3,009
  • 23
  • 28

2 Answers2

0

A loop isn't the answer here, a loop is for automating something. This is an event (the user interacted) so it's not suitable.

The algorithm you probably want is basically the following:

  • User clicked an item
  • Find out the item index
  • Increment the index
  • Find the item with that index (by asking the list)
  • If it exists, do something with it

However, the issue in the code you posted is that your 'nextContent' is always the current item. So you need the following change (but be careful about bounds):

String nextContent = listItems.get(i+1).getContent();
String previousContent = listItems.get(i-1).getContent();

You current code isn't using a loop, so the onBind method should be called once for each value of i so it should not always be the second item


After your comments it appears you have buttons unrelated to the list, so now what you need to do is make it so every time you click an item in the RV or a Next/Prev, you store the correct index (as you have no access to i)

In the activity:

private int currentTextItem = 0;

public void setCurrentTextItem(int i) {
    currentTextItem = i;
    //the dots here will be how you get the text from the item
    //   probably recyclerView.findViewHolderForAdapterPosition(pos) 
    myTextView.setText( ... );
}

In your view holder code:

myViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //(You should have some kind of call back from viewholder to activity)
        activity.setCurrentTextItem(i);
    }
});

Then your next/prev:

btnNext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setCurrentTextItem(currentTextItem + 1);
    }
});

btnPrev.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setCurrentTextItem(currentTextItem - 1);
    }
});
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • I have tried this, then when I click on Next button it show me content from position 1 that's all. There are still more than 30 positions I want to go through. When I try (i+30) it goes straight to the 30th position but this is not the solution. I want to go position by position (i+1) and then (i+2) and then (i+3)... each time I click on Next button. Hope you understand me brother :) – mugiwaranoluffy Jul 09 '19 at 14:26
  • How do I use a loop inside onBind method? I've tried it and it gave me an error ! – mugiwaranoluffy Jul 09 '19 at 14:51
  • Don't call me brother please. First, is btnNext inside the list as one row only? Or separate from the recycler? Or there is a next button on every row? – Nick Cardoso Jul 10 '19 at 07:45
  • Well I have an Sqlite database with 3 columns ( id, title, content). There's a recyclerview in the app that get titles from db and it works perfectly, What I want to do is creating an activity with a textview and two buttons as I mentioned before so When I click on some item from the recyclerview it should give me the content data from db and set it to textview and also I want to go through content data without needing to go back and choose another item from the rv! – mugiwaranoluffy Jul 10 '19 at 08:23
  • Here's the project i'm working on https://stackoverflow.com/questions/56933555/how-to-convert-an-activity-to-a-viewpager – mugiwaranoluffy Jul 10 '19 at 08:24
  • in case you didnt notice, i edited the answer, hopefully it helps you – Nick Cardoso Jul 11 '19 at 14:27
0

It's bad practice to set click listeners in the onBindViewHolder method.

Why? Because onBindViewHolder is called each time the views are recycled for the new content to be displayed on the screen. Say you have a list of 1000 elements with 10 of them visible on the screen.

You scroll it to the end => Then onBindViewHolder would be called 990 times => 990 click listeners set.

You also want to dodge costly operations in onBindViewHolder() because your scrolling would be potentially slowed down.

More tips here:

Recyclerview(Getting item on Recyclerview)