Android app, how to use cursor in multiple methods.

Asked 2 years ago, Updated 2 years ago, 51 views

Hi, everyone.I am currently creating an Android image slide show application.To give you an overview, we obtain images saved on the actual machine with cursor and switch between images by playing, advancing, and returning buttons on the screen. Currently, we are preparing methods for each button to perform various operations.The thing that stumbles at the moment is that the image does not switch even if you press the forward/back button.There are no errors.
Here's the code:

package jp.ooooooo.hojun.bun.autoslideshowapp;

import android.Manifest;
import android.content.ContentResolver;
import android.content.contentUris;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extensions AppCompatActivity{

Button mStartPauseButton;
Button mForwardButton;
Button mBackButton;
Cursor cursor;

private static final int PERMISSIONS_REQUEST_CODE=100;

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

    mStartPauseButton=findViewById (R.id.button1);
    mForwardButton=findViewById (R.id.button2);
    mBackButton=findViewById (R.id.button3);

    mForwardButton.setOnClickListener(newView.OnClickListener(){
        @ Override
        public void onClick (View v) {
             getNextInfo();
        }
    });

    mBackButton.setOnClickListener(newView.OnClickListener(){
        @ Override
        public void onClick (View v) {
             getPreviousInfo();
        }
    });

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
        if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            getContentsInfo();
        } else{
            requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_CODE);
        }
    } else{
        getContentsInfo();
    }
}

@ Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch(requestCode){
        case PERMISSIONS_REQUEST_CODE:
            if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                getContentsInfo();
            }
            break;
        default:
            break;
    }
}

private void getContentsInfo(){
    ContentResolver resolver=getContentResolver();
    cursor=resolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            Null,
            Null,
            Null,
            Null
    );

     cursor.moveToFirst();
        int fieldIndex=cursor.getColumnIndex(MediaStore.Images.Media._ID);
        Long id=cursor.getLong(fieldIndex);
        UriimageUri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

        ImageView imageView=findViewById (R.id.imageView);
        imageVIew.setImageURI(imageUri);
}
private void getNextInfo(){
    ContentResolver resolver=getContentResolver();
    cursor=resolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            Null,
            Null,
            Null,
            Null
    );

    cursor.moveToFirst();
    int fieldIndex=cursor.getColumnIndex(MediaStore.Images.Media._ID);
    Long id=cursor.getLong(fieldIndex);
    UriimageUri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

    ImageView imageView=findViewById (R.id.imageView);
    imageVIew.setImageURI(imageUri);
}

private void getPreviousInfo(){
    ContentResolver resolver=getContentResolver();
    cursor=resolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            Null,
            Null,
            Null,
            Null
    );

    cursor.moveToFirst();
    int fieldIndex=cursor.getColumnIndex(MediaStore.Images.Media._ID);
    Long id=cursor.getLong(fieldIndex);
    UriimageUri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

    ImageView imageView=findViewById (R.id.imageView);
    imageVIew.setImageURI(imageUri);
}


@ Override
protected void onStop() {
    super.onStop();
    cursor.close();
}
}

How can I get the next or previous image?

android java

2022-09-30 20:17

1 Answers

There are two main points to understand.

1. is the basic content of the programming language, so I will omit the detailed explanation.
The cursor is declared as an instance variable, so you can view any questionnaire code from any method.
However, getNextInfo()/getPreviousInfo, so I am referring to a different instance.

2. It may be easy to understand if you imagine Cursor as an arrow to the data list.

+-------+
|data1| —moveToFirst()
+-----+
|data2| —moveToPrevious()
+-----+
| data3 | ← Cursor position
+-----+
|data4|:moveToNext()
+-----+
|data5| —moveToLast()
+-----+

If Cursor is currently pointing to data3, the Next/Previous moves the arrow to point to the following and forward, respectively.
The arrows move so that First and Last point to the beginning and end of the data list, respectively.

The code in the questionnaire retrieves the Cursor instance again each time and points to the beginning with moveToFirst, so only the same data is retrieved.
If you ignore the error handling and simply write the code:
(The displayed image is looped at the beginning and end.)

private void getNextInfo(){
    if(!cursor.moveToNext()){
        cursor.moveToFirst();
    }

    int fieldIndex=cursor.getColumnIndex(MediaStore.Images.Media._ID);
    Long id=cursor.getLong(fieldIndex);
    UriimageUri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

    ImageView imageView=findViewById (R.id.imageView);
    imageVIew.setImageURI(imageUri);
}

private void getPreviousInfo(){
    if(!cursor.moveToPrevious()){
        cursor.moveToLast();
    }

    int fieldIndex=cursor.getColumnIndex(MediaStore.Images.Media._ID);
    Long id=cursor.getLong(fieldIndex);
    UriimageUri=ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

    ImageView imageView=findViewById (R.id.imageView);
    imageVIew.setImageURI(imageUri);
}


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.