使用SQLite数据库设计通讯录

使用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要包含此字段才能让它正常运行。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2ActivityHotLine">



        <ListView
            android:id="@+id/lv"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="20dp">

        </ListView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textview"
                android:text="姓名"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:hint="请输入姓名"
                android:inputType="textPersonName"
                android:id="@+id/etName"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textview2"
                android:text="电话"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:hint="(02)111222111222"
                android:inputType="phone"
                android:id="@+id/etPhone"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textview3"
                android:text="Email"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:hint="[email protected]"
                android:inputType="textEmailAddress"
                android:id="@+id/etEmail"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/btInsert"
                android:text="新增"
                android:onClick="onInsertUpdate"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/btUpdate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onInsertUpdate"
                android:text="更新" />

            <Button
                android:id="@+id/btDelete"
                android:text="删除"
                android:onClick="onDelete"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <ImageButton
                android:src="@android:drawable/stat_sys_phone_call"
                android:onClick="call"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <ImageButton
                android:src="@android:drawable/sym_action_email"
                android:onClick="mail"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
        </LinearLayout>




</LinearLayout>
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);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--android:orientation="horizontal"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--<TextView-->
<!--android:id="@+id/name"-->
<!--android:layout_marginRight="5dp"-->
<!--android:textSize="30sp"-->
<!--android:textColor="#07a"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content" />-->

<!--<LinearLayout-->
<!--android:orientation="vertical"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content">-->
<!--<TextView-->
<!--android:id="@+id/phone"-->
<!--android:textSize="14sp"-->
<!--android:textColor="#840"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
<!--<TextView-->
<!--android:id="@+id/email"-->
<!--android:textSize="14sp"-->
<!--android:textColor="#480"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->

<!--</LinearLayout>-->


<!--</LinearLayout>-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#07a"
        android:textSize="30sp"
        android:fontFamily="sans-serif" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#840"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#480"
            android:textSize="14sp" />
    </LinearLayout>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/w2597014466/article/details/82805210
今日推荐