安卓作业----慕课移动应用开发作业22之ContentProvider,ContentResolver与SqliteOpenHelper简单应用

本篇做了一个ContentProvider、ContentResolver、SqliteOpenHelper的简单应用,通过ContentProvider提供数据,通过ContentPResolver读取数据。
同时这也是中国大学慕课移动终端应用开发的网课作业22

原题目要求:
(1)创建2个项目,一个ProviderApp用于提供数据,一个ResoverApp用于使用数据,数据源使用SQLite数据库。
(2)自定义ContentProvider。
(3)自定义SQLiteOpenHelper,创建数据库与表。
(4)对数据库的增、删、改、查操作。

说明

1.两个应用,ProviderApp提供数据,ResoverApp使用数据。
2.由于时间问题,我没有做一些判错处理,测试的时候尽量跟随提示输入,进行正确操作,错误的操作如查询为空的时候会闪退。
3.修改数据功能是根据id进行修改的
3.有关ContentProvider方面知识,我建议大家结合网课和此篇博客学习。

演示视频

由于gif图片大小有限制,我只演示部分功能

目录结构

ProviderApp

安卓作业----慕课移动应用开发作业22之ContentProvider,ContentResolver与SqliteOpenHelper简单应用_第1张图片

ResoverApp

安卓作业----慕课移动应用开发作业22之ContentProvider,ContentResolver与SqliteOpenHelper简单应用_第2张图片

代码部分

ProviderApp

1.配置文件 AndroidManifest.xml

添加如下部分

 <provider
	  android:name=".provider.WordContentProvider"
	  android:authorities="com.example.providerapp.WordContentProvider"
	  android:enabled="true"
	  android:exported="true">provider>

2.WordCursorWrapper.java
public class WordCursorWrapper extends CursorWrapper {
    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public WordCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Word getWord(){
        //获取数据
        int id = getInt(getColumnIndex(WordDbSchema.WordTable.Columns.ID));
        String english = getString(getColumnIndex(WordDbSchema.WordTable.Columns.ENGLISH));
        String chinese = getString(getColumnIndex(WordDbSchema.WordTable.Columns.CHINESE));
        String description = getString(getColumnIndex(WordDbSchema.WordTable.Columns.DESCRIPTION));

        return new Word(id,english,chinese,description);
    }
}

3.WordDbSchema.java
public class WordDbSchema {
    public static final class WordTable{
        //定义表名
        public static final String TABLE_NAME = "words";

        //定义数据表字段
        public static final class Columns{
            public static final String ID = "id";
            public static final String ENGLISH = "english";
            public static final String CHINESE = "chinese";
            public static final String DESCRIPTION = "description";
        }
    }
}
4.WordSqlHelper.java
public class WordSqlHelper extends SQLiteOpenHelper {
    private static final int VERSION = 1;//定义版本
    private static final String DATABASE_NAME = "course22DataBase";

    public WordSqlHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+ WordDbSchema.WordTable.TABLE_NAME+
                "("+"id integer primary key autoincrement,"+
                WordDbSchema.WordTable.Columns.ENGLISH+","+
                WordDbSchema.WordTable.Columns.CHINESE+","+
                WordDbSchema.WordTable.Columns.DESCRIPTION+ ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //不更新
    }
}
5.Word.java
public class Word {
    private int id;
    private String english;
    private String chinese;
    private String description;

    public Word() {
    }

