Created
April 9, 2015 16:56
-
-
Save tajribati/90cea2e75991b3933960 to your computer and use it in GitHub Desktop.
java files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.enig.exaviser; | |
import android.app.ActionBar; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.io.File; | |
public class MainActivity extends Activity{ | |
SQLiteDatabase contactsDB = null; | |
Button createDBButton, addContactButton, deleteContactButton, getContactsButton, | |
deleteDBButton; | |
EditText nameEditText, emailEditText, contactListEditText, idEditText; | |
createDBButton = (Button) findViewById(R.id.createDBButton); | |
addContactButton = (Button) findViewById(R.id.addContactButton); | |
deleteContactButton = (Button) findViewById(R.id.deleteContactButton); | |
getContactsButton = (Button) findViewById(R.id.getContactsButton); | |
deleteDBButton = (Button) findViewById(R.id.deleteDBButton); | |
nameEditText = (EditText) findViewById(R.id.nameEditText); | |
emailEditText = (EditText) findViewById(R.id.emailEditText); | |
contactListEditText = (EditText) findViewById(R.id.contactListEditText); | |
idEditText = (EditText) findViewById(R.id.idEditText); | |
} | |
public void createDatabase(View view) { | |
try{ | |
// Opens a current database or creates it | |
// Pass the database name, designate that only this app can use it | |
// and a DatabaseErrorHandler in the case of database corruption | |
contactsDB = this.openOrCreateDatabase("MyContacts", MODE_PRIVATE, null); | |
// Execute an SQL statement that isn't select | |
contactsDB.execSQL("CREATE TABLE IF NOT EXISTS contacts " + | |
"(id integer primary key, name VARCHAR, email VARCHAR);"); | |
// The database on the file system | |
File database = getApplicationContext().getDatabasePath("MyContacts.db"); | |
// Check if the database exists | |
if (database.exists()) { | |
Toast.makeText(this, "Database Created", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(this, "Database Missing", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
catch(Exception e){ | |
Log.e("CONTACTS ERROR", "Error Creating Database"); | |
} | |
// Make buttons clickable since the database was created | |
addContactButton.setClickable(true); | |
deleteContactButton.setClickable(true); | |
getContactsButton.setClickable(true); | |
deleteDBButton.setClickable(true); | |
} | |
public void addContact(View view) { | |
// Get the contact name and email entered | |
String contactName = nameEditText.getText().toString(); | |
String contactEmail = emailEditText.getText().toString(); | |
// Execute SQL statement to insert new data | |
contactsDB.execSQL("INSERT INTO contacts (name, email) VALUES ('" + | |
contactName + "', '" + contactEmail + "');"); | |
} | |
public void getContacts(View view) { | |
// A Cursor provides read and write access to database results | |
Cursor cursor = contactsDB.rawQuery("SELECT * FROM contacts", null); | |
// Get the index for the column name provided | |
int idColumn = cursor.getColumnIndex("id"); | |
int nameColumn = cursor.getColumnIndex("name"); | |
int emailColumn = cursor.getColumnIndex("email"); | |
// Move to the first row of results | |
cursor.moveToFirst(); | |
String contactList = ""; | |
// Verify that we have results | |
if(cursor != null && (cursor.getCount() > 0)){ | |
do{ | |
// Get the results and store them in a String | |
String id = cursor.getString(idColumn); | |
String name = cursor.getString(nameColumn); | |
String email = cursor.getString(emailColumn); | |
contactList = contactList + id + " : " + name + " : " + email + "\n"; | |
// Keep getting results as long as they exist | |
}while(cursor.moveToNext()); | |
contactListEditText.setText(contactList); | |
} else { | |
Toast.makeText(this, "No Results to Show", Toast.LENGTH_SHORT).show(); | |
contactListEditText.setText(""); | |
} | |
} | |
public void deleteContact(View view) { | |
// Get the id to delete | |
String id = idEditText.getText().toString(); | |
// Delete matching id in database | |
contactsDB.execSQL("DELETE FROM contacts WHERE id = " + id + ";"); | |
} | |
public void deleteDatabase(View view) { | |
// Delete database | |
this.deleteDatabase("MyContacts"); | |
} | |
@Override | |
protected void onDestroy() { | |
contactsDB.close(); | |
super.onDestroy(); | |
} | |
ActionBar.Tab tab1, tab2, tab3; | |
// Fragments that will load when the tabs are clicked | |
Fragment fragment1 = new TabFragment1(); | |
Fragment fragment2 = new TabFragment2(); | |
Fragment fragment3 = new TabFragment3(); | |
@SuppressWarnings("deprecation") | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ActionBar actionBar = getActionBar(); | |
// Set the current navigation mode to tabs | |
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
// Add titles to tabs | |
tab1 = actionBar.newTab().setText("Tab1"); | |
tab2 = actionBar.newTab().setText("Tab2"); | |
tab3 = actionBar.newTab().setText("Tab3"); | |
// Set tab listeners which provide callbacks for tab events | |
// A callback informs another class when an action occurs | |
tab1.setTabListener(new TabListener(fragment1)); | |
tab2.setTabListener(new TabListener(fragment2)); | |
tab3.setTabListener(new TabListener(fragment3)); | |
// Adds tabs to the actionbar | |
actionBar.addTab(tab1); | |
actionBar.addTab(tab2); | |
actionBar.addTab(tab3); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.enig.exaviser; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.io.File; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
// Do the same for TabFragment2 and TabFragment3 | |
// Just change R.layout.tab_fragment_1 | |
public class TabFragment1 extends Fragment{ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.tab_fragment_1, null); | |
return rootView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.enig.exaviser; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
// Do the same for TabFragment2 and TabFragment3 | |
// Just change R.layout.tab_fragment_1 | |
public class TabFragment2 extends Fragment{ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.tab_fragment_2, null); | |
return rootView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.enig.exaviser; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
// Do the same for TabFragment2 and TabFragment3 | |
// Just change R.layout.tab_fragment_1 | |
public class TabFragment3 extends Fragment{ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.tab_fragment_3, null); | |
return rootView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.enig.exaviser; | |
import android.app.ActionBar; | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
// The TabListener is a callback interface which is invoked when a tab action occurs | |
public class TabListener implements ActionBar.TabListener { | |
// Fragment that gets passed so we can switch fragments when a tab is clicked | |
Fragment fragment; | |
// Receive the fragment we will place in activity_main | |
public TabListener(Fragment fragment) { | |
this.fragment = fragment; | |
} | |
// Replace the current fragment with the one passed | |
@Override | |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { | |
ft.replace(R.id.fragmentContainer, fragment); | |
} | |
// Remove the fragment when a tab is unselected | |
@Override | |
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { | |
ft.remove(fragment); | |
} | |
@Override | |
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment