简体   繁体   English

如何保存和恢复用户看到的最后一个图像视图?

[英]How can I save and restore the last Image View seen by the user?

I am currently developing an android music reading app in Android Studio.我目前正在 Android Studio 中开发 android 音乐阅读应用程序。 The language I am using is Java but I am a beginner (sorry for my English too).我使用的语言是 Java 但我是初学者(也对不起我的英语)。

The app has several exercises.该应用程序有几个练习。 Ideally, people can click on the next or back button to see one exercise or another.理想情况下,人们可以单击下一步或返回按钮来查看一个或另一个练习。 The problem is that I am also looking for the user, even if he exits the application, to continue in the last exercise (image) seen.问题是我也在寻找用户,即使他退出了应用程序,也要继续看到上一个练习(图像)。

I have read about the cycle of activities and I understand it, but since I am a beginner in the language I cannot do what I need to do.我已经阅读了有关活动循环的内容并且我理解它,但是由于我是该语言的初学者,所以我无法做我需要做的事情。 I have learned that OnSaveInstanceState or OnRestoreInstanceState do not solve my problem and that the solution may be by using SharedPreferences but I don't know how to apply it to the code I currently have.我了解到 OnSaveInstanceState 或 OnRestoreInstanceState 不能解决我的问题,解决方案可能是使用 SharedPreferences,但我不知道如何将其应用于我目前拥有的代码。 With SharedPreferences I hope I can save and restore the last imageview seen and that I can continue from there by clicking next or back.使用 SharedPreferences 我希望我可以保存和恢复上次看到的 imageview,并且我可以通过单击下一步或返回从那里继续。

This is the current code and it allows me to: show the initial exercise and go from one exercise to another.这是当前代码,它允许我:显示初始练习和 go 从一个练习到另一个练习。

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int image_index = 0 ;
    private static final int MAX_IMAGE_COUNT = 3;

    private int[] mImageIds = {
            R.raw.image1,
            R.raw.image2,
            R.raw.image3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showImage();
    }
    private void showImage() {
        ImageView imgView = (ImageView) findViewById(R.id.myimage);
        imgView.setImageResource(mImageIds[image_index]);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case (R.id.previous_btn):
                image_index--;
                if (image_index == -1) {
                    image_index = MAX_IMAGE_COUNT - 1;
                } else {
                }
                showImage();
                break;

            case (R.id.next_btn):
                image_index++;
                if (image_index == MAX_IMAGE_COUNT) {
                    image_index = 0;
                } else {
                }
                showImage();
                break;
        }
    }
}

This is the xml这是 xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:fontFamily="@font/gilroyextrabold"
                android:text="Ejercicios"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/myimage"
                android:layout_width="wrap_content"
                android:layout_height="370dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/myimage">

                <Button
                    android:id="@+id/previous_btn"
                    android:layout_width="190dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/btnatsslf"
                    android:onClick="onClick" />

                <Button
                    android:id="@+id/next_btn"
                    android:layout_width="190dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/btnsgtslf"
                    android:onClick="onClick" />
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</ScrollView>

As I don't have that much knowledge, I seek your help and I also receive alternatives.由于我没有那么多知识,我寻求你的帮助,我也得到了替代品。

put these lines after setContentView , but before showImage in onCreate将这些行放在setContentView之后,但在onCreate中的showImage之前

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    image_index = sp.getInt("image_index", image_index);

and save this value inside showImage just after setImageResource line并在setImageResource行之后将此值保存在showImage

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    sp.edit().putInt("image_index", image_index).apply();

you may keep reference to SharedPreferences sp as class member, so it won't be needed to obtain every time when showImage is called (also ImageView should be stored same way without need of findViewById everytime).您可以将SharedPreferences sp引用为 class 成员,因此每次调用showImage时都不需要获取它( ImageView也应该以相同的方式存储,而不需要每次都使用findViewById )。 also the key ( "image_index" ) may be stored as some private static final String class member密钥( "image_index" )也可以存储为一些private static final String class 成员

also remember for future use: this is index in some array you have declared static.还要记住以备将来使用:这是您已声明 static 的某个数组中的索引。 when you change it, eg stored value is 2 and you cut last item in images array then user get index out of bounds exception after app update installation, so inside onCreate check that this value is in range of array (just for safety)当您更改它时,例如存储值为 2 并且您剪切了图像数组中的最后一项,然后用户在应用程序更新安装后获取索引越界异常,因此在onCreate中检查该值是否在数组范围内(只是为了安全)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM