共享首选项仅查找列表中的第一个条目

时间:2014-10-16 16:55:19

标签: android eclipse eclipse-indigo

我试图在"报告活动中显示一些减法问题。"

当我只使用捆绑包和" getIntExtra"它工作正常,我得到了所有的问题。

然而,当我将该代码注释掉并创建一个SharedPReferences类,并使用与#34;共享首选项相同的结构时,"问题被添加到数组中,它似乎被添加到列表中,但是"列表大小"是一个。

我想知道为什么注释掉的代码使用了正确的列表大小,但从SharedPreferences获取相同的东西并不是(为什么我清除列表的所有尝试似乎都没有?工作;列出的一个问题总是相同的;我担心这些问题的集合越来越多)。

int numQuestions = prefState.getInt(getPackageName() + ".numQuestions", 0);

    Log.i(getClass().getSimpleName(), "Num Questions: " + numQuestions);
       // this gives me ONE even though         
    for (int i = 0; i < numQuestions; ++i)
    {
        int minuendAnswer =prefState.getInt(getPackageName() + ".minuendAnswer" + i, 0);
         // more of these 


        Log.i(getClass().getSimpleName(), "Minuend Answer: " + minuendAnswer);


        // add to list
        StringBuilder str = new StringBuilder();
        str.append("Question #: " + (i+1) + '\n');
        str.append("Subtrahend, minuend, and difference answers: "
        + minuendAnswer + " - " + subtrahendAnswer + " = " + differenceAnswer + '\n');
        str.append("Subtrahend, minuend, and difference Responses: "
                + minuendResponse + " - " + subtrahendResponse + " = " + differenceResponse + '\n');

        listItems.add(str.toString());
        Log.i(getClass().getSimpleName(), " ListItems List Size: " + listItems.size()); 

    }

    /*
     * 
     *           bundling part
     * 
     * int numQuestions = getIntent().getIntExtra(getPackageName() + ".numQuestions", 0);

    Log.i(getClass().getSimpleName(), "Num Questions: " + numQuestions);

    for (int i = 0; i < numQuestions; ++i)
    {
        int minuendAnswer = getIntent().getIntExtra(getPackageName() + ".minuendAnswer" + i, 0);
         // and some more of those


        Log.i(getClass().getSimpleName(), "Minuend Answer: " + minuendAnswer);
         // and some more of those
        // add to list
        StringBuilder str = new StringBuilder();
        str.append("Question #: " + (i+1) + '\n');
        str.append("Subtrahend, minuend, and difference answers: "
        + minuendAnswer + " - " + subtrahendAnswer + " = " + differenceAnswer + '\n');
        str.append("Subtrahend, minuend, and difference Responses: "
                + minuendResponse + " - " + subtrahendResponse + " = " + differenceResponse + '\n');

        listItems.add(str.toString());



    }
    */ 


private void putQuestionsPreferences()
{
    SharedPreferences state = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);  

    SharedPreferences.Editor editor = state.edit();

    editor.putInt(getPackageName() + ".numQuestions", labelQuestions.size());
     Log.i("sp editor says numQ is:  ", Integer.toString( labelQuestions.size())); 

    int i = 0;
    for(Iterator<LabelTestQuestion> it = labelQuestions.iterator(); it.hasNext();)
    {
        LabelTestQuestion tmp = it.next();
        editor.putInt(getPackageName() + ".minuendAnswer" + i, tmp.getMinuendAnswer());
        editor.putInt(getPackageName() + ".subtrahendAnswer" + i, tmp.getSubtrahendAnswer());
        editor.putInt(getPackageName() + ".differenceAnswer" + i, tmp.getDifferenceAnswer());
        editor.putInt(getPackageName() + ".minuendResponse" + i, tmp.getMinuendResponse());
        editor.putInt(getPackageName() + ".subtrahendResponse" + i, tmp.getSubtrahendResponse());
        editor.putInt(getPackageName() + ".differenceResponse" + i, tmp.getDifferenceResponse());

        ++i;   // remember that, my dear ;) 

    }

    editor.commit();      
}private void getQuestionsPreferences() {
    labelQuestions.clear();

    // Get Shared Preferences
    SharedPreferences prefState = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 

    // get test questions from the shared preferences

    int numQuestions = prefState.getInt(getPackageName() + ".numQuestions", 0);

    for (int i = 0; i < numQuestions; ++i)
    {
        labelq = new LabelTestQuestion();

        // get the test question data
        int minuendAnswer = prefState.getInt(getPackageName() + ".minuendAnswer" +i, 0);
        int subtrahendAnswer = prefState.getInt(getPackageName() + ".subtrahendAnswer" +i, 0);
        int differenceAnswer = prefState.getInt(getPackageName() + ".differenceAnswer" +i, 0);
        int minuendResponse = prefState.getInt(getPackageName() + ".minuendResponse" +i, 0);
        int subtrahendResponse = prefState.getInt(getPackageName() + ".subtrahendResponse" +i, 0);
        int differenceResponse = prefState.getInt(getPackageName() + ".differenceResponse" +i, 0);

        labelq.setMinuendAnswer(minuendAnswer);
        labelq.setSubtrahendAnswer(subtrahendAnswer);
        labelq.setDifferenceAnswer(differenceAnswer);
        labelq.setMinuendResponse(minuendResponse);
        labelq.setSubtrahendResponse(subtrahendResponse);
        labelq.setDifferenceResponse(differenceResponse);

        labelQuestions.add(labelq);
    }


};

0 个答案:

没有答案