-1

I will try to be as clear as possible:

I have an application with a recyclerview, when the user clicks on a recyclerview item, he opens a new activity that starts playing a video directly.

In this new activity (the one that contains videoview) I have inserted three imagebutton to allow the user to share, like or download the video while watching it.

My question is how to define the listener of each imagebutton in order to perform a precise action on the selected recyclerview item.

So I would like the user to download the video he is looking at, ie the one that corresponds to the selected item.

Here is my recyclerview Adapter:

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private LayoutInflater inflater;
private List<Data> data= Collections.emptyList();
Data current;
private EditText editText;
private String GetName;
private int currentPos=0;

// create constructor to innitilize context and data sent from MainActivity
public Adapter(Context context, List<Data> data){
    this.context=context;
    inflater= LayoutInflater.from(context);
    this.data=data;
}

// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.container, parent,false);
    MyHolder holder=new MyHolder(view);
    return holder;
}

// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    // Obtenir la position actuelle de l'élément dans recyclerview pour lier les données et affecter des valeurs à partir de la liste
    MyHolder myHolder= (MyHolder) holder;
    Data current = data.get(position);

    myHolder.textFishName.setText(current.catName);
    myHolder.textSize.setText(current.sizeName);
    myHolder.textSize.setVisibility(View.GONE);
    myHolder.textType.setText(current.fishName);
    myHolder.textPrice.setText("" + current.price + "");
    myHolder.textPrice.setVisibility(View.GONE);
    myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

    // load image into imageview using glide
    Glide.with(context).load("http://192.168.43.196/vibe2/images/" + current.fishImage)
            .placeholder(R.drawable.ic_menu_camera)
            .error(R.drawable.ic_menu_camera)
            .into(myHolder.ivFish);

}

// return total item from List
@Override
public int getItemCount() {
    return data.size();
}


class MyHolder extends RecyclerView.ViewHolder {

    TextView textFishName;
    ImageView ivFish;
    TextView textSize;
    TextView textType;
    TextView textPrice;
    TextView counter;
    TextView id;
    ImageButton imageButton;


    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
        editText = itemView.findViewById(R.id.editText);
        textFishName = itemView.findViewById(R.id.textFishName);
        ivFish = itemView.findViewById(R.id.ivFish);
        textSize = itemView.findViewById(R.id.textSize);
        textType = itemView.findViewById(R.id.textType);
        textPrice = itemView.findViewById(R.id.textPrice);
        counter = itemView.findViewById(R.id.counter);
    }
   }
}

My RecyclerView:

RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
                @Override
                public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                        Intent intent = new Intent(MainActivity.this, PlayVideo.class);
                        intent.putExtra("url", data.get(position).sizeName);
                        intent.putExtra("name", data.get(position).fishName);
                        intent.putExtra("titre", data.get(position).catName);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
                        finish();
                }
            });

Here is how I get and use the position in my second activity but it does not seem to work. no matter what element I click toast the condition is not true.

final int position = getIntent().getIntExtra("a",-1);

sparkButton1.setEventListener(new SparkEventListener(){
        @Override
        public void onEvent(ImageView button, boolean buttonState) {
            if (position == 0) {
                Toast.makeText(PlayVideo.this, "welcome", Toast.LENGTH_LONG).show();
            }else if (position ==1){
                Toast.makeText(PlayVideo.this, "exit", Toast.LENGTH_LONG).show();
            }
        }
pharaon
  • 11
  • 6

3 Answers3

0

If your list item contains the information to like, download or share the video, for e.g., a unique id or an url to download the video, then just pass the item to the second activity in the intent and do those tasks there.
If you really want to get the callback to the previous activity, for your imageButtons click you can use BroadcastReceiver.

Update, after your last edit
You are not sending anything in your intent with the key "a". Hence you are not getting any.

RoyalGriffin
  • 1,987
  • 1
  • 12
  • 25
0

In order to access the recyclerview's clicked position in the 2nd activity you can:
a) pass extra data to the intent that opens the activity, like the position of the item clicked:

intent.putExtra("position", position);

and you read it back in your 2nd activity with int position = getIntent().getIntExtra("position", -1)


or

b) use a static variable in another class like:

public final class AppData {
    public static int currentPos = -1;
}

and your onClick will be:

    public void onClick(View view) {
        AppData.currentPos = position;
        showPosition(position);
    }

so you can access AppData.currentPos in your 2nd Activity.

Of course you must remove private int currentPos=0; from your adapter class which you don't use anyway.

0

I finally found the solution by doing this:

RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
                @Override
                public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                        Intent intent = new Intent(MainActivity.this, PlayVideo.class);
                        intent.putExtra("position", position);
                        startActivity(intent);
                }
            });

Then adding this in my second activity:

 if (extras != null) {
        final int position = extras.getInt("position");
        sparkButton1.setEventListener(new SparkEventListener() {

            @Override
            public void onEvent(ImageView button, boolean buttonState) {
                if (position == 0) {
                    Toast.makeText(PlayVideo.this, "welcome", Toast.LENGTH_LONG).show();
                } else if (position == 1) {
                    Toast.makeText(PlayVideo.this, "exit", Toast.LENGTH_LONG).show();
                }
            }
pharaon
  • 11
  • 6