The core code is as follows:
MainActivity. java
[Java]
Package com. example. lession05_dbs;
Import java. util. List;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AdapterView;
Import android. widget. BaseAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
Import android. widget. Toast;
Import com. example. lession05_dbs.dao.PersonDao;
Import com. example. lession05_dbs.dao.PersonDaoImpl;
Import com. example. lession05_dbs.domain.Person;
Public class MainActivity extends Activity {
// ListView Control
Public ListView listView;
// Display all user information
Public List <Person> persons;
// Database operation object
Public PersonDao personDao;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Instantiate the database operation object
PersonDao = new PersonDaoImpl (MainActivity. this );
// Execute the query data
Persons = personDao. findAll ();
// Obtain the control object by id
ListView = (ListView) findViewById (R. id. lv_persons );
// Set the displayed data Adpater
ListView. setAdapter (new PersonListAdapter ());
// ListView registration event
ListView. setOnItemClickListener (new AdapterView. OnItemClickListener (){
/**
* Parent: listView
* View each entry control
* Position: the position of the Entry
* Id: the row number is 0.
*/
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
TextView TV = (TextView) view;
// Toast. makeText (getApplicationContext (), TV. getText (). toString (), Toast. LENGTH_LONG). show ();
// Obtain the control text
/* String text = TV. getText (). toString ();
// Split
String arr [] = text. split ("-");
// Get the phone number
String phone = arr [1];
// Intention
Intent intent = new Intent ();
// Set the action
Intent. setAction (Intent. ACTION_CALL );
// Set Data
Intent. setData (Uri. parse ("tel:" + phone ));
// Execution intent
StartActivity (intent );*/
// Parent listView
Person p = (Person) parent. getItemAtPosition (position); // The returned value is actually the value returned by getItem.
/* // Intention
Intent intent = new Intent ();
// Set the action
Intent. setAction (Intent. ACTION_CALL );
// Set Data
Intent. setData (Uri. parse ("tel:" + p. getPhone ()));
// Execution intent
StartActivity (intent );
*/
// Toast. makeText (getApplicationContext (), p. getName (), Toast. LENGTH_LONG). show ();
Int lid = (int) parent. getItemIdAtPosition (position );
//
Toast. makeText (getApplicationContext (), lid + "", Toast. LENGTH_LONG). show ();
}
});
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
Class PersonListAdapter extends BaseAdapter {
// Tell you how many entries there are
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return persons. size ();
}
// Return the data corresponding to the control.
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return persons. get (position );
}
// Return the position of the Entry
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// Create a control corresponding to each entry of the displayed Control
TextView TV = new TextView (MainActivity. this );
TV. setText (persons. get (position). getName () + "-" + persons. get (position). getPhone ());
Return TV;
}
}
}
Package com. example. lession05_dbs;
Import java. util. List;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AdapterView;
Import android. widget. BaseAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
Import android. widget. Toast;
Import com. example. lession05_dbs.dao.PersonDao;
Import com. example. lession05_dbs.dao.PersonDaoImpl;
Import com. example. lession05_dbs.domain.Person;
Public class MainActivity extends Activity {
// ListView Control
Public ListView listView;
// Display all user information
Public List <Person> persons;
// Database operation object
Public PersonDao personDao;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Instantiate the database operation object
PersonDao = new PersonDaoImpl (MainActivity. this );
// Execute the query data
Persons = personDao. findAll ();
// Obtain the control object by id
ListView = (ListView) findViewById (R. id. lv_persons );
// Set the displayed data Adpater
ListView. setAdapter (new PersonListAdapter ());
// ListView registration event
ListView. setOnItemClickListener (new AdapterView. OnItemClickListener (){
/**
* Parent: listView
* View each entry control
* Position: the position of the Entry
* Id: the row number is 0.
*/
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
TextView TV = (TextView) view;
// Toast. makeText (getApplicationContext (), TV. getText (). toString (), Toast. LENGTH_LONG). show ();
// Obtain the control text
/* String text = TV. getText (). toString ();
// Split
String arr [] = text. split ("-");
// Get the phone number
String phone = arr [1];
// Intention
Intent intent = new Intent ();
// Set the action
Intent. setAction (Intent. ACTION_CALL );
// Set Data
Intent. setData (Uri. parse ("tel:" + phone ));
// Execution intent
StartActivity (intent );*/
// Parent listView
Person p = (Person) parent. getItemAtPosition (position); // The returned value is actually the value returned by getItem.
/* // Intention
Intent intent = new Intent ();
// Set the action
Intent. setAction (Intent. ACTION_CALL );
// Set Data
Intent. setData (Uri. parse ("tel:" + p. getPhone ()));
// Execution intent
StartActivity (intent );
*/
// Toast. makeText (getApplicationContext (), p. getName (), Toast. LENGTH_LONG). show ();
Int lid = (int) parent. getItemIdAtPosition (position );
//
Toast. makeText (getApplicationContext (), lid + "", Toast. LENGTH_LONG). show ();
}
});
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
Class PersonListAdapter extends BaseAdapter {
// Tell you how many entries there are
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return persons. size ();
}
// Return the data corresponding to the control.
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return persons. get (position );
}
// Return the position of the Entry
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// Create a control corresponding to each entry of the displayed Control
TextView TV = new TextView (MainActivity. this );
TV. setText (persons. get (position). getName () + "-" + persons. get (position). getPhone ());
Return TV;
}
}
}
Note: add the dialing permission to the configuration file:
<Uses-permission android: name = "android. permission. CALL_PHONE"/>
For other entity classes, interfaces, and implementation classes, see the previous blog