Instead of v.getContext(), try Screen1.this in Intent Nithin
On Dec 22, 9:47 pm, Sam B <[email protected]> wrote: > Hi ive got a class containing a button and in the on click event im > trying to call a class that creates a database and displays a query > from the db in a list view this works fine on it's own but when > trying to hook up to the button it doesn't work. > > Here is code for the calling class > > package project.Maps; > > import android.view.View; > import android.view.View.OnClickListener; > import android.app.Activity; > import android.database.Cursor; > import android.database.sqlite.SQLiteDatabase; > import android.database.sqlite.SQLiteException; > import android.os.Bundle; > import android.widget.ArrayAdapter; > import android.widget.Button; > import android.content.Intent; > import android.widget.Toast; > > import java.util.ArrayList; > > import android.app.ListActivity; > > public class Screen1 extends Activity { > > public void onCreate(final Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > > setContentView(R.layout.main); > Button button1 = (Button) findViewById(R.id.button1); > button1.setText("click"); > > button1.setOnClickListener(new OnClickListener() { > public void onClick(View v) { > // Perform action on clicks > ; > > Intent myIntent = new Intent(v.getContext(), > CreateLineUpDB.class); > > Screen1.this.startActivity(myIntent); > startActivity(myIntent); > > }}); }} > > And Code for the database class > > package project.Maps; > > import java.util.ArrayList; > > import android.app.ListActivity; > import android.database.Cursor; > import android.database.sqlite.SQLiteDatabase; > import android.database.sqlite.SQLiteException; > import android.os.Bundle; > import android.util.Log; > import android.widget.ArrayAdapter; > > public class CreateLineUpDB extends ListActivity { > > private final String SAMPLE_DB_NAME = "myFriendsDb"; > private final String SAMPLE_TABLE_NAME = "friends"; > > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > > ArrayList<String> results = new ArrayList<String>(); > SQLiteDatabase sampleDB = null; > > try { > sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, > MODE_PRIVATE, null); > > sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + > SAMPLE_TABLE_NAME + > " (Artist VARCHAR, Stage VARCHAR," + > " Day VARCHAR, TIME VARCHAR);"); > > sampleDB.execSQL("INSERT INTO " + > SAMPLE_TABLE_NAME + > " Values ('Oasis','Main','Friday','22:00');"); > sampleDB.execSQL("INSERT INTO " + > SAMPLE_TABLE_NAME + > " Values ('Rage Against The Machine','Second > stage','Saturday','22:00');"); > sampleDB.execSQL("INSERT INTO " + > SAMPLE_TABLE_NAME + > " Values ('Joe McEldery','Pop > Stage','Sunday','20:00');"); > > Cursor c = sampleDB.rawQuery("SELECT Artist, Stage, Day, Time > FROM " + > SAMPLE_TABLE_NAME + > " where Day=Sunday", null); > > if (c != null ) { > if (c.moveToFirst()) { > do { > String firstName = > c.getString(c.getColumnIndex("Artist")); > String age = > c.getString(c.getColumnIndex("Stage")); > results.add("" + firstName + ",Age: " + age); > }while (c.moveToNext()); > } > } > > this.setListAdapter(new ArrayAdapter<String>(this, > android.R.layout.simple_list_item_1,results)); > > } catch (SQLiteException se ) { > Log.e(getClass().getSimpleName(), "Could not create or Open the > database"); > } finally { > if (sampleDB != null) > sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME); > sampleDB.close(); > } > > } > } -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en

