Android: Get activity context into array adapter class

Nicholas Muir

I am trying to make a glass breaking effect on a tinder like cardstack. The cards are working fine but I am having trouble getting the activity contextinto the adapter so I can use the brokenview plugin.

I have my mainactivity, on that activity I show a fragment and on that fragment I display the cardstack.

On that card stack I want to show a glass breaking animation when a user clicks a low rating button using this library:

https://github.com/zhanyongsheng/BrokenView

So in my adapter getview I set the context for the broken view with the context with this line:

mBrokenView = BrokenView.add2Window((MainActivity)this.getContext());

But when I run click on the button I am getting this error:

ClassCastException: com.mysampleapp.Application cannot be cast to com.mysampleapp.MainActivity

Thank you for your help.

Full code below:

public class SongPreviewCardsDataAdapter extends ArrayAdapter<SongDatabaseMappingAdapter> {

    public SongPreviewCardsDataAdapter(Context context, int resource) {
        super(context, resource);

    }

    ImageButton oneStarRating;
    BrokenView mBrokenView;
    private BrokenTouchListener listener;

    @Override
    public View getView(int position, final View contentView, ViewGroup parent) {

       // Initialise Song Views
       final SongDatabaseMappingAdapter item = getItem(position);

       TextView songName = (TextView) (contentView.findViewById(R.id.songNameTextView));
       songName.setText(item.getSongTitle());

       // Initialise  Rating Buttons
        oneStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton1);
        if (item.getOneStarRating()) {
            oneStarRating.setImageResource(R.drawable.starfull);

            mBrokenView = BrokenView.add2Window((MainActivity)this.getContext());
            listener = new BrokenTouchListener.Builder(mBrokenView).build();
            songName.setOnTouchListener(listener);

        }
Bartek Lipinski

try this instead:

mBrokenView = BrokenView.add2Window((MainActivity)(parent.getContext()));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

android get activity context in abstract base class

From Dev

Android get context from non Activity Class

From Dev

How to get activity context into a non-activity class android?

From Dev

How can I get the context of a class (non-activity) in Android?

From Dev

Best way to get Context in android for long running non Activity Class?

From Dev

Get the context of Activity in a Serializable class

From Dev

how to get the position of an array adapter in another activity

From Java

get Context in non-Activity class

From Dev

Will passing context to helper class in android activity leak?

From Dev

Android: transferring Context and activity from a fragment to a class

From Dev

How to receive callback from Activity class into ListView Adapter class in android?

From Dev

Android: Get context of working activity at the time

From Dev

Android adapter context

From Dev

Get application context from non activity singleton class

From Dev

passing context to non-activity class for a dbHelper in Android

From Dev

Android get activity from within anonymous class

From Dev

Android: get current activity in an extended class

From Dev

Get Activity.Class from service in Android

From Dev

Android Issue with activity context

From Dev

Android Activity Context is Null

From Dev

Android. How to Get context from different activity

From Dev

Android. How to Get context from different activity

From Dev

Application Context in Non Activity Class?

From Dev

How to Call Adapter class before Activity

From Dev

How to finish an activity from a ListView adapter class?

From Dev

How to access Activity object in Adapter class

From Dev

How to finish an activity from a ListView adapter class?

From Dev

How to get activity context in this case?

From Dev

get main activity context for imageView

Related Related

  1. 1

    android get activity context in abstract base class

  2. 2

    Android get context from non Activity Class

  3. 3

    How to get activity context into a non-activity class android?

  4. 4

    How can I get the context of a class (non-activity) in Android?

  5. 5

    Best way to get Context in android for long running non Activity Class?

  6. 6

    Get the context of Activity in a Serializable class

  7. 7

    how to get the position of an array adapter in another activity

  8. 8

    get Context in non-Activity class

  9. 9

    Will passing context to helper class in android activity leak?

  10. 10

    Android: transferring Context and activity from a fragment to a class

  11. 11

    How to receive callback from Activity class into ListView Adapter class in android?

  12. 12

    Android: Get context of working activity at the time

  13. 13

    Android adapter context

  14. 14

    Get application context from non activity singleton class

  15. 15

    passing context to non-activity class for a dbHelper in Android

  16. 16

    Android get activity from within anonymous class

  17. 17

    Android: get current activity in an extended class

  18. 18

    Get Activity.Class from service in Android

  19. 19

    Android Issue with activity context

  20. 20

    Android Activity Context is Null

  21. 21

    Android. How to Get context from different activity

  22. 22

    Android. How to Get context from different activity

  23. 23

    Application Context in Non Activity Class?

  24. 24

    How to Call Adapter class before Activity

  25. 25

    How to finish an activity from a ListView adapter class?

  26. 26

    How to access Activity object in Adapter class

  27. 27

    How to finish an activity from a ListView adapter class?

  28. 28

    How to get activity context in this case?

  29. 29

    get main activity context for imageView

HotTag

Archive