0

I am trying to learn how to open an activity from a click event inside my adapter.

Right now, I can click a cardview item and send a textview content to a toast thanks to jogarcia, but i cant seem to figure out how to open a new activity and pass the textview content through putExtra()

Here is my adapter code:...

    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final ListItem ListItem = ListItems.get(position);
        holder.ConfinedSpaceID.setText(ListItem.getSpaceId());
        holder.ConfinedSpaceDescription.setText(ListItem.getDescription());
//the following is added to create an onclick listener for the cardview
        holder.cardview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String string;
                string = ListItem.getSpaceId().toString();
                Toast.makeText(context, "You clicked "+string, Toast.LENGTH_LONG).show();
            }
        });
    }

I want to be able to open a new activity called DisplayIndividual.class, and pass 'string' as a putExtra(). Any help would be appreciated

dlowey
  • 9
  • 5

5 Answers5

0

You can pass context from your activity class to adapter like this

AdapterClass obj = new AdapterClass(this);

In Adapter class

Context context; //global
public AdapterClass(Context context)
{
   this.context = context;  
}

Then you can use Intent now

@Override
            public void onClick(View v) {
                String string;
                string = ListItem.getSpaceId().toString();
                Toast.makeText(context, "You clicked "+string, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context,...)
            }
        });
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

Put an int which is your id into the new Intent.

final Intent intent = new Intent(
     FirstActivity.this, SecondActivity.class);
final Bundle b = new Bundle();
b.putString("key", "value");
intent.putExtras(b);
startActivity(intent);
GensaGames
  • 5,538
  • 4
  • 24
  • 53
0

This is a basically question in Android. We usually pass data from activity to another through Intent.

lets make some change from your snippet:

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final ListItem ListItem = ListItems.get(position);
    holder.ConfinedSpaceID.setText(ListItem.getSpaceId());
    holder.ConfinedSpaceDescription.setText(ListItem.getDescription());

    //the following is added to create an onclick listener for the cardview
    holder.cardview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String string;
            string = ListItem.getSpaceId().toString();
            Toast.makeText(context, "You clicked "+string, Toast.LENGTH_LONG).show();

            // code here, context usually is which activity you set this adapter from
            Intent intent = new Intent(context, DisplayIndi​​vidual.class);
            intent.putExtra("stringKey", string);
            startActivity(intent);
        }
    });
}

and in DisplayIndi​​vidual:

you can get the string data back from Intent

String string getIntent().getStringExtra("stringKey");
Hababa
  • 551
  • 5
  • 8
  • Thank you all for the quick responses... my evening is late, but I will try your suggestions tomorrow. To those in my time zone... sleep well, to those elswhere... good day :) – dlowey Oct 30 '19 at 04:08
  • Hello Hababa... I tried your code and got an error on DisplayIndividual.class as well as startActivity(intent). – dlowey Oct 30 '19 at 14:35
  • maybe I can give some advise if you can paste some error log here – Hababa Oct 30 '19 at 23:49
  • It is fine. I was able to make it work using getContext() – dlowey Nov 01 '19 at 02:52
  • Oh my fault .. I thought your adapter was defined in activity as an inner class .. BTW, `startActivity()` is the member function of `Context`, you can call it straightforward with `context.startActivity()` instead of additional `getContext().startActivity()`, though they are the same .. Anyway, glad you solved your problem :) – Hababa Nov 01 '19 at 03:19
0

In onClick method :

Intent intent = new Intent(context,MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

and in fragment pass the context :

adapter = new Adapter(list , this.getApplicationContext());
recyclerView.setAdapter(adapter);
Gourav Rana
  • 64
  • 1
  • 8
0

Thank you all so much for your input. It has been wonderful. The following worked perfectly for me :) I hadn't read/learned enough to understand getContext()

                Intent myIntent = new Intent(v.getContext(),DisplayIndividual.class);
                myIntent.putExtra("STRING_2_Search", string);
                v.getContext().startActivity(myIntent);
dlowey
  • 9
  • 5