Fill Spinner with my Sqlite list

1

I have a Spinner with three positions 0, 1, 2 and what I want to do is create more positions with a list that I have in my Sqlite :

  final List lista = mDatabase.ReadCategorias();

Activity

   int flags[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3};

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if (position == 0) {
            tv_categoria.setText("diaria");
            tv_imagen.setText("image_0001");
        } else if (position == 1) {
            tv_categoria.setText("semanal");
            tv_imagen.setText("image_0002");
        } else if (position == 2) {
            tv_categoria.setText("mensual");
            tv_imagen.setText("image_0003");
        }
        spin.setSelection(position);
    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    // fin spinner

    SqliteDatabase mDatabase;
    TextView tv_categoria, tv_imagen;
    String[] categoria;
    Spinner spin;

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

        tv_categoria = new TextView(CrearNota.this);
        tv_imagen = new TextView(CrearNota.this);
        mDatabase = new SqliteDatabase(this);
        spin = (Spinner) findViewById(R.id.spinnercategoria);

      // spinner

        /// aqui cargo de un array los títulos de los tres primeros

        categoria = getResources().getStringArray(R.array.titulos_categorias);
        CustomAdapter customAdapter = new CustomAdapter(ContextyMetodos.getAppContext(), flags, categoria);
        spin.setOnItemSelectedListener(this);
        spin.setAdapter(customAdapter);

        // fin spinner

Adapter

public class CustomAdapter extends BaseAdapter {
    Context context;
    int flags[];
    String[] titulo_categorias;
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, int[] flags, String[] titulo_categorias) {
        this.context = applicationContext;
        this.flags = flags;
        this.titulo_categorias = titulo_categorias;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return flags.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.custom_spinner_items, null);
        ImageView icon = (ImageView) view.findViewById(R.id.imagenspinner);
        TextView names = (TextView) view.findViewById(R.id.textspinner);
        icon.setImageResource(flags[i]);
        names.setText(titulo_categorias[i]);
        return view;
    }
}

How can I do it? Thanks !!!

    
asked by UserNameYo 26.06.2017 в 18:22
source

2 answers

1

I leave you as you could do, there is the commented code:

    //obtienes el primer array
    String[] categoria = getResources().getStringArray(R.array.titulos_categorias);
    //obtienes la lista de tu DB
    List lista = mDatabase.ReadCategorias();
    //conviertes la lista en un array
    String[] listaEnArry = (String[])lista.toArray(new String[lista.size()]);
    //creas un nuevo array con el tamaño de los dos anteriores
    String[] arrayFinal = new String[listaEnArry.length + categoria.length];
    //juntas ambos array usando System.arraycopy que es mas eficiente
    System.arraycopy(categoria, 0, arrayFinal, 0, categoria.length);
    System.arraycopy(listaEnArry, 0, arrayFinal, categoria.length, listaEnArry.length);

    //a tu adapter le pasas el nuevo array
    CustomAdapter customAdapter = new CustomAdapter(ContextyMetodos.getAppContext(), flags, arrayFinal);
    spin.setOnItemSelectedListener(this);
    spin.setAdapter(customAdapter);

To put a default icon in the created categories, you have two options, one is to add more elements to the array, and another one in the adapter, I give the example of the adapter:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.custom_spinner_items, null);
    ImageView icon = (ImageView) view.findViewById(R.id.imagenspinner);
    TextView names = (TextView) view.findViewById(R.id.textspinner);
    if(flags.length-1 < i)
    {
       icon.setImageResource(flags[0]); //ponemos la primera imagen del array
    }else{
       icon.setImageResource(flags[i]);
    }
    names.setText(titulo_categorias[i]);
    return view;
}

In the example, when placing an image, check how many images the array has, if the position of the note to be placed is greater than the number of images, it will place the image of position 0, that is, the first category. I explain myself better: Categories - 5 Images - 3 Place the images to the notes:

category 1 - image 1

category 2 - image 2

category 3 - image 3

category 4 - image 1 (no more images, we put the first)

category 5 - image 1 (no more images, we put the first)

Being able to play with the image you want to show. If you want to add a unique image for those created categories, it would be enough with your Activity , add one more element to the array:

int flags[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.IMAGENNUEVA};

Having that added image, it would be enough to modify the previous code:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.custom_spinner_items, null);
    ImageView icon = (ImageView) view.findViewById(R.id.imagenspinner);
    TextView names = (TextView) view.findViewById(R.id.textspinner);
    if(flags.length-1 < i)
    {
       icon.setImageResource(flags[3]); // 3 es la posicion de la ultima imagen, es decir, la agregada 
    }else{
       icon.setImageResource(flags[i]);
    }
    names.setText(titulo_categorias[i]);
    return view;
}

Another option would be that adding a category could also add an image, it would be enough to save the path of that image in a db table next to the category, and pass the array to the adapter to assign them, that already It's your preference.

    
answered by 26.06.2017 / 19:08
source
0

Practically you already have it. To your array categoria just add the new options you want.

Another thing: android handles the nomenclature camelcase so that you have it in consideration when writing your code.

    
answered by 26.06.2017 в 19:06