设为首页 收藏本站
查看: 2068|回复: 0

[经验分享] Android心得4.5--SQLite数据库--事务处理、ListView列表显示

[复制链接]

尚未签到

发表于 2016-12-2 06:21:45 | 显示全部楼层 |阅读模式
  1.   使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,如果程序执行到endTransaction()之前调用了setTransactionSuccessful() 方法设置事务的标志为成功则提交事务,如果没有调用setTransactionSuccessful() 方法则回滚事务。使用例子如下:
 SQLiteDatabase db = ....;

  db.beginTransaction();//开始事务
  try {
      db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"传智播客", 4});
      db.execSQL("update person set name=? where personid=?", new Object[]{"传智", 1});
      db.setTransactionSuccessful();//调用此方法会在执行到endTransaction() 时提交当前事务,如果不调用此方法会回滚事务
  } finally {
      db.endTransaction();//由事务的标志决定是提交事务,还是回滚事务
  }
  db.close();
  上面两条SQL语句在同一个事务中执行。
  2.ListView列表显示
     先在Layout文件夹下创建一个item.xml文件,水平线性布局显示文件,然后在main.xml文件下引用<ListView/>.
     网格显示跟列表显示基本上一样。
     有三种方法可以完成操作,分别是下面代码的show()、show2()、show3(),show3()是自定义的适配器,某些特殊情况下会用到。
  代码如下:
  package cn.itcast.db;
   
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
   
  import cn.itcast.adapter.PersonAdapter;
  import cn.itcast.domain.Person;
  import cn.itcast.service.PersonService;
  import android.app.Activity;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.SimpleCursorAdapter;
  import android.widget.Toast;
   
  public class MainActivity extends Activity {
      private ListView listView;
      private PersonService personService;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          personService = new PersonService(this);
         
          listView = (ListView) this.findViewById(R.id.listView);
          listView.setOnItemClickListener(new ItemClickListener());
          show2();
      }
     
      private final class ItemClickListener implements OnItemClickListener{
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                       ListView lView = (ListView)parent;
                       /* 自定义适配器
                       Person person = (Person) lView.getItemAtPosition(position);
                       Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();*/
                      
                       Cursor cursor = (Cursor) lView.getItemAtPosition(position);
                       int personid = cursor.getInt(cursor.getColumnIndex("_id"));
                       Toast.makeText(getApplicationContext(), personid+ "", 1).show();
                }
      }
     
     
      //自定义适配器
         private void show3() {
                List<Person> persons = personService.getScrollData(0, 20);
                PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
                listView.setAdapter(adapter);
         }
   
         private void show2() {
                Cursor cursor = personService.getCursorScrollData(0, 20);
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
                              new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
                listView.setAdapter(adapter);
         }
   
         private void show() {
                List<Person> persons = personService.getScrollData(0, 20);
                List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
                for(Person person : persons){
                       HashMap<String, Object> item = new HashMap<String, Object>();
                       item.put("name", person.getName());
                       item.put("phone", person.getPhone());
                       item.put("amount", person.getAmount());
                       item.put("id", person.getId());
                       data.add(item);
                }
                SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
                              new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
               
                listView.setAdapter(adapter);
               
         }
  }
  Adapter代码
  package cn.itcast.adapter;
   
  import java.util.List;
   
  import cn.itcast.db.R;
  import cn.itcast.domain.Person;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.TextView;
   
  public class PersonAdapter extends BaseAdapter {
         private List<Person> persons;//在绑定的数据
         private int resource;//绑定的条目界面
         private LayoutInflater inflater;
        
         public PersonAdapter(Context context, List<Person> persons, int resource) {
                this.persons = persons;
                this.resource = resource;
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         }
   
         @Override
         public int getCount() {
                return persons.size();//数据总数
         }
   
         @Override
         public Object getItem(int position) {
                return persons.get(position);
         }
   
         @Override
         public long getItemId(int position) {
                return position;
         }
   
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
                TextView nameView = null;
                TextView phoneView = null;
                TextView amountView = null;
                if(convertView==null){
                       convertView = inflater.inflate(resource, null);//生成条目界面对象
                       nameView = (TextView) convertView.findViewById(R.id.name);
                       phoneView = (TextView) convertView.findViewById(R.id.phone);
                       amountView = (TextView) convertView.findViewById(R.id.amount);
                      
                       ViewCache cache = new ViewCache();
                       cache.nameView = nameView;
                       cache.phoneView = phoneView;
                       cache.amountView = amountView;                 
                       convertView.setTag(cache);
                }else{
                       ViewCache cache = (ViewCache) convertView.getTag();
                       nameView = cache.nameView;
                       phoneView = cache.phoneView;
                       amountView = cache.amountView;
                }
                Person person = persons.get(position);
                //下面代码实现数据绑定
                nameView.setText(person.getName());
                phoneView.setText(person.getPhone());
                amountView.setText(person.getAmount().toString());
               
                return convertView;
         }
        
         private final class ViewCache{
                public TextView nameView;
                public TextView phoneView;
                public TextView amountView;
         }
   
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-308267-1-1.html 上篇帖子: 把带有逗号的excel文件导入到sqlite数据库 下篇帖子: Android开发—数据库应用—访问数据表(SQLite OpenHelper) —删除数据
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表