I was studying of Onclick events and the listeners in android. On the way, I created a sample app and my aim is to save the given number (register.java) in the database and to show it in an another activity (main.java). But, now on clicking the 'save' button, nothing has been happening. Even the toast method is also not working.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = register.this.openOrCreateDatabase("Number", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS table1(number varchar(15));");
e1 = (EditText)findViewById(R.id.mob_num);
b1 = (Button)findViewById(R.id.save);
b2 = (Button)findViewById(R.id.go);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
number = e1.getText().toString();
data.execSQL("INSERT INTO table1 VALUES('"+number+"')");
Toast.makeText(getApplicationContext(), "'"+number+"'successfully inserted",Toast.LENGTH_SHORT).show();
Intent i = new Intent(register.this, main.class);
startActivity(i);
finish();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent(register.this,main.class);
startActivity(i);
data.close();
finish();
}
});
}
Here is my manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.a.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</activity>
</application>
</manifest>
I know this a very basic thing in android. But, I hope you may help me in this. Sorry and Thanks for your time.
There's an easy way to register onClickListeners
in Android:
In your declaration of the button add
android:onClick="onClick"
and create a method in the Activity
containing the button called onClick(View v)
. From here you can just
switch(v) {
case R.id.save:
Toast.makeText(this, "save button has been pressed", Toast.LENGTH_LONG).show();
break;
case R.id.go:
Toast.makeText(this, "go button has been pressed", Toast.LENGTH_LONG).show();
break;
}
and so on until you've registered all the buttons in the activity. Of course you can use other variable names than what I did in this example, just remember the onClick method (or whatever you name it) MUST be called with View as parameter and the switch MUST run on that parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With