Curso App para Pizzería con delivery
Detalle del Producto
Detalle del Producto
Detalle del Producto
package codea.app.pizzeria9;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import codea.app.pizzeria9.database.DBManager;
import codea.app.pizzeria9.modelo.Precio;
import codea.app.pizzeria9.utils.Config;
import codea.app.pizzeria9.utils.RetrofitClient;
import codea.app.pizzeria9.utils.SessionManager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class DetalleActivity extends AppCompatActivity {
private String nombre,descripcion,foto, tamano;
private double precio;
private int producto_id;
private SessionManager _sessionManager;
private ImageView _foto;
private TextView _nombre, _precio,_descripcion;
private Button _btnAddItem;
private DBManager dbManager = null;
RadioGroup.LayoutParams layoutParams;
ArrayList<Precio> precioArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle);
setTitle(getIntent().getExtras().getString("nombre"));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
dbManager = new DBManager(this);
_sessionManager = new SessionManager(getApplicationContext());
producto_id = getIntent().getExtras().getInt("producto_id");
nombre = getIntent().getExtras().getString("nombre");
precio = getIntent().getExtras().getDouble("precio");
descripcion = getIntent().getExtras().getString("descripcion");
foto = getIntent().getExtras().getString("foto");
tamano = "";
_foto = findViewById(R.id.pfoto);
_nombre = findViewById(R.id.pnombre);
_descripcion = findViewById(R.id.pdescripcion);
_precio = findViewById(R.id.pprecio);
_btnAddItem = findViewById(R.id.btnAddItem);
_nombre.setText(nombre);
_precio.setText("USD "+precio);
_descripcion.setText(descripcion);
_btnAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbManager.open();
Cursor cursor = dbManager.checkItem(" producto_id="+producto_id);
if (cursor.moveToFirst()){
Config.message(getApplicationContext(),nombre+" ya fue agregado!!");
}else{
dbManager.addItem(nombre,precio,1,tamano,precio,foto,producto_id);
Config.message(getApplicationContext(),nombre+" agregado correctamente!!");
}
}
});
Picasso.get().load(Config.BASE_URL_IMG+foto).into(_foto);
// RADIOGROUP
RadioGroup radioGroup =findViewById(R.id.radio_group);
Call<ArrayList<Precio>> call = RetrofitClient.getApiService().getPrecios(
"Bearer "+_sessionManager.getAccessToken(),
producto_id);
call.enqueue(new Callback<ArrayList<Precio>>() {
@Override
public void onResponse(Call<ArrayList<Precio>> call, Response<ArrayList<Precio>> response) {
//Log.d("precio:",response.body().toString());
if(response.isSuccessful()){
precioArrayList = response.body();
for(Precio precio: precioArrayList){
RadioButton radioButton = new RadioButton(getApplicationContext());
radioButton.setText(precio.getNombre());
radioButton.setTag(precio.getPrecio());
radioButton.setId(View.generateViewId());
layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
radioGroup.addView(radioButton,layoutParams);
}
}
}
@Override
public void onFailure(Call<ArrayList<Precio>> call, Throwable t) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb = findViewById(i);
_precio.setText("USD "+rb.getTag().toString());
precio = Double.valueOf(rb.getTag().toString());
tamano = rb.getText().toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
case R.id.action_cart:
startActivity(new Intent(getApplicationContext(),CarritoActivity.class));
return true;
case R.id.action_user:
_sessionManager.destroySession();
return true;
}
return true;
}
}
629 visitas
« Capítulo 38 – Productos
Capítulo 40 – Creación de la DB dbpizza »
Descarga el código fuente del proyecto adquiriendo el curso completo
Comprar© Copyright Codea::App Cursos de Programación Online | LATAM | 2020 - 2024