跳至主要内容

博文

How to use Realm database in Kotlin

Hot to use Realm in android project. Befor you use realm we should be read documentation https://realm.io/docs/java/latest#getting-started Then we got the way how to install. Install Realm as a Gradle plugin. Setp 1:   change build.gradle in project level buildscript { repositories { jcenter() } dependencies { classpath "io.realm:realm-gradle-plugin:5.14.0" } } Step 2:   Apply the realm-android plugin to the top of the application level build.gradle file. Create reaml map Object There has two way for mapping Object Make   Object   extends   ReamlObject Or make   Object   implement   ReamlModel Declear reaml mapping objects Realm not support   data class   yet. So we can't use it now. And object will make it open, because Realm object will generate subclass after   aapt   or   kapt . // 1. open class User : RealmObject() { var id: String = UUID . random UUID() . to String() var name: String = "
最新博文

How to make ViewPager auto play with a simple recursion function in android

Today a new request for ViewPager, will set ViewPager auto play, in last code is user Handler, but I want a new way to finish it. private void autoPlay (ViewPager viewPager ) {     viewPager . postDelayed ( new Runnable () {         @Override         public void run () {             try {                 if ( adapter != null && viewPager.getAdapter() . getCount () > 0 ) {                     int position = currentCount % adapter . getCount ();                     currentCount ++;                     viewPager . setCurrentItem ( position );                     autoPlay ( viewPager);                 }             } catch ( Exception e ) {                 Log . e ( TAG , "auto scroll pager error." , e );             }         }     }, 3000 ); } So I do like that , user recursion is very simple and fast.  That is.

How to change ViewPager scroll animation duration and velocity

When you call change current selected view pager position you may call like this. // change current position default is viewPager.setCurrentItem(position, true); viewPager . setCurrentItem ( position ); There will show transaction animation for change position , but this animation is too fast. So I want change this animation. When I see source code I find this, like flow. where is the scroll animation and speed.     int duration = 0 ;     velocity = Math . abs ( velocity );     if ( velocity > 0 ) {         duration = 4 * Math . round ( 1000 * Math . abs ( distance / velocity ));     } else {         final float pageWidth = width * mAdapter . getPageWidth ( mCurItem );         final float pageDelta = ( float ) Math . abs ( dx ) / ( pageWidth + mPageMargin );         duration = ( int ) (( pageDelta + 1 ) * 100 );     }     duration = Math . min ( duration , MAX_SETTLE_DURATION );     mScroller . startScroll ( sx , sy , dx , d

how to set search view open when activity created.

I has create an activity for search something. but I want search view is searching status when I start this activity. So..... like this, I create search view from option menu. but  setIconified set value false for set first open activity search view is open . public void onCreate ( Bundle savedInstanceState ) {   super . onCreate ( savedInstanceState );  setContentView ( R . layout . xxx . xml )  configActionBar ();  loadData ();   if ( mSearchView != null && mSearchView . isIconified ()) {             mSearchView . setIconified ( false );   } } public boolean onCreateOptionsMenu ( Menu menu ) {  getSupportMenuInflater (). inflate ( R . menu . search_button , menu );  mSearchView = ( SearchView ) menu . findItem ( R . id . search ). getActionView ();   return super . onCreateOptionsMenu ( menu ); }

Android Sqlite INSERT OR REPLCACE index

Android Sqlite INSERT OR REPLCACE 主键非 _id 前题: 今天遇到一个问题,我们本地存了多条评论来自不同的表。 原本的设计是拿服务器上的id直接当成本地的 _id 字段,但是一样是来自不同的表,这样就会出现id有可能重复的问题,所以当我想本地和服务器同步数据时 使用 INSERT OR UPDATE 就会出现问题。 因为出现了上述问题,所以我就开始想之前的在的数据是怎么实现的。 于是 查看之前的代码。 "insert or replace into " + TABLE_NAME_CATEGORY_ARTICLES + "(" + FIELDS_CATEGORY_ARTICLES + ") values ” + "(?, ?, ?, ?, ?, ?, ?, ?)"; 从这段代码里没有发现任何用于判断是决定更新还是新建,所以再向前…… "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_CATEGORY_ARTICLES + "(_id INTEGER PRIMARY KEY," + "category TEXT, " + "title TEXT, " + "summary TEXT, " + "content TEXT, " + "created_at TIMESTAMP, " + "readFlg BOOLEAN, " + "thumbnail TEXT)"; 我们找到了建表语句,在这里只发现了,primary key, 再没有其它的东西了,所以我推测它应该是通过 _id 这个primary key来确定是否存在这条记录来决定 是更新还是新建。 这了确定我的推测是正确的,我google了一下,果然…… 回到我之前的问题,我们现在不能使用 _id 这个主键来确定是更新还是插入了。于是我就查一下文档,发现 sqlite 可以有另一种方式来约

How to debug Android without usb?

first you must make sure you have a wireless device and your android devices can connect your computer with ip address. If you have connected with usb ,you can go adb shell run this: adb tcpip 5555 Then you can connect with you network, like this : adb connect ip:5555    this is should be you phone ip. If you are want one key start,  there have a app named  wireless  you can download in play, so easy and good luck.

how to make your click event and handler easier to read in Android Activities

How to make your click event and handler easy to read. If you have one button event in your activity, you can add listener like this. final String test = "test"; findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show(); } }); But this is rarely.  Also you wrote it everywhere like this.  findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.button2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.button3).setOnClickListener(new OnClickListener() { @Override publ