• 数据库


    主类

    package com.zhl.test;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
    
        private EditText ed;
        private SQLiteDatabase db;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 通过传着四个参数创建数据库
            Sql sql = new Sql(MainActivity.this, "name", null, 1);
            db = sql.getWritableDatabase();
            ed = (EditText) findViewById(R.id.name);
            Button button = (Button) findViewById(R.id.button1);
    
            button.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    // 把从EditText中获取的值转成字符串
                    String str = ed.getText().toString();
                    // 同过ContentValues向数据库中添加数据
                    ContentValues cv = new ContentValues();
                    cv.put("name", str);
                    // 把数据添加到数据裤中
                    db.insert("name", null, cv);
    
                    startActivity(new Intent(MainActivity.this, ZhanShi.class));
                }
            });
        }
    }

    vo类

    package com.zhl.vo;
    
    public class Vo {
    
        public String name;
        public String id;
    
        public Vo(String name, String id) {
            super();
            this.name = name;
            this.id = id;
        }
    
        @Override
        public String toString() {
            return "Vo [name=" + name + ", id=" + id + "]";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
    }

    显示类

    package com.zhl.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.zhl.vo.Vo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    public class ZhanShi extends Activity {
    
        private SQLiteDatabase db;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_zhan_shi);
    
            Sql sql = new Sql(ZhanShi.this, "name", null, 1);
            db = sql.getReadableDatabase();
            // 创建查询语句
            Cursor cr = db.query("name", null, null, null, null, null, null);
    
            List<Vo> list = new ArrayList<Vo>();
    
            while (cr.moveToNext()) {
                // 查询表中摸个名字的值
                String name = cr.getString(cr.getColumnIndex("name"));
                String id = cr.getString(cr.getColumnIndex("id"));
                // 放到集合中去
                list.add(new Vo(name, id));
            }
            ListView listview = (ListView) findViewById(R.id.listview);
            System.out.println(list);
    
            // 使用listview展示出来
            listview.setAdapter(new Myada(list, ZhanShi.this));
        }
    }

    建库

    package com.zhl.test;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class Sql extends SQLiteOpenHelper {
    
        public Sql(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
    
        /**
         * 创建数据库中的表
         * */
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            // 创建表时使用正常的sql语句进行创建
            db.execSQL("create table name(id integer primary key autoincrement,name char)");
    
        }
    
        /**
         * 数据库的版本发生改变时使用的方法
         * */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    
    }

    适配器

    package com.zhl.test;
    
    import java.util.List;
    
    import com.zhl.vo.Vo;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.DataSetObserver;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.TextView;
    
    public class Myada extends BaseAdapter {
    
        List<Vo> list;
        Context context;
        private SQLiteDatabase db;
    
        public Myada(List<Vo> list, Context context) {
            super();
            this.list = list;
            this.context = context;
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }
    
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }
    
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view;
            Holder holder;
            Sql sql = new Sql(context, "name", null, 1);
            db = sql.getWritableDatabase();
            if (convertView == null) {
                view = View.inflate(context, R.layout.activity_zhan_shi_item, null);
                holder = new Holder();
                holder.text = (TextView) view.findViewById(R.id.text_item);
                holder.button = (Button) view.findViewById(R.id.button);
                holder.button2 = (Button) view.findViewById(R.id.button2);
                view.setTag(holder);
    
            } else {
                view = convertView;
                holder = (Holder) view.getTag();
            }
            holder.text.setText(list.get(position).getName());
            holder.button.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     ContentValues cv = new ContentValues();  
                            cv.put("name" , "测试成功"); 
                          
                    db.update("name", cv, "id="+list.get(position).getId(), null);
                }
            });
            holder.button2.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    db.delete("name", "id=" + list.get(position).getId(), null);
                }
            });
    
            return view;
        }
    
        class Holder {
            TextView text;
            Button button;
            Button button2;
    
        }
    }
  • 相关阅读:
    LR问题集锦(二)
    报错“you do not have a license for this Vuser type”
    LoadRunner:Error 10344
    loadrunner 录制脚本后登陆用户名是乱码
    经典语句
    jprofiler
    数据类测试的一些方法
    中文页面显示和英文页面显示,不一样的语言显示,会对性能有影响哇
    LoadRunner问题集锦
    用loadrunner做压力测试,怎样把应用服务器压死
  • 原文地址:https://www.cnblogs.com/3674-it/p/5564956.html
Copyright © 2020-2023  润新知