0

I am trying to store variables that are inputs (Strings) in a resultsArray. For some reason it is not progressing to fill the next spot. Only the first spot in results array is working. Any ideas? I am storing the string elements each time as you can see in if placecounter == 0 and then recalling once it runs again (placeCounter > 0) and trying to add one more element to it.

 if(placeCounterExplanation == 0){
                 CategoryDetailArray = new String[30];
                 CategoryDetailArray[0] = categoriesItem;

                 IndividualExpenseDetailArray = new String[30];
                 IndividualExpenseDetailArray[0] = individualamountitem;

                 ExplanationDetailArray = new String[30];
                 ExplanationDetailArray[0] = explanationItem;

                 DateofPurchaseArray = new String [30];
                 DateofPurchaseArray[0] = currentDateItem;
                // SharedPreferences sp = SharedPreferences.getDefaultSharedPreferences(this);
                //    SharedPreferences.Editor mEdit1 = sp.edit();
                 test.setText(ExplanationDetailArray[0]);
                 test2.setText(IndividualExpenseDetailArray[0]);
                // test3.setText(DateofPurchaseArray[0]);
                 test4.setText(CategoryDetailArray[0]);


                 //String placeCount = Integer.toString(placeCounterExplanation);

                //  test3.setText(placeCount);


                 //STORE
                    editor.putInt("Status_size", ExplanationDetailArray.length); /* sKey is an array */ 

                    for(int i=0;i<ExplanationDetailArray.length;i++)  
                    {
                        editor.remove("Explanation_" + i);
                        editor.remove("ExpenseAmount_" + i);
                        editor.remove("Category_" + i);
                        editor.remove("DOP_" + i);
                        editor.putString("Explanation_" + i, ExplanationDetailArray[i]);  
                        editor.putString("ExpenseAmount_" + i, IndividualExpenseDetailArray[i]);  
                        editor.putString("Category_" + i, CategoryDetailArray[i]);  
                        editor.putString("DOP_" + i, DateofPurchaseArray[i]);  
                    }

                     editor.commit();     
                }






             if(placeCounterExplanation > 0){

                 //SharedPreferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(this);


                 //RECEIVE
                   // ExplanationDetailArray.clear();
                    int size = pref.getInt("Status_size", 0);  






                    for(int i=0;i<size;i++) 
                    {
                       // ExplanationDetailArray.add(pref.getString("Explanation" + i, null));
                        ExplanationDetailArray[i] = pref.getString("Explanation_" + i, null);
                        IndividualExpenseDetailArray[i] = pref.getString("ExpenseAmount_" + i, null);
                        CategoryDetailArray[i] = pref.getString("Category_" + i, null);
                        DateofPurchaseArray[i] = pref.getString("DOP_" + i, null);
                    }

                    ExplanationDetailArray[placeCounterExplanation] = explanationItem;
                    IndividualExpenseDetailArray[placeCounterExplanation] = individualamountitem;
                    CategoryDetailArray[placeCounterExplanation] = categoriesItem;
                    DateofPurchaseArray[placeCounterExplanation] = currentDateItem;

                    Intent intent = getIntent();
                    // getting attached intent data
                    String product = intent.getStringExtra("product");
                    // displaying selected product name
                     txtProduct.setText(product);



                    String[] ResultsArray = new String[placeCounterExplanation];




                    if (product.equals("Alcohol")){
                        //test3.setText(product);

                        if(CategoryDetailArray[0].equals("Alcohol")){
                            ResultsArray[resultCounter] = (IndividualExpenseDetailArray[resultCounter] + "" + ExplanationDetailArray[resultCounter] + "" + DateofPurchaseArray[resultCounter] );
                        }

                        if(CategoryDetailArray[1].equals("Alcohol")){
                            ResultsArray[resultCounter + 1] = (IndividualExpenseDetailArray[resultCounter + 1] + "" + ExplanationDetailArray[resultCounter + 1] + "" + DateofPurchaseArray[resultCounter + 1] );
                        }

                        //  resultCounter = resultCounter + 1;
                    //}
                        test.setText(ResultsArray[0]);
                         test2.setText(ResultsArray[1]);

                        //editor.putInt("resultCounter",resultCounter);
                        // editor.commit();

                    //arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ResultsArray);
                //  resultsListView.setAdapter(arrayAdapter);




            //building result array
                  /*    for(int i=0; i< placeCounterExplanation;i++){

                            if(CategoryDetailArray[i].equals("Alcohol")){

                                int resultCounter = pref.getInt("resultCounter",0);

                                    ResultsArray[resultCounter] = (IndividualExpenseDetailArray[resultCounter] + "" + ExplanationDetailArray[resultCounter] + "" + DateofPurchaseArray[resultCounter] );
                                    resultCounter = resultCounter + 1;

                                     editor.putInt("resultCounter",resultCounter);
                                     editor.commit();
                            }

                        }   
                    }

*/ }

                    //String placeCount = Integer.toString(placeCounterExplanation);
                   // test.setText(ExplanationDetailArray[0]);
                     //test2.setText(ExplanationDetailArray[1]);
                        //test3.setText(placeCount);


                        //test.setText(ResultsArray[0]);
                        // test2.setText(IndividualExpenseDetailArray[0]);
                        // test3.setText(CategoryDetailArray[0]);
                        // test4.setText(DateofPurchaseArray[0]);





                    //STORE

                    editor.putInt("Status_size", ExplanationDetailArray.length); /* sKey is an array */ 

                    for(int i=0;i<ExplanationDetailArray.length;i++)  
                    {

                        editor.remove("Explanation_" + i);
                        editor.remove("ExpenseAmount_" + i);
                        editor.remove("Category_" + i);
                        editor.remove("DOP_" + i);
                        editor.putString("Explanation_" + i, ExplanationDetailArray[i]);  
                        editor.putString("ExpenseAmount_" + i, IndividualExpenseDetailArray[i]);  
                        editor.putString("Category_" + i, CategoryDetailArray[i]);  
                        editor.putString("DOP_" + i, DateofPurchaseArray[i]);  



                    }

                     editor.commit();  






             }

             placeCounterExplanation = placeCounterExplanation + 1;
             editor.putInt("placeCounterExplanation",placeCounterExplanation);
             editor.commit();
