0

I am successfully create an onClick based on adapter getPosition.

the common way is that making onClicklistener at onBindViewHolder

@Override
public void onBindViewHolder (final MyViewHolder holder, final int position){
holder.mId.setText(itemList.get(position).getId());

holder.itemView.setOnClickListener(new View.OnClickListener(){

public void onClick(View view) {

final Intent intent;
if (position == 0) {
intent = new Intent(context, MyActivity.class);
} else if (position == 1) {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);

But, what I am trying achieve is, the parameter that I'm gonna use to go to next Activity is the getId.

I've modified my code to this.

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String idNumber = itemList.get(position).getId();
final Intent intent;
if (idNumber == "7") {
intent = new Intent(context, MyActivity.class);
} else if (idNumber == "10") {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
Log.e("YOUR ID NUMBER IS", idNumber);
Toast.makeText(context, "Recycle Click" + idNumber,Toast.LENGTH_SHORT).show();
}
});

When I run it, and when I click on any item, it keeps going to MyActivity3 But, on the Log and toast says, the right idNumber that I click.

Gideon Steven
  • 399
  • 4
  • 15

1 Answers1

1

Sometimes if(StringValue == StringValue1) does not gives expected result. try replacing

if (idNumber == "7")

with

if (idNumber.equals("7"))

so your code will be like,

if (idNumber.equals("7")) {
    intent = new Intent(context, MyActivity.class);
} else if (idNumber.equals("10")) {
    intent = new Intent(context, MyActivity2.class);
} else {
    intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);

Happy Coding.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50