How to fill a personalized ListView dimanicamente in android?

1

I am trying to fill a list in Android dynamically with a FOR

The ListView is filled in manually like this:

 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);

What I want is if, for example, I need 100 products, I do not have to code one by one;

I had thought something like that but it did not work:

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");
            }


    };

in any way I leave all the code of the activity, thanks in advance

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);
    }
}

}

    
asked by fredy 21.03.2018 в 20:52
source

1 answer

0

You can do it this way:

  

the variable NUMBER_ELEMENTS contains the number of elements in the list that will be generated   dynamically:

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");
            }
    
answered by 22.03.2018 / 02:44
source