-1
I need to calculate the maximum heart rate from the age informed by the user (fcm = 220 - age). I created the fields to capture the name and age, but I can’t make the calculated fcm appear on the screen. The name information and fcm should appear in a scrollable list. I’ve already made so many changes that I’ve created an error in the app when clicking the button... Anyway, in an athlete class I took age, converted to int and calculated fcm. In Main, I don’t know how to take the calculated formula and launch the method that processes the click. Same thing on my Adapter: since fcm is not a Textview, I don’t know how to treat it.
public class MainActivity extends AppCompatActivity {
TextView txtNome, txtIdade;
Button bt;
ListView listAtletas;
List<Atleta> atletas = new ArrayList<>();
AtletaAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNome = findViewById(R.id.txtNome);
txtIdade = findViewById(R.id.txtIdade);
bt = findViewById(R.id.bt);
bt.setOnClickListener(btClickListener);
listAtletas = findViewById(R.id.listAtleta);
adapter = new AtletaAdapter(atletas, getBaseContext());
listAtletas.setAdapter(adapter);
}
public void processarClique(View v) {
Atleta a = new Atleta();
a.setNome((txtNome.getText().toString()));
??????????????????
atletas.add(a);
adapter.notifyDataSetChanged();
}
View.OnClickListener btClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
processarClique(v);
}
};
}
public class Atleta {
private String nome;
TextView txtIdade;
private int idade = Integer.parseInt(txtIdade.getText().toString());
private int fcm = 220 - idade;
public void setNome(String nome){
this.nome = nome;
}
public void setFcm(int fcm){
this.fcm = fcm;
}
public String getNome() {
return nome;
}
public int getFcm(){
return fcm;
}
}
public class AtletaAdapter extends BaseAdapter {
LayoutInflater inflater;
List<Atleta> atletas;
public AtletaAdapter(List<Atleta> atletas, Context ctx){
this.atletas = atletas;
inflater = LayoutInflater.from(ctx);
}
@Override
public int getCount() {
return atletas.size();
}
@Override
public Object getItem(int position) {
return atletas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = inflater.inflate(R.layout.atleta_item, null);
TextView txtNome = v.findViewById(R.id.txtNomeItem);
??????????????
Atleta a = atletas.get(position);
txtNome.setText(a.getNome());
??????????????????
return v;
}
}