2012-09-29 46 views
1

我想从RechargeActivity.java文件中调用可插入的方法,其中定义位于另一个TryUIOpenHelper.java中,其中创建了我的数据库和表。我想知道这更好的办法..请帮助Android - Sqlite数据库方法未定义类型new View.OnClickListener(){}

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

     }   
     } 

回答

0

调用它的TryUIOpenHelper对象

helper.inserttable(dbDatabase); 
0
rb1.setOnClickListener(new OnClickListener() 
    { 
      public void onClick(View v) 
      { 

    TryUIOpenHelper openHelper=new TryUIOpenHelper(); 
    openHelper.inserttable(dbDatabase); 

      } 
      }); 

更改onClickListener筛选。

原因:当您尝试使用其位于一些其他类的方法,你需要创建该类的对象,然后才可以调用从类中的方法(假设它是公共的方法)。如果它是私人方法,那么您将无法访问它。

同样,如果该方法是静态方法,则不需要为该类创建对象。只需ClassName.MethodName();即可。

相关问题