2016年8月11日 星期四

why after rotate ,will have error ?

why after rotate ,will have error  ?

FATAL EXCEPTION: main
                                                                           Process: pom2.poly.com.trythemoviedbapi, PID: 798
                                                                           java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM movie
                                                                               at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                               at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
                                                                               at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:151)
                                                                               at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:123)
                                                                               at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:236)
                                                                               at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:197)
                                                                               at pom2.poly.com.trythemoviedbapi.MyRecyclerViewAdapter.onBindViewHolder(MyRecyclerViewAdapter.java:57)
                                                                               at pom2.poly.com.trythemoviedbapi.MyRecyclerViewAdapter.onBindViewHolder(MyRecyclerViewAdapter.java:18)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5212)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5245)


it is because the program want to mcursor.moveToPosition(position);,However, the mcursor already is coled. But why the cursor closed? the cursor get from the cursor loder should be kepp alive.No, it is not keep alive,  it jsut say after rotate,it will go to onLoadFinished directly.

after rotate,the onLoadFinished bennig call few time,so the myrecycleViewadapter.swapCursor(data); call too,

at here,may be ssome cursor is closed,so,have error,

soultion:
Method1.
Before use the cursor,check it it clise,,  !mcursor.isClosed()

Method 2

why the onLoadFinished call few time? one reson is it will call by MainActivity and MainFragment itself,Total 10

How to arage the recycle view better in the StaggeredGridLayoutManager?

How to arrange the recycle view better in the StaggeredGridLayoutManager?


the concept is:need to know the width of the screen first,that calculate each column width

how to get the width of a fragment?


int mHeaderViewHeight;
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
       new OnGlobalLayoutListener() {
           @Override
           public void onGlobalLayout() {
                                                                                                                                                                                                                                     
               mHeaderViewHeight = mHeaderView.getHeight();
               getViewTreeObserver()
                       .removeGlobalOnLayoutListener(this);
           }
       });

calculate the right width of the cell,then set it


imv.getLayoutParams().width= (int) cellWidth;
       imv.getLayoutParams().height= (int) (cellWidth*3/2);

to handle the Landscape and Portrait

when Landscape

nee to return the height as width

when Portrait

use the width as width


getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE



SpacesItemDecoration extends RecyclerView.ItemDecoration

set the Decoration between the item

2016年8月1日 星期一

External storage

Basic:

have private and public, if private,when the app delete,the file will be deleted too. Save at both place ,other application can read too.

Important:

it is because the external storage will be hang up,so when to read or write , we need to check first

How to check:


Environment.getExternalStorageState()

Summary
Constants
String MEDIA_BAD_REMOVAL
Storage state if the media was removed before it was unmounted.
String MEDIA_CHECKING
Storage state if the media is present and being disk-checked.
String MEDIA_EJECTING
Storage state if the media is in the process of being ejected.
String MEDIA_MOUNTED
Storage state if the media is present and mounted at its mount point with read/write access.
String MEDIA_MOUNTED_READ_ONLY
Storage state if the media is present and mounted at its mount point with read-only access.
String MEDIA_NOFS
Storage state if the media is present but is blank or is using an unsupported filesystem.
String MEDIA_REMOVED
Storage state if the media is not present.
String MEDIA_SHARED
Storage state if the media is present not mounted, and shared via USB mass storage.
String MEDIA_UNKNOWN
Unknown storage state, such as when a path isn't backed by known storage media.
String MEDIA_UNMOUNTABLE
Storage state if the media is present but cannot be mounted.
String MEDIA_UNMOUNTED
Storage state if the media is present but not mounted.

Permission need:

write
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
read:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />



Write:

//        if private
        File externalDir = this.getExternalFilesDir(null);

//        if public
//        File externalPublicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);

        File file = new File(externalDir, name);

FileOutputStream outPutStraem = new FileOutputStream(file, false);
            outPutStraem.write(input.getBytes());
            outPutStraem.close();

Read:


File externalDir = this.getExternalFilesDir(null);
        File file = new File(externalDir, name);
        try {
            StringBuilder sb = new StringBuilder();
            FileInputStream inputStream = new FileInputStream(file);
            int charr;
            while ((charr = inputStream.read()) != -1) {
                sb.append((char)charr);
            }
            inputStream.close();
            return sb.toString();



2016年7月27日 星期三

Recyclerview after rotate,the iteam sort has problem

solved

1. use the new version
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'


2.

after swap the data,create a new layout manager

myrecycleViewadapter.swapCursor(data); 
        } 

        if (mainActivity.getIsTwoPlanMode() && !isPortrait()) { 
            mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); 
        } else { 
            mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
        } 

        myRecyclerView.setLayoutManager(mLayoutManager); 

2016年7月14日 星期四

2016年7月10日 星期日

Accessibility of the android application

Accessibility of the android application
the four step of accessibility
  1. Add descriptive text to user interface controls in your application using the android:contentDescription attribute. Pay particular attention to ImageButton, ImageView and CheckBox.
  2. Make sure that all user interface elements that can accept input (touches or typing) can be reached with a directional controller, such as a trackball, D-pad (physical or virtual) or navigationgestures .
  3. Make sure that audio prompts are always accompanied by another visual prompt or notification, to assist users who are deaf or hard of hearing.
  4. Test your application using only accessibility navigation services and features. Turn on TalkBack and Explore by Touch, and then try using your application using only directional controls. For more information on testing for accessibility, see the Accessibility Testing Checklist.

Content Describtion
important for:
check box
Image button
image view

decorative graphic ‘s contentDescription set to null

Text field hints: For EditText fields, provide an android:hint attribute instead of a content description, to help users understand what content is expected when the text field is empty and allow the contents of the field to be spoken when it is filled.



in XML:
<ImageButton
   android:id=”@+id/add_note_button”
   android:src=”@drawable/add_note”
   android:contentDescription=”@string/add_note”/>


in java code:

setContentDescription(CharSequence)

2016年6月17日 星期五

ProgressDialog

Declare your progress dialog:
ProgressDialog progress;
When you're ready to start the progress dialog:
progress = ProgressDialog.show(this, "dialog title",
    "dialog message", true);
and to make it go away when you're done:
progress.dismiss();