samedi 28 mars 2015

Android app with with "favorites" added to a randomized list



I'm having a hard time correctly implementing a 'favorites' feature for my app. Moving through a list of objects, the user should be able to mark/unmark something as a favorite. Once the activity moves into the onPause(); state, it should save the list of favorites (rather, the complete list of boolean markers that signal whether something is a favorite or not... true for favorite, false for not a favorite.) Obviously, upon moving into the onResume(); state, the list should be loaded so they can view the favorites that they've previously marked.


My issue, I think, truly comes from the fact that the list is randomized upon initialization. I'm sure my algorithm is off, but I've tried various ways to the point where I can hardly bare to look at it anymore.


Main Activity Java



public class MainActivity extends ActionBarActivity {


Global global_main;


@Override
protected void onCreate(Bundle savedInstanceState) {

global_main = Global.getInstance("all");

}



@Override
protected void onResume(){
super.onResume();


SharedPreferences settings = getSharedPreferences(FILE_FAVORITES, 0);

for(int index = 0; index < TOTAL_QUESTIONS; index++){

boolean favFromFile = settings.getBoolean(("savedFavorite_" + String.valueOf(index)), false);
global_main.setFav(index, favFromFile);

}

}



@Override
protected void onPause(){
super.onPause();

SharedPreferences settings = getSharedPreferences(FILE_FAVORITES, 0);
SharedPreferences.Editor editor = settings.edit();

for(int index = 0; index < TOTAL_QUESTIONS; index++){
editor.putBoolean(("savedFavorite_" + String.valueOf(index)), global_main.getFav(index));

// Commit the edits!
editor.commit();
}

}


Practice Java



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
selectedSection = intent.getStringExtra(chooseSection.chosenSection);

global = Global.getInstance(selectedSection);

}


Global Class



public class Global {


private static Global global = null;

//Current total of questions which the user has chosen
//EX: if Multiplication was chosen and has 10 questions in the array
//Then this equals 10
int CURRENT_TOTAL;

//This is the position that the first question of the user's choice starts with
//EX: If user chooses Multiplication, and the multiplication questions start at questions[19];
//Then this equals 19
int CURRENT_START;

//This is the position that the last question of the user's choice ends with
//EX: If user chooses Multiplication, and the multiplication questions end at questions[24];
//Then this equals 24
int CURRENT_END;


//Basic question structure
class questionStruct
{

String q;
String a;
int position; //original position in the array;
boolean favorite;

}

//Array of question structures
questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS];


//userChoice is the choice of question type that the user has selected.
//EX: Multiplication, Division, Addition, Subtraction, or All/Default
public static Global getInstance(String userChoice) {
if(global == null)
{
global = new Global();
global.initialize();
}

global.getChoice(userChoice);
global.setQuestionsDefault();
global.randomize();
return global;

}


public void initialize() {
for (int i = 0; i < TOTAL_QUESTIONS; i++) {
questions[i] = new questionStruct();
}

questions[0].q = "Question 1 Text";
questions[0].a = "Answer";
questions[0].position = 0;
questions[1].q = "Question 2 Text";
questions[1].a = "Answer";
questions[1].position = 1;
questions[2].q = "Question 3 Text";
questions[2].a = "Answer";
questions[2].position = 2;
....ETC.
....ETC.
....ETC.
}


public void setQuestionsDefault(){

questionStruct temp = new questionStruct();

for(int index = 0; index < TOTAL_QUESTIONS; index++){

int count = questions[index].position;

temp = questions[count];
questions[count] = questions[index];
questions[index] = temp;

temp = null;
}
}


//Randomize the questions only within the range of the category
//which the user has chosen
public void randomize(){


for(int index = CURRENT_END; index >= CURRENT_START; index --)
{
//Generate random number to switch with random block
Random rand = new Random();
int currentQ = rand.nextInt((CURRENT_END - CURRENT_START) + 1) + CURRENT_START;


//Switch two Question blocks
questionStruct temp = questions[currentQ];
questions[currentQ] = questions[index];
questions[index] = temp;


}
}



public void setFav(int q, boolean b){
questions[q].favorite = b;
}


public boolean getFav(int q){
return questions[q].favorite;
}


That MIGHT be everything pertinent to my issues. I apologize if I left anything out or if something doesn't make sense. Feel free to ask questions. I'm still currently in the middle of altering everything to get it to work, so I might have copied over something that doesn't quite add up.




Aucun commentaire:

Enregistrer un commentaire