@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId();
//noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item); } }
Product Quote: public class Product {
private static String nombrePizza; private static String ingredientesPizza; private static int imagenPizza; private static int idThumbnail;
public Product(String nombrePizza, String ingredientesPizza, int imagenPizza){ this.nombrePizza = nombrePizza; this.ingredientesPizza = ingredientesPizza; this.imagenPizza = imagenPizza; }
public String getNombrePizza(){return nombrePizza;} public String getIngredientesPizza(){return ingredientesPizza;} public int getImagenPizza(){return imagenPizza;} public int getId(){return nombrePizza.hashCode();}
public static Product[] Pizzas={ new Product( "Proscuito", "Jamon York y queso", R.drawable.imagenprueba), new Product( "Tropical", "Jamon York, queso y piña", R.drawable.imagenprueba), new Product( "Barbacoa", "Carne picada, queso y salsa Barbacoa", R.drawable.imagenprueba), new Product( "Romana", "Jamon York, champiñones y queso", R.drawable.cubo), };
public static Product getItem(int id) { for (Product item : Pizzas) { if (item.getId() == id) { return item; } } return null; }
}
Grid Adapter Quote: public class GridAdapter extends BaseAdapter {
private final Context mContext;
public GridAdapter(Context c){ this.mContext = c; }
@Override public int getCount(){return Product.Pizzas.length;}
@Override public Product getItem(int position){return Product.Pizzas[position];}
@Override public long getItemId(int position){return 0;}
@Override public View getView(int position, View ConvertView, ViewGroup viewGroup){
Buenas!! Necesito ayuda con un grid view que estoy creando con el siguiente codigo:
MainActivity
Quote:
public class MainActivity extends AppCompatActivity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new GridAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Product
Quote:
public class Product {
private static String nombrePizza;
private static String ingredientesPizza;
private static int imagenPizza;
private static int idThumbnail;
public Product(String nombrePizza, String ingredientesPizza, int imagenPizza){
this.nombrePizza = nombrePizza;
this.ingredientesPizza = ingredientesPizza;
this.imagenPizza = imagenPizza;
}
public String getNombrePizza(){return nombrePizza;}
public String getIngredientesPizza(){return ingredientesPizza;}
public int getImagenPizza(){return imagenPizza;}
public int getId(){return nombrePizza.hashCode();}
public static Product[] Pizzas={
new Product(
"Proscuito",
"Jamon York y queso",
R.drawable.imagenprueba),
new Product(
"Tropical",
"Jamon York, queso y piña",
R.drawable.imagenprueba),
new Product(
"Barbacoa",
"Carne picada, queso y salsa Barbacoa",
R.drawable.imagenprueba),
new Product(
"Romana",
"Jamon York, champiñones y queso",
R.drawable.cubo),
};
public static Product getItem(int id) {
for (Product item : Pizzas) {
if (item.getId() == id) {
return item;
}
}
return null;
}
}
Grid Adapter
Quote:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
public GridAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount(){return Product.Pizzas.length;}
@Override
public Product getItem(int position){return Product.Pizzas[position];}
@Override
public long getItemId(int position){return 0;}
@Override
public View getView(int position, View ConvertView, ViewGroup viewGroup){
if(ConvertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_ SERVICE);
ConvertView = inflater.inflate(R.layout.grid_item, viewGroup, false);
}
TextView name = (TextView)ConvertView.findViewById(R.id.grid_mainT ext);
ImageView image = (ImageView) ConvertView.findViewById(R.id.grid_image);
TextView descripcion = (TextView) ConvertView.findViewById(R.id.grid_subText);
final Product item = getItem(position);
image.setImageResource(item.getImagenPizza());
name.setText(item.getNombrePizza());
descripcion.setText(item.getIngredientesPizza());
return ConvertView;
}
}
Funciona bien excepto que me muestra solamente el ultimo producto del codigo de java repetido cuatro veces, en este caso la pizza romana.
Alguien sabe porque me pasa esto?
Muchas gracias!!