1 回答
TA贡献1804条经验 获得超7个赞
我的理解是,您想以不同的显示机会随机显示 Entidad 列表中的单个 Entidad 对象。
例如:在您的 Entidad 对象上创建一个名为 Priority 的整数字段。将第一个 Entidad 的优先级设置为 50,第二个 200,第三个 350。创建以下方法以根据优先级随机选择。(在创建它们后将 Entidad 对象的引用保存在主活动中。)
编辑:Entidad.java
public class Entidad {
private int imgFoto;
private String titulo;
private String tiempo;
private int ratingBar;
private int colorfondo;
private int priority;
public Entidad(int imgFoto, String titulo, String tiempo, int ratingBar, int colorfondo, int priority) {
this.imgFoto = imgFoto;
this.titulo = titulo;
this.tiempo = tiempo;
this.ratingBar = ratingBar;
this.colorfondo = colorfondo;
this.priority = priority;
}
public int getRatingBar() {
return ratingBar;
}
public int getPriority() {
return priority;
}
public int getImgFoto() {
return imgFoto;
}
public String getTitulo() {
return titulo;
}
public String getTiempo() {
return tiempo;
}
public int getRating() { return ratingBar; }
public int getColorfondo() {
return colorfondo;
}
}
编辑 2:使用 getEntidad()
public class Comida extends AppCompatActivity implements Adaptador.OnRecipeListener {
private RecyclerView recyclerView1;
List<Entidad> listItems;
Adaptador adaptor;
private Entidad entidad1,entidad2,entidad3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comida);
recyclerView1 = findViewById(R.id.lv_1);
LinearLayoutManager layoutManager = new
LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView1.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
//Initialize entidad objects and keep references of them to use in pickEntidad() method
entidad1 = new Entidad(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, R.drawable.color_carnes,50);
entidad2 = new Entidad(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, R.drawable.color_carnes,200);
entidad3 = new Entidad(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, R.drawable.color_carnes,350);
listItems.add(entidad1);
listItems.add(entidad2);
listItems.add(entidad3);
adaptor = new Adaptador(listItems, this);
recyclerView1.setAdapter(adaptador);
adaptor.notifyDataSetChanged();
//Call pickEntidad() to randomly pick one entidad and show on the list
pickEntidad();
}
@Override
public void OnRecipe(int position) {
if (position == 0) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
}
private void pickEntidad(){
//Generates random int value between 0-350
final int random = new Random().nextInt(351);
//get priorities from your created Entidad objects.
int priority1 = entidad1.getPriority();
int priority2 = entidad2.getPriority();
int priority3 = entidad3.getPriority();
listItems.clear()
//Pick one randomly depending on priority
if(random < priority1){
listItems.add(entidad1)
}else if(random < priority2){
listItems.add(entidad2)
}else if (random <= priority3){
listItems.add(entidad3)
}
//update your adapter
adaptor.notifyDataSetChanged()
}
}
每次您想刷新列表时,调用此 pickEntidad() 方法。不过,这是一种硬编码方法,如果您有太多项目并且不知道优先级值或范围,它将毫无用处。您还可以通过生成 0-351 之间的整数来随机设置项目优先级。希望我能正确理解你的问题。
另外,不要给变量名加上大写字母。将您的“适配器”变量更改为“适配器”
添加回答
举报