user3924167
  • 61
  • 1
  • 7
  • That's a whole lotta code. Could you boil it down at all? – ne1410s Sep 03 '14 at 19:52
  • the length of array 30 but you fill 1 of them.use arrayList instead that. ArrayList x = new ArrayList(); – MHP Sep 03 '14 at 19:54
  • so the issue is somewhere in the if (placeCounterExplanation > 0), its not adding the new element like i am trying to do (WhicheverArray[placeCounterExplanation] = explanationItem – user3924167 Sep 03 '14 at 19:56
  • MHP I am rather new and dont have any expereince using arrayList. Can you explain more and help me – user3924167 Sep 03 '14 at 19:57
  • @user3924167 http://developer.android.com/reference/java/util/ArrayList.html – codeMagic Sep 03 '14 at 20:00
  • So this is crashing because there is no match for when it is trying to putString(calling from sharedprefrences). I tried changing the array from default 30 to only the length needed and this still didnt help – user3924167 Sep 03 '14 at 20:04
  • Yes at the very bottom of the activity it does placeCounterExplanation + 1 and then stores that back in shared pref – user3924167 Sep 03 '14 at 20:05
  • And i know it works because after going through adn trying to input a second time (add to the resultArray), it still shows the first element as being correct so its not that the second element is replacing the first. Its just that for some reason in my if(placeCounterExplanation > 0) it is not adding to the array. Are my parameters for my for statements inside that not permitting the extra spot for some reason? – user3924167 Sep 03 '14 at 20:06
  • Because Yeah the 3 individual element arrays are defaulted at 30 but the resultArray is just the length needed – user3924167 Sep 03 '14 at 20:24

1 Answers1

0

You can use ArrayList so that there is no limit of adding items.

ArrayList<String> yourlistname = new ArrayList<String>();

just change the ArrayList<String> to ArrayList<Integer> if you want to store int values. even you can use class name instead of String or int

for adding values

yourlistname.add("item1");

for getting value

String value = yourlistname.get(index).toString();

for getting size of ArrayList

int length = yourlistname.size();
Jamil
  • 5,457
  • 4
  • 26
  • 29
  • How do I store it in shared pref – user3924167 Sep 03 '14 at 20:39
  • http://stackoverflow.com/questions/21794419/how-to-save-the-arraylist-data-in-sharedpreferences – Jamil Sep 03 '14 at 20:46
  • I will try implementing this now. Based on what I already have this will require taking quite a few steps back though. I appreciate the help and will try it now but based on what I already have, is there any issue you see that would be causing the Array indexOutofBoundsException? – user3924167 Sep 03 '14 at 21:48
  • I was able to find the problem in my approach. Thank you though. I checked the check mark. I dont have 15 reputation yet so cant upvote. – user3924167 Sep 03 '14 at 23:44