public class MainActivity extends ActionBarActivity {
ArrayList<String>sourceContacts;
ArrayAdapter<String>adapterContacts;
ListView lvContacts;
//---------------------------
String DATABASE_NAME="SqContactss.sqlite";// this is name database from assets folder
String DB_PATH_SUFFIX="/databases/";
SQLiteDatabase database=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleCoppyFileFromAssetsToSystemAndroid();
addControls();
showAllContactsOnListView();
}
private void showAllContactsOnListView() {
// step first open database
database=openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE,null);
// one way
//Cursor cursor2=database.query("SqContactss", null, null, null, null, null,null);
// two way
Cursor cursor=database.rawQuery("SELECT * FROM SqContactss", null);
sourceContacts.clear();
while(cursor.moveToNext())
{
int code=cursor.getInt(0);
String name1=cursor.getString(1);
String numberPhone1=cursor.getString(2);
sourceContacts.add("Code:"+code+"Name:"+name1+"\nPhone:"+numberPhone1);
}
cursor.close();// disconnet
adapterContacts.notifyDataSetChanged();
}
private void addControls() {
sourceContacts=new ArrayList<String>();
adapterContacts=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,sourceContacts );
lvContacts=(ListView)findViewById(R.id.lvContacts);
lvContacts.setAdapter(adapterContacts);
}
private void handleCoppyFileFromAssetsToSystemAndroid() {
File dbFile=getDatabasePath(DATABASE_NAME);
if(!dbFile.exists())
{
try{
coppyDatabaseFromAsset();
Toast.makeText(this,"successful", Toast.LENGTH_LONG).show();
}
catch(Exception ex)
{
Toast.makeText(this,ex.toString(), Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(this,"Failed", Toast.LENGTH_LONG).show();
}
}
private void coppyDatabaseFromAsset() {
try {
//get name file your database
InputStream myInput=getAssets().open(DATABASE_NAME);
// my mission is get myInput take it in outFileName;
String outFileName=getDatabasePath11();
//create out official folder + folder in assets
File f=new File(getApplicationInfo().dataDir+DB_PATH_SUFFIX);
// check it
if(!f.exists())
{
f.mkdir();
}
// create folder to save database
OutputStream myOutput=new FileOutputStream(outFileName);
byte[] buffer=new byte[1024];
int lengh;
while((lengh=myInput.read(buffer))>0)// read in file database
{
myOutput.write(buffer,0,lengh);// start write down up phone
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("Error File", e.toString());
}
}
private String getDatabasePath11()
{
return getApplicationInfo().dataDir+DB_PATH_SUFFIX+DATABASE_NAME;
}
}
protected void handleAdd() {
ContentValues row=new ContentValues();
row.put("Id", 113);
row.put("Name", "Trần Bá Thiên");
row.put("Phone","016834565454");
long r=database.insert("SqContactss",null, row);
showAllContactsOnListView();
}
Leave a commend in order to me improve me
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.