Fork me on GitHub
image frame

Android Database Sugar 简单实用

Android Database Sugar 简单实用

在看过三个版本(对就是版本)greenDao教程最终发现AIDE无法使用带有注解的库的时候,我是稍有点绝望的。

同时也萌发了自己对网络教程的感慨:还是看官方文档吧官方文档我爱你。

然后呢,不想用sqlite只好自己继续寻数据库了,于是找到了sugar。

正文

先把github放出来,很多东西自己看就够了。

github : http://satyan.github.io/sugar/

ps : 在我自己浏览的时候好像没有看见需要在使用开始和结束的时候调用:

1
2
SugarContext.init(this);
SugarContext.terminate();

这两个方法。

在github上有下载,我用的是gradle。

接下来

简单操作

插入

  • 创建实例
    只需要新建一个类来继承 SugarRecord这个类,然后在类中声明变量等即可,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class MyRed extends SugarRecord
{
private String mDesc;

private String mUrl;

private String mWho;

private String mThink;

public MyRed(){

}

public MyRed(String desc, String url, String who, String think){
setDesc(desc);
setUrl(url);
setWho(who);
setThink(think);
}

public void setThink(String mThink)
{
this.mThink = mThink;
}

public String getThink()
{
return mThink;
}


public void setDesc(String mDesc)
{
this.mDesc = mDesc;
}

public String getDesc()
{
return mDesc;
}

public void setUrl(String mUrl)
{
this.mUrl = mUrl;
}

public String getUrl()
{
return mUrl;
}

public void setWho(String mWho)
{
this.mWho = mWho;
}

public String getWho()
{
return mWho;
}}
  • 在AndroidManifest.xml中配置
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfdykme.mered.myred">


<meta-data android:name="DATABASE" android:value="sugar_myred.db" />
<meta-data android:name="VERSION" android:value="1" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.kfykme.sugar" />


</manifest>

android:value 的值随自己需要更改。

  • 初始化
    在使用前需要调用上文提到的两个方法。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SugarContext.init(this);


    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    @Override
    protected void onDestroy()
    {
    // TODO: Implement this method
    super.onDestroy();

    SugarContext.terminate();

    }

我是在主activity的onCreate() 和onDestory()中调用的方法。

  1. 保存
    需要调用.save()方法。
1
2
3
4
5
6
mMyRedPresenter.saveMyRed(
new MyRed(
mEditTextDesc.getText().toString(),
mEditTextUrl.getText().toString(),
mEditTextWho.getText().toString(),
mEditTextThink.getText().toString()));

将四个EditText中的文本作为参数,new一个 MyRed ,然后传入方法saveMyRed中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void saveMyRed(MyRed myRed)
{
if(!myRed.getDesc().isEmpty()
&& (myRed.getUrl().length() > 5)
&& !myRed.getWho().isEmpty()){

myRed.save();
Toast.makeText(mView,"Saved",Toast.LENGTH_SHORT).show();
} else {

Toast.makeText(mView,"Saved failed",Toast.LENGTH_SHORT).show();
}
// TODO: Implement this method
}

稍作判断后觉定是否存入数据库。

查询

嗯 通过调用MyRed的

1
MyRed.listAll(MyRed.class);

方法即可获取一个List 然后查询操作请随意。

更新

在通过listAll()获取的List中获取MyRed类,然后用setXXX()方法改变对应值后再使用MyRed.save()方法即可。
MyRed.save()会自动判断是新数据还是更新数据。

删除

同上获取MyRed类变量后调用MyRed.delete()即可。

备注1

简单使用嘛,就这样了。

备注2
空构造空构造方法很重要  
备注3
下一次研究一下如何建立多个表

Android NavigationView

NavigationView (1)

Today I learnt how to use navigationview to make a esay 导航栏

没错我还想顺便练练英语来着

And I plan to write down some notes.
Write in this order:

蹩脚 努力吧少年

需要写三个布局
menu
header of navigationview
your main layout

menu/draw_view.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">

<item
android:id="@+id/nav_home"
android:title=" Home"
android:icon="@drawable/image_1"/>

<item
android:id="@+id/nav_message"
android:title=" Messages"/>
<item
android:id="@+id/nav_friends"
android:title="F riends"/>
<item
android:id="@+id/nav__discussion"
android:title=" Discussion"/>

</group>
<item android:title="Sub items">
<menu>
<item
android:title="Sub item 1"/>
<item
android:title="Sub item 2"/>
</menu>
</item>

</menu>

header layout

layout/nav_header.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:paddingTop="30dp"
android:paddingLeft=" 16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:gravity="center|left"
android:orientation="vertical">

<ImageView
android:id="@+id/avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_head"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="kfdykme"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>

main layout

layout/main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/id_drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>

<TextView
android:id="@+id/id_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="HelloWorld"
android:textSize="30sp"/>


<android.support.design.widget.NavigationView
android:id="@+id/id_nv_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/draw_view"
/>

</android.support.v4.widget.DrawerLayout>

在activity 中实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

package com.kfdykme.view.navigationview;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.*;
import android.support.v4.view.*;
import android.support.v7.app.*;


public class Main extends AppCompatActivity
{

private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawer_layout);
mNavigationView = (NavigationView) findViewById(R.id.id_nv_menu);


setupDrawerContent(mNavigationView);


}

private void setupDrawerContent(NavigationView navigationView)
{
navigationView.setNavigationItemSelectedListener(

new NavigationView.OnNavigationItemSelectedListener()
{

@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}

}
  • © 2020 Kfdykme
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信