Search code examples
androidandroid-activitythisoncreate

What should i write in parameter of a method in android


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.


Solution

  • 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);
    }