安卓项目——使用SQLite和悬浮图标实现通讯录功能

通讯录实现

      • 源码地址
      • 编写代码
          • MyDbHelper.java
          • MainActivity.java
      • 出现过的问题
          • DbHelper的Oncreate的时机没有把握清除
          • 没有Device File Manage
          • 通过SQL查询的资源字符串得到其ID
          • Gradle更新导入design包出错
      • 执行结果截图
      • 待改进

源码地址

github地址

编写代码

MyDbHelper.java
package dpHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.maibenben.sqlitetest.MainActivity;

/**
 * Created by MAIBENBEN on 2020/5/6.
 */
public class MyDbHelper extends SQLiteOpenHelper {
    public MyDbHelper(Context context) {
        super(context, "test5.db", null, 1);
        MainActivity.result.append("创建打开库");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("drop table if exists person");
        db.execSQL("CREATE TABLE person(id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),type VARCHAR(20),imgurl VARCHAR(20))");
        db.execSQL("insert into person values(null,'lxw的爸爸','家人','fish')");
        db.execSQL("insert into person values(null,'lxw的妈妈','家人','queen')");
        db.execSQL("insert into person values(null,'Delta','同学','l4')");
        db.execSQL("insert into person values(null,'DongDong','同学','l5')");
        db.execSQL("insert into person values(null,'栗子','同学','l1')");
        db.execSQL("insert into person values(null,'小明','朋友','l2')");
        MainActivity.result.append("创建打开表");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

MainActivity.java

在这里本来是ContractFragment.java,由于下述出现的问题,重建了项目。
包含数据库的操作
以及控件的显示与否
和悬浮图标的监听函数

package com.example.maibenben.sqlitetest;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import dpHelper.MyDbHelper;

public class MainActivity extends AppCompatActivity {
    public static StringBuilder result = new StringBuilder("程序的运行结果:");

    SQLiteDatabase db;
    Cursor cursor;
//    private static final String TAG = contactFragment.class.getSimpleName();

    private List<String> mList = new ArrayList<>();
    private List<GroupDataBean> mDataList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RecyclerView recyclerView;
        MyDbHelper myhelper = new MyDbHelper(this);
        db = myhelper.getWritableDatabase();

        cursor = db.rawQuery("select * from person", null);
        while (cursor.moveToNext()){
            GroupDataBean bean = new GroupDataBean();
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            String type = cursor.getString(cursor.getColumnIndex("type"));
            String imgurl = cursor.getString(cursor.getColumnIndex("imgurl"));

            bean.setTeam(name);
            bean.setArea(type);
            bean.setimageid(getimages(imgurl));
            mDataList.add(bean);
            this.result.append(name);
        }
        Log.d("test",this.result.toString()+cursor.getCount());

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_03);
        LinearLayoutManager manager = new LinearLayoutManager(getBaseContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);
        GroupAdapter adapter = new GroupAdapter(mDataList);
        recyclerView.setAdapter(adapter);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                RelativeLayout reLayout =(RelativeLayout) findViewById(R.id.recycler_view_03);

                final EditText nameV = (EditText)findViewById(R.id.name);
                final EditText typeV = (EditText) findViewById(R.id.type);
                final Button btnV = (Button) findViewById(R.id.button);
                recyclerView.setVisibility(View.GONE);
                nameV.setVisibility(View.VISIBLE);
                typeV.setVisibility(View.VISIBLE);
                btnV.setVisibility(View.VISIBLE);
                //监听button事件
                btnV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ContentValues values = new ContentValues();
                        values.put("name", String.valueOf(nameV.getText()));
                        values.put("type", String.valueOf(typeV.getText()));
                        values.put("imgurl", String.valueOf("l9"));
                        long rowid=db.insert("person", null, values);
                        if(rowid==-1)
                            Log.i("myDbDemo", "数据插入失败!");
                        else
                            Log.i("myDbDemo", "数据插入成功!"+rowid);
                        nameV.setVisibility(View.INVISIBLE);
                        typeV.setVisibility(View.INVISIBLE);
                        btnV.setVisibility(View.INVISIBLE);
                        mDataList.clear();
                        cursor = db.rawQuery("select * from person", null);
                        while (cursor.moveToNext()){
                            GroupDataBean bean = new GroupDataBean();
                            int id = cursor.getInt(0);
                            String name = cursor.getString(1);
                            String type = cursor.getString(cursor.getColumnIndex("type"));
                            String imgurl = cursor.getString(cursor.getColumnIndex("imgurl"));

                            bean.setTeam(name);
                            bean.setArea(type);
                            bean.setimageid(getimages(imgurl));
                            mDataList.add(bean);
                        }

                        LinearLayoutManager manager = new LinearLayoutManager(getBaseContext(),LinearLayoutManager.VERTICAL,false);
                        recyclerView.setLayoutManager(manager);
                        GroupAdapter adapter = new GroupAdapter(mDataList);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setVisibility(View.VISIBLE);
//                        Toast tot = Toast.makeText(
//                                MainActivity.this,
//                                "匿名内部类实现button点击事件",
//                                Toast.LENGTH_LONG);
//                        tot.show();
                    }
                });
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
            }
        });
    }

    public static int getimages(String name){
        Class drawable = R.drawable.class;
        Field field = null;
        try {
            field =drawable.getField(name);
            int images = field.getInt(field.getName());
            return images;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

出现过的问题

DbHelper的Oncreate的时机没有把握清除

由于时机把握错误,在第一次建库后就不会再执行Oncreat的插值。
解决:换个数据库名,或者删除数据库。
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第1张图片

没有Device File Manage

这个安卓版本没有这个,但有Android Device Monitor可供资源文件的查找。
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第2张图片

通过SQL查询的资源字符串得到其ID
public static int getimages(String name){
	Class drawable = R.drawable.class;
	Field field = null;
	try {
		field =drawable.getField(name);
		int images = field.getInt(field.getName());
		return images;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return 0;
}

Gradle更新导入design包出错

原有的项目更新包失败。
打算重新下一个Android Studio,目前采用的是重新建了一个项目,没有在原有的项目情况下继续添加悬浮按钮。
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第3张图片安卓项目——使用SQLite和悬浮图标实现通讯录功能_第4张图片

执行结果截图

通过GroupList展示通讯录的用户列表。
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第5张图片
点击按钮,列表隐藏,输入框显示,可添加用户。
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第6张图片
点击按钮insert执行并且列表重新加载(clear)。(列表显示、输入框隐藏)
安卓项目——使用SQLite和悬浮图标实现通讯录功能_第7张图片

待改进

头像的上传功能并没有实现,对于新增加的用户,用的相同的资源名称。

你可能感兴趣的:(源码,笔记,安卓,安卓)