-1

I have a custom listView row which looks like this:

customRow

I have a customAdapter with the following getView method (not interesting parts were deleted)

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    holder.viewProfileImageButton = view.findViewById(R.id.viewProfileImageButton);
    holder.viewProfileImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           String pubKey ((objClass_foundUsers)foundUsersList.get(i)).getPublicKey();
            Intent getToUserProfileIntent = new Intent(context,activity_1_3_my_profile.class);
            context.startActivity(getToUserProfileIntent);
        }
    });
}

When the user clicks on the first imagebutton in the row this user's profile should be displayed. In order to do this I get the publicKey of the user in the onClick method in my customAdapter and want to pass it to another activity.

When using

getToUserProfileIntent.putExtra("pubKey", pubKey);

in the onClick method in the customAdapter and

pubKey = getIntent().getExtras().getString("pubKey");

in my activity it throws a NullPointerException saying:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

How can I send the publicKey from this adapter to another activity?

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
acincognito
  • 1,595
  • 1
  • 13
  • 24

6 Answers6

3

you can send your data using intents in adapters like this:

   Intent mIntent = new Intent(context, yourTargetActivity.class);
    mIntent.putExtra("pubKey", pubKey);
    context.startActivity(mIntent);

OR:

You can implement an Interface to send data to your activity:

 public interface OnButtonPayNowCallBack {
    void onClickListener(String pubKey);
}

add this to your adapter's constructor:

  try {
        this.mOnButtonCallBack = (OnButtonCallBack) context;
    } catch (ClassCastException e) {
        throw new ClassCastException("Calling Context must implement 
                 OnButtonBack");
    }

and then in your click listener:

holder.viewProfileImageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
onClickListener(pubKey)
}
}

And in your next activity you get the data like this: incase of sending data through intents:

   Bundle extras = getIntent().getExtras();
if (extras != null) {
    String pubKey= extras.getString("pubKey");
}

and incase of sending data through Interface:

first implement the interface in activity then use it's override method
    @Override
    public void OnButtonCallBack (String param) {

       // add your functionality here.
    }
Umair
  • 6,366
  • 15
  • 42
  • 50
1

Try this you need to add data in your getToUserProfileIntent using getToUserProfileIntent.putExtra()

 Intent getToUserProfileIntent = new Intent(context,activity_1_3_my_profile.class);
 getToUserProfileIntent.putExtra("pubKey", pubKey);
 context.startActivity(getToUserProfileIntent);

than receive like this in your activity_1_3_my_profile

Bundle data = getIntent().getExtras();
if (data != null) {
    String pubKey= data.getString("pubKey");
}
Goku
  • 9,102
  • 8
  • 50
  • 81
1

for sending use this code

Intent getToUserProfileIntent  = new Intent(getApplicationContext(), activity_1_3_my_profile.class);
getToUserProfileIntent .putExtra("pubKey","pubKey");
startActivity(getToUserProfileIntent );

for getting data inside your activity use this code

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String pubKey= extras.getString("pubKey");
    //The key argument here must match that used in your adapter
}
sunil kushwah
  • 495
  • 1
  • 6
  • 20
0
 holder.viewProfileImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent getToUserProfileIntent = new Intent(context,activity_1_3_my_profile.class);
            getToUserProfileIntent.putExtra("pubKey", pubkey);

                v.getContext().startActivity(getToUserProfileIntent);
            }
        });
chunkydonuts21
  • 79
  • 1
  • 10
0

Please check below answers hope it helps you.

if(getIntent()!=null) // at first to check data is in intent or not
{

if(getIntent().hasExtra("pubKey")) // to avoid crash if pubKey is not passing.
{

pubKey = getIntent().getExtras().getString("pubKey");

}

}

Hope it helps.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Jyubin Patel
  • 1,373
  • 7
  • 17
0

Try below code for sending data to another activity:

@Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            holder.viewProfileImageButton = view.findViewById(R.id.viewProfileImageButton);

            holder.viewProfileImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                   String pubKey = ((objClass_foundUsers)foundUsersList.get(i)).getPublicKey();

                    Intent getToUserProfileIntent = new Intent(context,activity_1_3_my_profile.class);
 getToUserProfileIntent.putExtra("pubKey", pubKey);

                    context.startActivity(getToUserProfileIntent);

                }
            });
        }

Below code for fetching data in another activity:

pubKey = getIntent().getExtras().getString("pubKey");
Pankaj Mundra
  • 1,401
  • 9
  • 25