• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting/passing the Main Activity "context"??

Jgjio

Lurker
Hello, I recently created a list I will use in my statistics application http://androidforums.com/threads/need-help-to-program-a-list-interface.1086892/

Except I don't want one list, I want 15. I was planning to solve this by creating a class List and initializing it 15 times. http://pastebin.com/uULVdwe3 . My issue is this line

final EditText newEditText = new EditText(this);

Android wants me to extend context and implement all of its methods but I'm not sure why it wants me to do this. I am confused about what I should do to implement this solution. Any insight would be helpful.
 
You would normally create an EditText from within an Activity class. So the parameter to the EditText constructor (this) is an object of type Context (Activity extends from Context).

As it stands, your List class is not a Context object, because it doesn't extend from this class.
 
That makes more sense. I'm unclear about how to best implement this solution now though (I never intended this solution to be a separate activity).

So I should make List an Activity? I can see this would mean that constructing this 15 times makes me create 15 activities which would probably be too resource intensive right?

Or (kind of a crazy Idea), I can extend Main Activity?

Or should I find another way to implement this?
 
But you do have a MainActivity right? Why aren't you creating these EditText objects from within that?
I guess I'm not understanding why you need this List class, and what part of the code is creating it.
 
Well you could always pass in the Activity (Context) object to your List class in the constructor -

Code:
private Context activity;
...
List (int identificationNumber, LinearLayout  l, Context context) {
  ...
  this.activity = context;
  ...
}
...
private void addNewEdit() {
        final EditText newEditText = new EditText(activity);
        ...
 
Back
Top Bottom