我应该在 android 中的方法参数中写什么
What should i write in parameter of a method in android
我正在制作一个 android 应用程序,我想在其中传递另一个方法中 onCreate 方法的 "this" 对象。我应该在参数中写什么才能正确捕获该对象。因为我想将对象添加到视图中。
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);
}
请帮忙。谢谢。
使用 Context
因为创建 TextView
需要上下文
void showhistory(Context context)
{
TextView historyword[] = new TextView[rs.getColumnCount()];
for(int i=0;i<rs.getColumnCout();i++)
historyword[i] = new TextView(context);
}
如果您在任何 activity 或服务中使用您的方法,则无需传递上下文,因为它们有自己的上下文。只需在需要的地方使用 this
(上下文)。
所以你的代码看起来像:
void showhistory()
{
TextView historyword[] = new TextView[rs.getColumnCount()];
for(int i=0;i<rs.getColumnCout();i++)
historyword[i] = new TextView(this);
}
我正在制作一个 android 应用程序,我想在其中传递另一个方法中 onCreate 方法的 "this" 对象。我应该在参数中写什么才能正确捕获该对象。因为我想将对象添加到视图中。
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);
}
请帮忙。谢谢。
使用 Context
因为创建 TextView
void showhistory(Context context)
{
TextView historyword[] = new TextView[rs.getColumnCount()];
for(int i=0;i<rs.getColumnCout();i++)
historyword[i] = new TextView(context);
}
如果您在任何 activity 或服务中使用您的方法,则无需传递上下文,因为它们有自己的上下文。只需在需要的地方使用 this
(上下文)。
所以你的代码看起来像:
void showhistory()
{
TextView historyword[] = new TextView[rs.getColumnCount()];
for(int i=0;i<rs.getColumnCout();i++)
historyword[i] = new TextView(this);
}