[英]Android - Sqlite database method undefined for type new View.OnClickListener(){}
i want to call inserttable method from RechargeActivity.java file, where the definition lies in another TryUIOpenHelper.java , where my databse and tables are created. 我想从RechargeActivity.java文件调用inserttable方法,该文件的定义位于另一个TryUIOpenHelper.java中,在该文件中创建了我的数据库和表。 I wish to know better way to to this.. Please help
我希望知道更好的方法。.请帮助
public class RechargeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rechtable);
RadioButton rb1 = (RadioButton) findViewById(R.id.rtt1);
RadioButton rb2 = (RadioButton) findViewById(R.id.rtt2);
final TextView amount = (TextView)findViewById(R.id.amount1);
final TextView talktime = (TextView)findViewById(R.id.talktime1);
final TextView validity = (TextView)findViewById(R.id.validity1);
final Button button = (Button) findViewById(R.id.done);
final TryUIOpenHelper helper=new TryUIOpenHelper(this);
final SQLiteDatabase dbDatabase=helper.getWritableDatabase();
final Date date = new Date();
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH); // 0 to 11
int day = now.get(Calendar.DAY_OF_MONTH);
String val=""+(day)+"/"+(month+1)+"/"+(year+1)+"";
validity.setText(val);
try{
rb1.setOnClickListener( new OnClickListener ()
{
public void onClick(View v)
{
//Error here: The method inserttable(SQLiteDatabase) is undefined for the type new View.OnClickListener(){}
inserttable(dbDatabase);
}
});
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
rb2.setOnClickListener( new OnClickListener ()
{
public void onClick(View v)
{
} } );
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent =
new Intent(RechargeActivity.this, NumForRecharge.class);
startActivity(intent);
}
});
//db.close();
}
}
在您的TryUIOpenHelper
对象上调用它
helper.inserttable(dbDatabase);
rb1.setOnClickListener( new OnClickListener ()
{
public void onClick(View v)
{
TryUIOpenHelper openHelper=new TryUIOpenHelper();
openHelper.inserttable(dbDatabase);
}
});
Change your onClickListener Like this. 像这样更改您的onClickListener。
Reason: When you try to use a method which lies in some other Class, you need to create a Object for that class and only then you can call a method from that class(Assuming it is public method). 原因:当您尝试使用其他某个类中的方法时,您需要为该类创建一个对象,然后才可以从该类中调用一个方法(假设它是公共方法)。 If it is a private method then you will not be able to access it.
如果它是私有方法,那么您将无法访问它。
Similarly if that method is a static one, you need not create a object for that class. 同样,如果该方法是静态方法,则无需为该类创建对象。 Simply
ClassName.MethodName();
只需
ClassName.MethodName();
will do. 会做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.