ArrayAdapter_SimpleAdapter_SimpleCursorAdapter+SQL_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > ArrayAdapter_SimpleAdapter_SimpleCursorAdapter+SQL

ArrayAdapter_SimpleAdapter_SimpleCursorAdapter+SQL

 2015/2/1 23:31:26  Mancx  程序员俱乐部  我要评论(0)
  • 摘要:ArrayAdapter_SimpleAdapter:1privateListViewlistview;2privateArrayAdapter<String>arr_adapter;3privateSimpleAdaptersimp_adapter;4privateList<Map<String,Object>>dataList;56@Override7protectedvoidonCreate(BundlesavedInstanceState)
  • 标签:ORA SQL RAD

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={"传说中的数据源1","传说中的数据源2","传说中的数据源3","传说中的数据源4"};
13         
14         //新建一个适配器
15         //参数:1.上下文 2.当前Listview加载的每一个列表项所对应的布局文件3.数据源
16         //arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);       //普通文字适配器
17         //视图加载适配器
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.上下文
23          * 2.data:   数据源  List<? extends Map<String, ?>> data (一个map所组成的list集合)
24          *           每一个Map都会去对应ListView中的一行
25          *           每一个Map(键——值对)中的键必须包含所有在from中所指定的键
26          * 3.resource:列表项的布局文件ID
27          * 4.from:Map中的键名
28          * 5.to:绑定数据视图中的ID。与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", "传说中的数据源"+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     @Override
10     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                 //插入数据库
21                 ContentValues values = new ContentValues();
22                 values.put("name", ed1.getText().toString());
23                 values.put("sex",ed2.getText().toString());        
24                 mysql.insert(values);  
25                 //刷新适配器
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绑定适配器
35         
36     }
37     
38     @Override
39     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 }

与SimpleCursorAdapter配套的SQL:

 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 stub
12     }
13 
14     @Override
15     public void onCreate(SQLiteDatabase db) {
16         // TODO 创建数据库
17         db.execSQL("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     @Override
24     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
25         // TODO Auto-generated method stub
26         
27     }
28     
29     /*插入方法*/
30     public void insert(ContentValues values){
31         SQLiteDatabase db = getWritableDatabase();  //获取SQLiteDatabase
32         db.insert(TABLE_NAME, null, values);   //插入数据库中
33         db.close();
34         
35     }
36     /*查询方法*/
37     public Cursor query(){
38         SQLiteDatabase db = getReadableDatabase();
39         Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);  // 获取Cursor
40         return cursor;
41     }
42     /*关闭数据库*/
43     public void close(){
44         if(db != null){
45             db.close();
46         }
47     }
48 
49 }

 

发表评论
用户名: 匿名