Summary
ListActivity, AdapterView, adapter, multiple activities, SQLite, GUI style, menu resources and MenuInflater.
Use style.xml file to define style element
All resource files use the
Use the defined style in the view
style="@style/SomeStyle"/>
Use menu resource to define application MenuItem in XML
Each menu resource XML file contains a root menu element, and each item nested in it represents a MenuItem. You need to specify an android:id attribute for each item so that you can interact with the corresponding MenuItem in the program. Other item attributes also include:
- android:title and android:titleCondensed represent the title text and the compact title text that is displayed if it cannot be displayed completely.
- android:icon specifies a Drawable attribute to indicate the displayed picture.
- andorid:orderInCategory determines the order in which the MenuItem appears.
The following is the sample code:
Code end
ListActivity class and ListView
ListActivity is a class that contains only one ListView. You can use CursorAdapter as an adapter to fill ListView. And ListActivity is a built-in ListView. ListView needs to set adapter and OnItemClickListener. At the same time, to use SimpleCursorAdapter, you must first define two arrays, each containing the column names that need to be mapped to GUI components and the resource ID of the GUI components. The constructor of SimpleCursorAdapter receives the following parameters:
- The Context where the ListView is running
- The resource ID used to display the layout of each item in the ListView
- The Cursor that provides data access can provide null and specify it later
- String array containing the name of the column to be displayed
- Int array containing the corresponding GUI resource ID
The example code is as follows:
ListView lv=getListView();
lv.setOnitemClickListener(viewListener);
String[] from= new String[]{"name"};
int[] to=new int[]{R.id.to};
CursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.listview,null ,from,to);
setListAdapter(mAdapter);
When the Cursor class is no longer needed
Cursor cursor=mAdapter.getCursor();
if (cursor!=null){
cursor.deactivate();
}
mAdapter.changeCursor(null);
AsyncTask How to use
Create a new AsyncTask and execute it. AsyncTask will execute the task in a separate thread. The actual parameters of the execute method here indicate that the task will not receive any actual parameters. This method will receive a variable number of actual parameters, which are then passed to the character’s doInBackground method as actual parameters. . Each time the following method is executed, a new AsyncTask object will be created, because each AsyncTask can only be executed once, so this is necessary.
AsyncTask is a generic type that requires three parameters:
- The first parameter is the type of variable-length parameter table, which is used in the doInBackground method of AsyncTask. When the execute method is called, the doInBackground method will execute the task in a separate thread of execution.
- The second parameter is the type of variable-length parameter table, which is used in the onProgressUpdate method of AsyncTask. This method is executed in the GUI thread and is used to receive specific types of transitional update data from long-running tasks.
- The third parameter is the type of task result, which will be passed to the onPostExecute method of AsyncTask.
The doInBackground method uses databaseConnector to open the database connection.
private class GetTask extends AsyncTask
Create a new AsyncTask and execute it.
Bundle extras = getIntent().getExtras();
rowID = extras.getLong("row_id");
new GetTask().execute(rowID) ;
Rewrite onCreateOptionsMenu and onOptionsItemSelected, use MenuInflater
Use MenuInflater to fill the menu.
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate (R.menu.address_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Intent addNew = new Intent(this, EditActivity.class);
startActivity(addNew);
return super.onOptionsItemSelected(item);
}
Implement the OnItemClickListener interface
The parameters of the OnItemClickListener method of Menu
- The reference of the AdapterView interacting with the user
- The reference of the root View of the touched list item
- The index of the list item touched in the ListView
- The unique long type ID of the selected list item, here is the row ID in Cursor
The following is the sample code
OnItemClickListener viewContactListener=new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3 ){
Intent viewContact=new Intent(this, ViewContact.class);
viewContact.putExtra(ROW_ID, arg3);
startActivity(viewContact);
}
}
Code end
DatabaseConnector utility class
The utility DatabaseConnector utility class manages the interaction with SQLite.
public class DatabaseConnector{
private SQLiteDatabase database;
private DatabaseOpenHelper databaseOpenHelper;
public DatabaseConnector(Context context){
databaseOpenHelper-new DatabaseOpenHelper (context, DATABASE_NAME, null, 1);
}
public void open() throws SQLException{
database=databaseOpenHelper.getWritableDatabase();
}
public void close(){
if(database!=null){
database.close();
}
}
public void insertContact(String name, String phone){
ContentValues newContact=new ContentValues();
newContact.put("name", name);
newContact.put("phone ", phone);
open();
database.insert("contacts", null, newContact);
close();
}
public void updateContact(long id, String name, String phone){
ContentValues e ditContact=new ContentValues();
editContact.put("name", name);
editContact.put("phone", phone);
open();< br /> database.update("contacts", editContact, "_id="+id, null);
close();
}
publci Cursor getAllContacts(){
return database.query("contacts", new String[]{"_id", "name"}, null, null, null, null, "name");
}
< br /> public Cursor getOneContact(long id){
return database.query("contacts", null, "_id="+id, null, null, null, null);
}
public void deleteContact(long id){
open();
database.delete("contacts", "_id="+id, null);
close() ;
}
}
The getAllContacts method uses the query method of SQLiteDatabase to obtain a Cursor cursor. Its actual parameters are as follows
- The table to be queried Name
- A String array of column names to be returned
- A SQL WHERE clause, or null for returning all rows
- String array of actual parameters will be Replace the question mark placeholder in the actual parameter value in the WHERE clause. If there are no actual parameters in the WHERE clause, null will be used instead.
- A SQL GROUP BY clause (does not contain the keyword GROUP BY) or null (if you do not want to group the results)
- SQL HAVING clause (does not include HAVING), specify the result Which groups to include from the GROUP BY clause, if the GROUP BY clause is null, then it is also null here.
- SQL ORDER BY clause (does not contain the keyword ORDER BY), specify the result order, or null (do not specify the order)
Extend SQLiteOpenHelper
Use SQLiteOpenHelper to help applications create databases and manage version changes. It requires the following 4 actual parameters.
- Context, contains the database to be created and opened.
- Database name, if you want to use an in-memory database, then null
- The CursorFactory used, null means that you want to use the default SQLite CursorFactory
- Database version number, Start from 1.
The example code is as follows:
private class DatabaseOpenHelper extends SQLiteOpenHelper{
public DatabaseOpenHelper(Context context, String name, CursorFactory factory, int version){
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db){
String createQuery="CREATE TABLE contacts"+"(_id integer primary key autoincrement,"+"name TEXT, phone TEXT);";
db.execSQL(createQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}