1

estoy intentando llenar una lista en Android de forma dinamica con un FOR

El ListView lo se llenar de forma manual asi:

 Lista_productos_categoria lista_productos_categoria_data[] = new Lista_productos_categoria[]{

            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto1", "$50.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto2", "$40.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto3", "$34.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto4", "$44.300"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto5", "$99.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto6", "$140.400"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto7", "$90.040")
    };

    Lista_productos_categorias_Adapter adapter = new Lista_productos_categorias_Adapter(this, R.layout.item_lista_productos_categoria, lista_productos_categoria_data);

    lista_productos= (ListView) findViewById(R.id.list_productos);


    lista_productos.setAdapter(adapter);

Lo que quiero es si por ejemplo necesito 100 productos, no tener que codificar uno por uno;

habia pensado algo asi pero no me funciono:

Lista_productos_categoria lista_productos_categoria_data[] = new Lista_productos_categoria[]{

            for (int i=0;i<100;i++){
                new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto1", "$50.000");
            }


    };

de cualquier manera dejo todo el codigo del activity, gracias de antemano

public class categoria_productos extends Activity {
String categoria;
TextView cat_actual;
String name_pr, pre_pr;
ListView lista_productos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista_productos_categoria);

    cat_actual = (TextView) findViewById(R.id.categoria_actual);

    Lista_productos_categoria lista_productos_categoria_data[] = new Lista_productos_categoria[]{

            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto1", "$50.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto2", "$40.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto3", "$34.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto4", "$44.300"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto5", "$99.000"),
            new Lista_productos_categoria(R.drawable.ic_launcher_foreground, "producto6", "$140.400"),
            new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto7", "$90.040")
    };

    Lista_productos_categorias_Adapter adapter = new Lista_productos_categorias_Adapter(this, R.layout.item_lista_productos_categoria, lista_productos_categoria_data);

    lista_productos= (ListView) findViewById(R.id.list_productos);


    lista_productos.setAdapter(adapter);

    lista_productos.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            TextView producto_Sel = (TextView)view.findViewById(R.id.text_producto);
            TextView producto_Sel_pre= (TextView)view.findViewById(R.id.pre_producto);
            Toast.makeText(getApplicationContext(),"INGRESANDO AL PRODUCTO " + producto_Sel.getText(), Toast.LENGTH_LONG).show();

            //voy a intentar ingresar a solo producto
            name_pr= producto_Sel.getText().toString();
            pre_pr=producto_Sel_pre.getText().toString();
            Intent intent = new Intent(categoria_productos.this, Solo_Producto.class );
            intent.putExtra("precio", pre_pr);
            intent.putExtra("nombre", name_pr);
            startActivity(intent);

        }
    });

    Bundle extras  = getIntent().getExtras();

    if (extras != null){
        categoria = extras.getString("valor");
        cat_actual.setText(categoria);
    }
}

}

1

1 respuesta 1

1

Lo puedes hacer de esta manera:

la variable NUMERO_ELEMENTOS contiene la cantidad de elementos del listado que se generará dinámicamente:

Lista_productos_categoria lista_productos_categoria_data[] = new Lista_productos_categoria[NUMERO_ELEMENTOS];

            for (int i=0;i<NUMERO_ELEMENTOS;i++){
                lista_productos_categoria_data[i] = new Lista_productos_categoria(R.drawable.ic_launcher_background, "producto1", "$50.000");
            }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.