35

I have a custom view and would like to access a String that is available in its activity. I've seen code that uses getContext() in the view class, but there is no method to access the String that has been made available to its activity via an intent. How to make a String in an activity available to its custom view?

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
turtleboy
  • 8,210
  • 27
  • 100
  • 199

2 Answers2

45

The getContext() method in the View class returns the context that was passed on its constructor. Usually that's the Activity you want (Activity extends Context). So this probably works for you:

Java:

((Activity)getContext()).someMethod(...);

Kotlin:

(context as? Activity)?.someMethod(...)
oguzhan
  • 2,073
  • 1
  • 26
  • 23
Tiago Simão
  • 797
  • 7
  • 5
  • 16
    It's a good solution for cases where the View is directly created by the Activity. This is not always the case. If the view is a view of a Dialog or a PopupWindow then casting the context as an Activity will result in an error – EyalBellisha Feb 05 '13 at 14:48
  • 2
    As @EyalBellisha points out, this might not always work. In the case of context wrapped compatibility widgets, such as is the case when extending from `android.support.v*`, the `getContext()` will not be of the type Activity. For AppCompatButton, for instance, it's of type TintContextWrapper and cannot be cast to Activity. – Paul Lammertsma Sep 26 '17 at 11:58
-9

I'm a noob to java and android still, so this may not work, but I'm just trying to help.

You should be able to pass in parameters to intents/activities using "putExtra" and "getExtra", for example:

In your main activity:

 Intent EditDebtActivity = new Intent(getBaseContext(), EditDebt.class);
        EditDebtActivity.putExtra(DbAdapter.KEY_ROWID,dRowID);
        EditDebtActivity.putExtra(DbAdapter.KEY_DEBT, dName);
        EditDebtActivity.putExtra(DbAdapter.KEY_STARTINGAMOUNT, dStartAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_CURRENTAMOUNT, dCurrentAmount);
        EditDebtActivity.putExtra(DbAdapter.KEY_DUEDATE, dDueDate);
        EditDebtActivity.putExtra(DbAdapter.KEY_INTERESTRATE, dInterestRate);
        EditDebtActivity.putExtra(DbAdapter.KEY_MINPAYMENT, dMinPayment);
        startActivity(EditDebtActivity);

Then in the "onCreate" method in your new view, use the following:

 Bundle extras = getIntent().getExtras();
  dRowID = extras.getLong(DbAdapter.KEY_ROWID);
  String rowidname = extras.getString(DbAdapter.KEY_DEBT);
  currentamount = extras.getDouble(DbAdapter.KEY_CURRENTAMOUNT);
  startingamount = extras.getDouble(DbAdapter.KEY_STARTINGAMOUNT);
  duedate = extras.getInt(DbAdapter.KEY_DUEDATE);

obviously I've used my own code here, but I am passing row information into a new activity when a user clicks a button.

See this SO link for more info on using putextra and getextra: Sending arrays with Intent.putExtra

Hope this helps, it's my first contribution back to the community :)

Community
  • 1
  • 1
Evan R.
  • 1,210
  • 1
  • 27
  • 42
  • R Hi tahnks for your first contribution:) i have a listview that lists the jpeg filenames at the root. when the user selects, that fires an intent with a filename. the recieving activity can get the filename but it's view which is a custom view can't access the filename. Thanks. – turtleboy Oct 12 '11 at 20:29