安卓基础干货(三):安卓数据库的学习 (2)

在android应用程序中使用SQLite数据库事务的步骤:

try{ //1、在业务逻辑开始的时候开启事务: db.beginTransaction(); //张三转出100 ContentValues values = new ContentValues(); values.put("money", "1900"); db.update("account", values, "id=?", new String[]{"1"}); //李四收到100 ContentValues values02 = new ContentValues(); values02.put("money", "102"); db.update("account", values02, "id=?", new String[]{"2"}); //2、 在业务逻辑结束的时候告诉系统数据库提交成功 db.setTransactionSuccessful(); } finally{ //3、告诉系统数据库结束 db.endTransaction(); }

listview的使用(重点)

ListView :用来在界面上显示数据列表。

getCount GetView()

listview显示数据的原理:

MVC : javaweb mode: javabean view: 破jsp controller:破servlet //(1) 对listview的优化 convertview: MVC: Model Person 数据 View ListView Controle: Adapter 数据适配器

使用listview显示数据列表的步骤:

1、在布局文件中添加一个listview控件 2、在代码中找到这个listview控件 3、创建一个数据适配器为listview填充数据 ArrayAdapter

使用ArrayAdapter为listview填充数据的步骤:

1、在布局文件中添加ListView 2、在代码中初始化这个listview控件 3、调用listview.setAdapter()填充数据 //使用适配器为listview填充数据 //new ArrayAdapter:context 上下文,resourceId 条目布局文件的资源ID,object[] 要显示的数据 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item, new String[]{"王菲","谢霆锋","张柏芝","李亚鹏"})); SimpleAdapter

使用ArrayAdapter为listview填充数据的步骤:

1、在布局文件中添加ListView 2、在代码中初始化这个listview控件 3、调用listview.setAdapter()填充数据 //使用适配器为listview填充数据 //new SimpleAdapter:context 上下文,resourceId 条目布局文件的资源ID,String[] 要显示的列名,int[] 指定列显示在item布局文件的哪个控件上 lv.setAdapter(new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name"}, new int[]{R.id.tv_id,R.id.tv_name}));

条目的布局文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_id" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name" /> </LinearLayout> 复杂listview界面的显示(重点) 步骤: 1、在布局文件中添加ListView 2、在代码中初始化这个listview控件 lv = (ListView) findViewById(R.id.lv); 3、自定义数据适配器,继承了BaseAdapter,重写4个方法,其中getCount、getView是我们关系的 private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 20; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.item, null); ImageView iv = (ImageView) view.findViewById(R.id.iv); TextView tv_title = (TextView) view.findViewById(R.id.tv_title); tv_title.setText("111"); TextView tv_desc = (TextView) view.findViewById(R.id.tv_desc); tv_desc.setText("2222"); return view; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } 4、调用lv.setAdapter(new MyAdapter())填充数据 数据库listview界面的显示 public class MainActivity extends Activity { private SQLiteDatabase db; private ListView lv; private List<Person> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化listview控件 lv = (ListView) findViewById(R.id.lv); DBHelper helper = new DBHelper(this, "persons.db", null, 1); db = helper.getWritableDatabase(); } public void insert(View v){ // db.execSQL("insert into person (name)values(?)", new String[]{"lisi"}); ContentValues values = new ContentValues(); Random r = new Random(); values.put("name", "zhangsan"+r.nextInt(100)); long rowId = db.insert("person", null, values); System.out.println("rowId="+rowId); Toast.makeText(this, "插入数据成功", 0).show(); } public void query(View v){ list = new ArrayList<Person>(); /** * table 表名 * columns 查询的列 * selection 查询条件"id=1" * selectionArgs 查询条件的值 * String groupBy * String having * String orderBy) * */ Cursor cursor = db.query("person", new String[]{"id","name" }, null, null, null, null, null); while(cursor.moveToNext()){ Person p = new Person(); int id = cursor.getInt(0); p.setId(id); String name = cursor.getString(1); p.setName(name); list.add(p); System.out.println("id="+id+";查询数据成功", 0).show(); } /** *创建一个数据适配器,为listview填充数据 */ private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if(convertView != null){ view = convertView; } else{ view = View.inflate(MainActivity.this, R.layout.item, null); } TextView tv_id = (TextView) view.findViewById(R.id.tv_id); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); Person p = list.get(position); tv_id.setText(p.getId()+""); tv_name.setText(p.getName()); return view; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgwzwz.html