ArrayAdapter_SimpleAdapter_SimpleCursorAdapter + SQL, simpleadapter usage
ArrayAdapter_SimpleAdapter:
1 private ListView listview; 2 private ArrayAdapter <String> arr_adapter; 3 private SimpleAdapter simp_adapter; 4 private List <Map <String, Object> dataList; 5 6 @ Override 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_man); 10 listview = (ListView) findViewById (R. id. listView1); 11 12 String [] arr_data = {"legendary data source 1", "legendary data source 2 "," Legendary data source 3 "," legendary data source 4 "}; 13 14 // create an adapter 15 // parameter: 1. context 2. layout file corresponding to each list item loaded by the current Listview 3. data Source 16 // arr_adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, arr_data); // common text adapter 17 // view loading adapter 18 // listview. setAdapter (arr_adapter); 19 20 dataList = new ArrayList <Map <String, Object> (); 21 simp_adapter = new SimpleAdapter (this, getData (), R. layout. item, new String [] {"img", "text"}, new int [] {R. id. img, R. id. text}); 22/* 1. Context 23*2. data: data Source List <? Extends Map <String,?> Data (list set composed of a map) 24 * Each Map will correspond to a row in ListView 25 * Each Map (key-Value Pair) the key must contain all the keys specified in from 26*3. resource: The layout file ID27 * 4 of the list items. from: Map key 28 * 5.to: bind the ID in the data view. Corresponds to from 29 */30 listview. setAdapter (simp_adapter); 31} 32 33 private List <Map <String, Object> getData () {34 35 for (int I = 0; I <20; I ++) {36 Map <String, Object> map = new HashMap <String, Object> (); 37 map. put ("img", R. drawable. ic_launcher); 38 map. put ("text", "legendary data source" + I); 39 dataList. add (map); 40} 41 42 return dataList; 43}
SimpleCursorAdapter:
1 public class ManActivity extends Activity {2 private EditText ed1 = null; 3 private EditText ed2 = null; 4 private Button btn = null; 5 private ListView listview = null; 6 private SimpleCursorAdapter adapter; 7 private MySql mysql = null; 8 9 @ Override10 protected void onCreate (Bundle savedInstanceState) {11 super. onCreate (savedInstanceState); 12 setContentView (R. layout. activity_man); 13 this. mysql = new MySql (ManActivity. this); 14 ed1 = (EditText) findViewById (R. id. editText1); 15 ed2 = (EditText) findViewById (R. id. editText2); 16 listview = (ListView) findViewById (R. id. listView1); 17 btn = (Button) findViewById (R. id. button1); 18 btn. setOnClickListener (new OnClickListener () {19 public void onClick (View view) {20 // insert database 21 ContentValues values = new ContentValues (); 22 values. put ("name", ed1.getText (). toString (); 23 values. put ("sex", ed2.getText (). toString (); 24 mysql. insert (values); 25 // refresh the adapter 26 Cursor cursor = mysql. query (); 27 adapter. changeCursor (cursor); 28 29} 30}); 31 Cursor c = mysql. query (); 32 adapter = new SimpleCursorAdapter (this, R. layout. item, c, new String [] {mysql. NAME, mysql. SEX}, new int [] {R. id. textView1, R. id. textView2}); 33 listview. setAdapter (adapter); 34 // listview bind adapter 35 36} 37 38 @ Override39 public boolean onCreateOptionsMenu (Menu menu) {40 // Inflate the menu; this adds items to the action bar if it is present.41 getMenuInflater (). inflate (R. menu. man, menu); 42 return true; 43} 44 45}
Matching SQL with SimpleCursorAdapter:
1 public class MySql extends SQLiteOpenHelper {2 private final static String TABLE_NAME = "user"; 3 public static final String _ ID = "_ id "; 4 public static final String NAME = "name"; 5 public static final String SEX = "sex"; 6 7 private SQLiteDatabase db = null; 8 9 public MySql (Context context) {10 super (context, "user", null, 1); 11 // TODO Auto-generated constructor stub12} 13 14 @ Override15 pub Lic void onCreate (SQLiteDatabase db) {16 // TODO create DATABASE 17 db.exe cSQL ("create table if not exists" + TABLE_NAME + "" + 18 "(" + _ ID + "integer primary key autoincrement, "19 + NAME +" text not null, "+ SEX +" text not null) "); 20 21} 22 23 @ Override24 public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2) {25 // TODO Auto-generated method stub26 27} 28 29/* Insertion method */30 public void insert (ContentVal Ues values) {31 SQLiteDatabase db = getWritableDatabase (); // obtain SQLiteDatabase32 db. insert (TABLE_NAME, null, values); // insert 33 db into the database. close (); 34 35} 36/* query Method */37 public Cursor query () {38 SQLiteDatabase db = getReadableDatabase (); 39 Cursor cursor = db. query (TABLE_NAME, null, null); // get Cursor40 return cursor; 41} 42/* close database */43 public void close () {44 if (db! = Null) {45 db. close (); 46} 47} 48 49}