-2

i am making an android application in which i want to pass "this" object of onCreate method in another method. What should i write in the parameter to catch that object correctly. Because i want to add objects into view.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary);
    showhistory(this);

    //other code
}

void showhistory(what should i write here??)
{
    TextView historyword[] = new TextView[rs.getColumnCount()];
    for(int i=0;i<rs.getColumnCout();i++)
        historyword[i] = new TextView(i want **this** object of onCreate method here);
}

please help. Thank you.

Abhishek Panjabi
  • 439
  • 4
  • 23
  • 1
    Since `showhistory()` is an instance method in the same class, you don't need a method parameter; you can just use `this` inside the method, just as you do inside `onCreate()`. – Ted Hopp Feb 23 '17 at 18:14

2 Answers2

4

If you are using your method in any activity or service, then no need to pass context,because they have their own context. Just use this (context) wherever you need. so your code will look like:

void showhistory()
{
    TextView historyword[] = new TextView[rs.getColumnCount()];
    for(int i=0;i<rs.getColumnCout();i++)
        historyword[i] = new TextView(this);
}
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
3

Use Context as context is needed for the creation of TextView

void showhistory(Context context)
{
    TextView historyword[] = new TextView[rs.getColumnCount()];
    for(int i=0;i<rs.getColumnCout();i++)
        historyword[i] = new TextView(context);
}
N J
  • 27,217
  • 13
  • 76
  • 96