0

I´m beginner programmer in android, and i need to change my gallery control, and read about Devsmart horizontallistview control, but i can´t run it.

I use an array of integers from web service to fill dynamically my old gallery control. How i can fill the horizontalllistview?

...at this point:

private void llenagallery() {

        int i,Id;
        for ( i = 0; i<(Servicios.length()); i++) {

            String valor = Character.toString(Servicios.charAt(i));

            if (valor.equals("a")) {
                Id = this.getResources().getIdentifier("a", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("b")) {
                Id = this.getResources().getIdentifier("b", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("c")) {
                Id = this.getResources().getIdentifier("c", "drawable", this.getPackageName());
                imagenes.add(Id);   
            }else if (valor.equals("d")) {
                Id = this.getResources().getIdentifier("d", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("e")) {
                Id = this.getResources().getIdentifier("e", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("f")) {
                Id = this.getResources().getIdentifier("f", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("g")) {
                Id = this.getResources().getIdentifier("g", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("i")) {
                Id = this.getResources().getIdentifier("i", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("j")) {
                Id = this.getResources().getIdentifier("j", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("l")) {
                Id = this.getResources().getIdentifier("l", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("m")) {
                Id = this.getResources().getIdentifier("m", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("n")) {
                Id = this.getResources().getIdentifier("n", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("o")) {
                Id = this.getResources().getIdentifier("o", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("p")) {
                Id = this.getResources().getIdentifier("p", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("r")) {
                Id = this.getResources().getIdentifier("r", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("s")) {
                Id = this.getResources().getIdentifier("s", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("t")) {
                Id = this.getResources().getIdentifier("t", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("u")) {
                Id = this.getResources().getIdentifier("u", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("v")) {
                Id = this.getResources().getIdentifier("v", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("w")) {
                Id = this.getResources().getIdentifier("w", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("y")) {
                Id = this.getResources().getIdentifier("y", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("z")) {
                Id = this.getResources().getIdentifier("z", "drawable", this.getPackageName());
                imagenes.add(Id);   
            }else if (valor.equals("P")) {
                Id = this.getResources().getIdentifier("euro1", "drawable", this.getPackageName());
                imagenes.add(Id);
            }else if (valor.equals("h")) {
                Id = this.getResources().getIdentifier("euro2", "drawable", this.getPackageName());
                imagenes.add(Id);   
            }
        }                                 

        HorizontalListView hscroll = (HorizontalListView)findViewById(R.id.iconos);
    hscroll.setAdapter(new ImageAdapter(this,imagenes));

but when debug program, nothing is displayed (of dinamic icons) Thank´s and sorry for my english.

Edit: This is my ImageAdapter:

public class ImageAdapter extends BaseAdapter {

List<Integer> imageIDs;
private Context context;

public ImageAdapter(Context c, List<Integer> imagenes) 
{
    context = c;
    imageIDs = imagenes;
}

//---returns the number of images---
public int getCount() {
    return imageIDs.size();
}

//---returns the ID of an item--- 
public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) 
{
        ImageView imageView = new ImageView(this.context);
        imageView.setImageResource(imageIDs.get(position));
        imageView.setLayoutParams(new HorizontalListView.LayoutParams(120,120));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        //imageView.setBackgroundResource(mItemBackground);
        return imageView;
}    

}

Thank´s for help.

Javier dc
  • 565
  • 6
  • 14
  • This is the perfect example where you should use "switch" instruction to make better quality code ... – Marek Jun 24 '13 at 23:49
  • yes, you're right. More work! thank´s. – Javier dc Jun 25 '13 at 00:16
  • Mmm switch is not posibble whit Strings, and examples only work whith enums, and i don´t have this. Values is character of one string. (ex 'abcdefgh') and we extract character to character and I need to assign an icon according to the value of character and show all together in horizontallistview, because gallery control not run in some news android devices. – Javier dc Jun 25 '13 at 08:08
  • convert string to int! ;) – Marek Jun 25 '13 at 23:46
  • I try at least to see how to use it. Anyway I know that I have problem, I'm trying to use a horizontallistview within a layout with more controls, and listview can only be shown single, so I've read. So I have to use a horizontallscroll, for my application is perfect. Now I need to know how to fill a horizontalscrollview with an array of images. A study! Thanks for response. – Javier dc Jun 26 '13 at 18:22

1 Answers1

0

It's hard to know why anything is not displayed since this code just shows populating a list.

It would be easier if you replaced

Id = this.getResources().getIdentifier("euro1", "drawable", this.getPackageName());

with

Id = R.drawable.euro1;

though.

Plato
  • 2,338
  • 18
  • 21