ListView with my images

1

I have 10 images and I want to put them with ListView . If it were a activity , it would already be done because I did it in an application, but now they are Fragment .

    String[] menuItems = { "Do some","kkk",",,,,,","Do some","kkk",",,,,,","Do some","kkk",",,,,,","Do some","kkk",",,,,,","Do some","kkk",",,,,,"};


    ListView listView = (ListView) v.findViewById(R.id.mymenu);

    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String> (getActivity(),android.R.layout.simple_list_item_1,menuItems);

    listView.setAdapter(listViewAdapter);


    return v;

Instead of String[] would be to put something type Drawable or similar to let me upload the photos by R.drawable.photo1 , R.drawable.photo2 . Replace the text line, but I do not know how, or how should I do it?

    
asked by Rf Mvs 26.10.2016 в 20:15
source

2 answers

4

All the resources in your project are defined by integer values which you can see inside your R.java file.

If the values are integers, proceed to create an integer value containing the reference of your images contained in sl folder /drawable :

int[] imagenes = {R.drawable.photo1, R.drawable.photo2, R.drawable.photo3 ...};

This way you can get a Drawable of the resource and add it to an ImageView within the elements in your ListView, this is usually done in the adapter within the getView() method, taking the variable position as an index to access the corresponding image:

//Asigna el Drawable al ImageView.
imageView.setImageDrawable(imagenes[position]);

For this you need to create a customadapter. As an example you can use the commented on this article you have a complete example.

Examples:

    
answered by 15.09.2017 / 14:17
source
2

You should create an adapter to load the images because by default the ListView can only load text.

You can do this by creating a view in res / layout / with the name of listview_img.xml . To this view we only define a ImageView :

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" />

Now in your activity, you just have to create an array with the recursor and implement the adapter:

// obtenemos el listview 
ListView listView = (ListView) findViewById(R.id.listview);

// definimos las imagenes que cargara el listview
final int[] resources = {R.drawable.screenshot, R.drawable.cloud_outline_icon_8, R.drawable.screenshot, R.drawable.cloud_outline_icon_8};

// le asignamos el adapter que se encargara de cargar la vista de la imagen y asignarle el id del recurso al ImageView
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_2) {

        @Override
        public int getCount() {
            return resources.length; // le indicamos la cantidad de elementos que va a cargar
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            if(convertView == null)
            {
                // cargamos la vista 
                convertView = ((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_item, parent, false);
            }

            // como la vista es un ImageView lo convertimos y le asignamos el id del recurso
            ((ImageView)convertView).setImageDrawable( ActivityCompat.getDrawable( MainActivity.this, resources[position]));


            return convertView; // retornamos la vista
        }
});

This would be the result:

    
answered by 15.09.2017 в 15:13