使用SQLite数据库设计通讯录

使用SQLite数据库设计通讯录_第1张图片

使用SimpleCursorAdapter自定义ListView版面:

static final String[] FROM=new String[]{"name","phone","email"};


adapter=new SimpleCursorAdapter(this,R.layout.item,cur,FROM,
                new int[]{R.id.name,R.id.phone,R.id.email},0);

FROM数组中的字段会对应到

new int[]{R.id.name,R.id.phone,R.id.email}所指的TextView。

说、所以数据库表的_id字段的用处:SimpleCursorAdapter要辨别当前在用户界面“List View”中被选用的选项代表哪个数据,其内部设计就是使用_id字段,所以我们的Cuosor要包含此字段才能让它正常运行。






        

        

        
            
            
        

        
            
            
        

        
            
            
        

        
            
package com.example.mbenben.newotherapp;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import static com.example.mbenben.newotherapp.Main2ActivitySQLite.db_name;
import static com.example.mbenben.newotherapp.Main2ActivitySQLite.tb_name;

public class Main2ActivityHotLine extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{

    static final String DB_NAME="HotlineDB";
    static final String TB_NAME="hotlist";
    static final int MAX=8;
    static final String[] FROM=new String[]{"name","phone","email"};

    SQLiteDatabase db;
    Cursor cur;
    SimpleCursorAdapter adapter;
    EditText etName,etPhone,etEmail;
    Button btInsert,btUpdate,btDelete;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2_hot_line);

        etName=(EditText)findViewById(R.id.etName);
        etPhone=(EditText)findViewById(R.id.etPhone);
        etEmail=(EditText)findViewById(R.id.etEmail);
        btInsert=(Button)findViewById(R.id.btInsert);
        btUpdate=(Button)findViewById(R.id.btUpdate);
        btDelete=(Button)findViewById(R.id.btDelete);

        db=openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE,null);

        String createTable="CREATE TABLE IF NOT EXISTS "+TB_NAME+
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
                    "name VARCHAR(32),"+
                    "phone VARCHAR(16),"+
                    "email VARCHAR(64))";

        db.execSQL(createTable);

        cur=db.rawQuery("SELECT * FROM "+TB_NAME,null);

        if(cur.getCount()==0){
            addData("广州大学","10086","[email protected]");
            addData("djksdj","134141051085","[email protected]");
        }

        adapter=new SimpleCursorAdapter(this,R.layout.item,cur,FROM,
                new int[]{R.id.name,R.id.phone,R.id.email},0);

        lv=findViewById(R.id.lv);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        lv.setOnItemLongClickListener(this);
        requery();
    }

    private void addData(String name ,String phone,String email){
        ContentValues cv=new ContentValues(3);
        cv.put(FROM[0],name);
        cv.put(FROM[1],phone);
        cv.put(FROM[2],email);

        db.insert(TB_NAME,null,cv);
    }

    private void update(String name ,String phone,String email,int id){
        ContentValues cv=new ContentValues(3);
        cv.put(FROM[0],name);
        cv.put(FROM[1],phone);
        cv.put(FROM[2],email);

        db.update(TB_NAME,cv,"_id="+id,null);
    }

    private void requery(){
        cur=db.rawQuery("SELECT * FROM "+TB_NAME,null);
        adapter.changeCursor(cur);
        if(cur.getCount()==MAX)
            btInsert.setEnabled(false);
        else
            btInsert.setEnabled(true);

        btUpdate.setEnabled(false);
        btDelete.setEnabled(false);
    }


    public boolean onItemLongClick(AdapterView parent,View view,int position, long id){
        onItemClick(parent,view,position,id);
        call(view);
        return true;
    }


    public void onItemClick(AdapterView parent, View v,int position,long id){
        cur.moveToPosition(position);
        etName.setText(cur.getString(
                cur.getColumnIndex(FROM[0])
        ));
        etPhone.setText(cur.getString(
                cur.getColumnIndex(FROM[1])
        ));
        etEmail.setText(cur.getString(
                cur.getColumnIndex(FROM[2])
        ));

        btUpdate.setEnabled(true);
        btDelete.setEnabled(true);
    }

    public void onInsertUpdate(View v){
        String nameStr=etName.getText().toString().trim();
        String phoneStr=etPhone.getText().toString().trim();
        String emailStr=etEmail.getText().toString().trim();
        if(nameStr.length()==0 || phoneStr.length()==0 || emailStr.length()==0){
            return;
        }

        if(v.getId()==R.id.btUpdate)
            update(nameStr,phoneStr,emailStr,cur.getInt(0));
        else
            addData(nameStr,phoneStr,emailStr);

        requery();
    }

    public void onDelete(View v){
        db.delete(TB_NAME,"_id="+cur.getInt(0),null);
        requery();
    }

    public void call(View v){
        String uri="tel:" +cur.getString(cur.getColumnIndex(FROM[1]));
        Intent it=new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        startActivity(it);
    }

    public void mail(View v){
        String uri="mailto:"+cur.getString(cur.getColumnIndex(FROM[2]));
        Intent it=new Intent(Intent.ACTION_SENDTO,Uri.parse(uri));
        startActivity(it);
    }
}





































    

    

        

        
    


 

 

你可能感兴趣的:(Android应用开发)