Reputation: 172
I've got an activity with a recyclerview, and I want that recyclerview to show different listItems every time. That works, but I want this to run only once(at a certain hour eg. 00:00h) and not to run again every time that I get into the activity all the void that randomly decides which item to show on the recyclerview.
Here's the code:
public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
private RecyclerView recyclerView1;
List<Entidad2> listItems;
Adaptador2 adaptor;
private Entidad2 entidad1,entidad2,entidad3;
Button cambiarmenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_comida);
cambiarmenu = (Button) findViewById(R.id.boton_cambiarmenu);
recyclerView1 = findViewById(R.id.lv_1);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView1.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);
listItems.add(entidad1);
listItems.add(entidad2);
listItems.add(entidad3);
adaptor = new Adaptador2(listItems, this);
recyclerView1.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}
@Override
public void OnRecipe(int priority) {
if (priority == 20) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
if (priority == 50) {
Intent in = new Intent(this, Entrecot.class);
startActivity(in);
}
if (priority == 100) {
Intent in = new Intent(this, Hamburguesa.class);
startActivity(in);
}
}
public void pickEntidad(){
final int random = new Random().nextInt(101);
int priority1 = entidad1.getPriority();
int priority2 = entidad2.getPriority();
int priority3 = entidad3.getPriority();
listItems.clear();
if(random < priority1){
listItems.add(entidad1);
}else if(random < priority2){
listItems.add(entidad2);
}else if (random <= priority3){
listItems.add(entidad3);
}
adaptor.notifyDataSetChanged();
}
}
I've looked at some other posts but the problem is that I want all that process to run one time when I first get into the app and then I'd shut down the app. After that, get in the app and still not run the void. But it should run at a certain hour, 00:00h would be ok as an example.
In addition, I'd like to add a button on a different activity which can run the void in this activity and therefore change what is randomly shown on the recyclerview.
Upvotes: 0
Views: 53
Reputation: 93678
Use WorkManager and schedule a job to run at 0:00h every night. Have it pick the items to show and write them to a file. Then when your activity runs, have it read that file and display those items.
For the button in the activity, have it delete the file. Code the RecyclerView activity to generate the file itself if there is none to begin with (showing a loading screen if necessary at the beginning).
Upvotes: 0