【问题标题】:How to make an operation calls once如何进行一次操作调用
【发布时间】:2015-04-02 16:14:25
【问题描述】:

假设我有无限的浏览器,并且每个浏览器都被着色了。 因此,在关闭应用程序时,再次调用该操作,但我想要的是下次启动时不应再次调用该操作

【问题讨论】:

    标签: android android-fragments android-viewpager


    【解决方案1】:

    根据您的问题、此讨论和project 链接,试试这个:

    MainActivity 中添加这两个全局变量:

    /**
     * {@link SharedPreferences} key for the random int array.
     */
    public static final String KEY_RANDOM_INT_FOR_INDEX = "random_int_index";
    
    /**
     * Value holding the total number of images.
     */
    public static final int TOTAL_IMAGES = 8;
    

    然后在MainActivityonCreate的末尾再次添加:

    // Get a reference to the default SharedPreferences
    SharedPreferences defSharedPreference = PreferenceManager
            .getDefaultSharedPreferences(this);
    // Check if there is a random int for at least 1 index
    if (defSharedPreference.getInt(KEY_RANDOM_INT_FOR_INDEX + "_0", -1) == -1) {
        // Generate array of <TOTAL_IMAGES> random ints;
        ArrayList<Integer> allInts = new ArrayList<Integer>();
        for (int i = 0; i < TOTAL_IMAGES; i++) {
            allInts.add(i);
        }
        // Editor instance for modifying SharedPreferences;
        SharedPreferences.Editor sp_editor = defSharedPreference.edit();
        // Generate random ints;
        for (int i = 0; i < TOTAL_IMAGES; i++) {
            int range = allInts.size();
            int pickIndex = (int) (Math.random() * range);
            int value = allInts.remove(pickIndex);
            // System.out.print("\nindex: " + i + "\t=\t" + value);
            // Save the random value with the key;
            sp_editor.putInt(KEY_RANDOM_INT_FOR_INDEX + "_" + i, value);
        }
        // Save the editors queue in the SharedPreferences;
        sp_editor.commit();
    }
    

    最后在onCreateViewMyFragment 中添加:

    // Get a reference to the default SharedPreferences
    SharedPreferences defSharedPreference = PreferenceManager
            .getDefaultSharedPreferences(getActivity());
    // Calculate the index of the page from base of TOTAL_IMAGES;
    int pageIndexInTotalImages = (mCurrentPage - 1)
            % MainActivity.TOTAL_IMAGES;
    // Calculate the index of the random image for the page;
    int imageID = defSharedPreference.getInt(
            MainActivity.KEY_RANDOM_INT_FOR_INDEX + "_"
                    + pageIndexInTotalImages, 0);
    int resourceImageID = imageID + R.drawable.image0;
    

    假设您在myfragment_layout 中设置了ImageView,您可以使用以下方法加载图像:

    &lt;ImageView&gt;.setImageResource(resourceImageID);

    【讨论】:

    【解决方案2】:

    您应该在该操作之后使用 SharedPreferences 来存储布尔值“firstTime”。在循环之前恢复该值并更改图像数组。

    【讨论】:

    • 抱歉,智能手机应用程序,几小时后 :(
    • 请先生解决
    【解决方案3】:

    这是您设置共享首选项的方式:

        SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("page" + mCurrentPage, randInt);
        editor.commit();
    

    要检索它们,您需要执行以下操作:

        int randInt = preferences.getInt("page" + mCurrentPage, -1);
    

    代码:

        SharedPreferences preferences = getActivity().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        int randInt = preferences.getInt("page" + mCurrentPage, -1);
        if(randInt == -1) {
            randInt = new Random().nextInt((8));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("page" + mCurrentPage, randInt);
            editor.commit();
        }
    

    【讨论】:

    • 我知道如何设置共享偏好,但是对于以下问题我应该采取什么方法或如何做,因为我认为如果我在这里使用共享偏好,它只会为我拥有的一页设置图像10000 页在我的视图寻呼机
    • 你可以将生成的随机int与页码一起存储在sharedprefrences中
    • 我需要在片段中实现它所以我用什么替换上下文?
    • 可以使用:getActivity()
    • 它只为所有布局设置一个图像..这就是我所说的共享偏好在这种情况下没有用
    猜你喜欢
    • 2020-02-20
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多