    public Word(int id, String english, String chinese, String description) {
        this.id = id;
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public Word(String english, String chinese, String description) {
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "单词ID:" + id + " 英文:" + english + " 中文:" + chinese + " 描述:" + description;
    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

6.WordContentProvider.java
public class WordContentProvider extends ContentProvider {
    private Context mContext;
    private SQLiteDatabase mDatabase;

    //创建authority,授权
    public static final String AUTHORITY = "com.example.providerapp.WordContentProvider";
    //创建UriMatcher对象
    private static UriMatcher sUriMatcher;

    //静态代码块
    static {
        //实例化UriMatcher对象
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

        //匹配uri
        //word是表名
        sUriMatcher.addURI(AUTHORITY, "word", 1);
        //匹配数字
        sUriMatcher.addURI(AUTHORITY, "word/#", 2);
        //匹配文本
        sUriMatcher.addURI(AUTHORITY, "word/*", 3);
        //匹配文本
        sUriMatcher.addURI(AUTHORITY, "word/*/*", 4);

    }

    @Override
    public boolean onCreate() {
        //在这里打开对应数据库
        mContext = this.getContext();
        mDatabase = new WordSqlHelper(mContext).getWritableDatabase();

        return true;
    }

    public WordContentProvider() { }

    /**
     * 这里提供了三种删除方式,通过id,通过名字,啥也没有就是全删除
     * */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
       int code = sUriMatcher.match(uri);
       int result = 0;
       switch (code){
           case UriMatcher.NO_MATCH:
               break;
           case 1://删除表中所有记录
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,null,null);
               break;
           case 2://投机取巧了一下
               // 按id删除
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,WordDbSchema.WordTable.Columns.ID+" = ? ",
                       new String[]{ContentUris.parseId(uri) + ""});
               break;
           case 3:
               //按english删除
               result = mDatabase.delete(WordDbSchema.WordTable.TABLE_NAME,WordDbSchema.WordTable.Columns.ENGLISH+" = ? ",
                       new String[]{uri.getPathSegments().get(1)});
               break;
       }

       return result;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 插入数据,数据一开始就得values
     * */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = sUriMatcher.match(uri);
        Uri result = null;
        switch (code){
            case 1://插入
                long index = mDatabase.insert(WordDbSchema.WordTable.TABLE_NAME,null,values);
                if (index > 0){
                    //插入成功,在前面已经有的uri后面追加index
                    result = ContentUris.withAppendedId(uri,index);
                    //通知数据已经发生改变
                    getContext().getContentResolver().notifyChange(result,null);
                    return result;
                }
                break;
        }
        return null;
    }


    /**
     * 查找单词信息
     * */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = sUriMatcher.match(uri);
        Cursor result = null;

        switch (code){
            case 1://查询所有记录
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case 2://根据id查询记录
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,WordDbSchema.WordTable.Columns.ID+" = ? ",
                        new String[]{ ContentUris.parseId(uri) + "" },null,null,sortOrder);
                break;
            case 3://根据English字段查询
                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
                        projection,WordDbSchema.WordTable.Columns.ENGLISH + " = ? ",
                        new String[]{uri.getPathSegments().get(1)},null,null,sortOrder);
                break;
//            case 4://根据指定的字段名查询记录,第一个是字段名,第二个是参数
//                result = mDatabase.query(WordDbSchema.WordTable.TABLE_NAME,
//                        projection,uri.getPathSegments().get(1) + " = ? ",
//                        new String[]{uri.getPathSegments().get(2)},null,null,sortOrder);
//                break;
        }
        return result;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = sUriMatcher.match(uri);
        int result = 0;
        switch (code) {
            case 2://根据指定id更新记录
                result = mDatabase.update(WordDbSchema.WordTable.TABLE_NAME,values,
                        WordDbSchema.WordTable.Columns.ID+" = ? ",new String[]{ContentUris.parseId(uri) + ""});
        }
        return result;
    }


    //私有方法,返回一个ContentValues对象
    private ContentValues getContentValues(Word word){
        ContentValues values = new ContentValues();
        //添加键值对
        values.put(WordDbSchema.WordTable.Columns.ENGLISH,word.getEnglish());
        values.put(WordDbSchema.WordTable.Columns.CHINESE,word.getChinese());
        values.put(WordDbSchema.WordTable.Columns.DESCRIPTION,word.getDescription());
        return values;
    }


}
7.MainActivity.java
public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private TextView mTextView;

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

        mButton = findViewById(R.id.update);
        mTextView = findViewById(R.id.show);

        /**
         * 简单更新数据
         * */
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此uri用于查询所有word
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));


            }
        });

    }

    /**
     * 获取arrayList对象
     * */
    private ArrayList<Word> getWords(Cursor cursor){
        ArrayList<Word> words = new ArrayList<>();
        WordCursorWrapper wrapper = new WordCursorWrapper(cursor);

        try {
            wrapper.moveToFirst();
            while (!wrapper.isAfterLast()){
                words.add(wrapper.getWord());
                wrapper.moveToNext();
            }
        } finally {
            wrapper.close();
        }
        return words;
    }

    /**
     * 将列表展开成字符串
     * */
    private String getString(ArrayList<Word> words){
        String res = "";
        for (Word word:words){
            res += word.toString()+"\n";
        }
        return res;
    }



}
8.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/show"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_gravity="bottom"
        android:id="@+id/update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="更新数据"/>

LinearLayout>

ResoverApp

1.WordCursorWrapper.java
public class WordCursorWrapper extends CursorWrapper {
    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public WordCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Word getWord(){
        //获取数据
        int id = getInt(getColumnIndex(WordDbSchema.WordTable.Columns.ID));
        String english = getString(getColumnIndex(WordDbSchema.WordTable.Columns.ENGLISH));
        String chinese = getString(getColumnIndex(WordDbSchema.WordTable.Columns.CHINESE));
        String description = getString(getColumnIndex(WordDbSchema.WordTable.Columns.DESCRIPTION));

        return new Word(id,english,chinese,description);
    }
}
2.WordDbSchema.java
public class WordDbSchema {
    public static final class WordTable{
        //定义表名
        public static final String TABLE_NAME = "words";

        //定义数据表字段
        public static final class Columns{
            public static final String ID = "id";
            public static final String ENGLISH = "english";
            public static final String CHINESE = "chinese";
            public static final String DESCRIPTION = "description";
        }
    }
}

3.Word.java
public class Word {
    private int id;
    private String english;
    private String chinese;
    private String description;

    public Word() {
    }

    public Word(int id, String english, String chinese, String description) {
        this.id = id;
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public Word(String english, String chinese, String description) {
        this.english = english;
        this.chinese = chinese;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "单词ID:" + id + " 英文:" + english + " 中文:" + chinese + " 描述:" + description;
    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
4.MainActivity.java
public class MainActivity extends AppCompatActivity {

    private EditText mEditTextID,mEditTextEnglish,mEditTextChinese,mEditTextDescription;
    private Button mButtonAdd,mButtonUpdate,mButtonDeleteAll,mButtonDeleteByEnglish,mButtonDeleteByID,
    mButtonFindAll,mButtonFindByEnglish,mButtonFindByID;
    private TextView mTextView;

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

        initView();

        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();
                String chinese = mEditTextChinese.getText().toString();
                String description = mEditTextDescription.getText().toString();

                ContentValues values = new ContentValues();
                values.put("english",english);
                values.put("chinese",chinese);
                values.put("description",description);

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();

                Uri result = contentResolver.insert(uri,values);
                if(result!=null){
                    Toast.makeText(MainActivity.this,
                            "插入成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,"插入失败!",
                            Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();

                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "删除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "删除失败!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteByID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = Integer.parseInt(mEditTextID.getText().toString());

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "删除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "删除失败!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonDeleteByEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+english);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.delete(uri,null,null);
                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "删除成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "删除失败!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();
                String chinese = mEditTextChinese.getText().toString();
                String description = mEditTextDescription.getText().toString();

                ContentValues values = new ContentValues();
                values.put(WordDbSchema.WordTable.Columns.ENGLISH,english);
                values.put(WordDbSchema.WordTable.Columns.CHINESE,chinese);
                values.put(WordDbSchema.WordTable.Columns.DESCRIPTION,description);

                String id = mEditTextID.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();
                int result = contentResolver.update(uri,values,null,null);

                if (result > 0){
                    Toast.makeText(MainActivity.this,
                            "更新成功!",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(MainActivity.this,
                            "更新失败!",Toast.LENGTH_LONG).show();
                }
                cleanEditText();
            }
        });

        mButtonFindAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此uri用于查询所有word
                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word");
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });

        mButtonFindByEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String english = mEditTextEnglish.getText().toString();

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+english);
                ContentResolver contentResolver = getContentResolver();

                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });

        mButtonFindByID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = Integer.parseInt(mEditTextID.getText().toString());

                Uri uri = Uri.parse("content://com.example.providerapp.WordContentProvider/word/"+id);
                ContentResolver contentResolver = getContentResolver();

                Cursor cursor = contentResolver.query(uri,
                        new String[]{WordDbSchema.WordTable.Columns.ID,
                                WordDbSchema.WordTable.Columns.ENGLISH,
                                WordDbSchema.WordTable.Columns.CHINESE,
                                WordDbSchema.WordTable.Columns.DESCRIPTION,}, null, null, null);

                ArrayList<Word> words = getWords(cursor);
                mTextView.setText(getString(words));
                cleanEditText();
            }
        });


    }

    /**
     * 获取arrayList对象
     * */
    private ArrayList<Word> getWords(Cursor cursor){
        ArrayList<Word> words = new ArrayList<>();
        WordCursorWrapper wrapper = new WordCursorWrapper(cursor);

        try {
            wrapper.moveToFirst();
            while (!wrapper.isAfterLast()){
                words.add(wrapper.getWord());
                wrapper.moveToNext();
            }
        } finally {
            wrapper.close();
        }
        return words;
    }

    /**
     * 将列表展开成字符串
     * */
    private String getString(ArrayList<Word> words){
        String res = "";
        for (Word word:words){
            res += word.toString()+"\n";
        }
        return res;
    }

    private void cleanEditText(){
        mEditTextID.setText("");
        mEditTextChinese.setText("");
        mEditTextEnglish.setText("");
        mEditTextDescription.setText("");
    }

    private void initView(){
        mEditTextID = findViewById(R.id.edit_id);
        mEditTextEnglish = findViewById(R.id.edit_english);
        mEditTextChinese = findViewById(R.id.edit_chinese);
        mEditTextDescription = findViewById(R.id.edit_description);

        mButtonAdd = findViewById(R.id.button_add);
        mButtonUpdate = findViewById(R.id.button_update);

        mButtonDeleteAll = findViewById(R.id.button_delete_all);
        mButtonDeleteByEnglish = findViewById(R.id.button_delete_by_english);
        mButtonDeleteByID = findViewById(R.id.button_delete_by_id);

        mButtonFindAll = findViewById(R.id.button_find_all);
        mButtonFindByEnglish = findViewById(R.id.button_find_by_english);
        mButtonFindByID = findViewById(R.id.button_find_by_id);

        mTextView = findViewById(R.id.show);
    }
}
5.activity_main.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#CFE7FA">

        <LinearLayout
            android:layout_centerInParent="true"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="英文:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_english"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加或按英文查找、删除时填写"/>

            LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="中文:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_chinese"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加数据时填写"/>

            LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text="描述:"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_description"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="添加数据时填写"/>

            LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <TextView
                    android:text=" I D :"
                    android:layout_marginLeft="10dp"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <EditText
                    android:id="@+id/edit_id"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:textSize="12dp"
                    android:hint="按ID查询、修改、删除时填写"/>

            LinearLayout>
        LinearLayout>
    RelativeLayout>
    <TextView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="0dp"
            android:layout_weight="1">
            <Button
                android:id="@+id/button_add"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#BDF8F8"
                android:text="添加数据"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
        LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/button_delete_all"
                android:text="全部删除"
                android:layout_marginLeft="5dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"/>
            <Button
                android:id="@+id/button_delete_by_id"
                android:text="按ID删除"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"/>
            <Button
                android:id="@+id/button_delete_by_english"
                android:text="按英文删除"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#ECF0DD"
                android:layout_marginRight="5dp"/>

        LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/button_update"
                android:background="#D4F5C1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="修改数据"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
        LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            >
            <Button
                android:id="@+id/button_find_all"
                android:text="查询全部"
                android:layout_marginLeft="5dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1" />
            <Button
                android:id="@+id/button_find_by_id"
                android:text="按ID查询"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1"/>
            <Button
                android:id="@+id/button_find_by_english"
                android:text="按英文查询"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#EEE0F1"
                android:layout_marginRight="5dp"/>
        LinearLayout>


    LinearLayout>

LinearLayout>

总结

如果有什么问题或改进方案,请私信联系我或者在评论区留言
码字不易,若有帮助,给个关注和赞呗

在这里插入图片描述

你可能感兴趣的:(安卓学习笔记)