-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.

1
  • 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(). Commented Feb 23, 2017 at 18:14

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.