Get ImageView id in a GridView

0

Images I want for in GridView

        private Integer[] imagens= { R.drawable.um, R.drawable.dois, R.drawable.tres, R.drawable.quatro, R.drawable.cinco, R.drawable.seis,
        R.drawable.sete, R.drawable.oito, R.drawable.nove, R.drawable.dez, R.drawable.onze, R.drawable.doze, R.drawable.treze, R.drawable.catorze, R.drawable.quinze};

This is where I put the images in GridView

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imagens[position]);
        return imageView;
    }

I have this method for every time I click on a GridView item it is supposed to create an Alertdialogo with the id of the item that was clicked

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
            IdAsString = arg0.getItemAtPosition(position).toString();;
            IdAsString += "_original";

            //Toast.makeText(getApplicationContext(), "Your text", Toast.LENGTH_LONG).show();
            AlertDialog.Builder alert = new AlertDialog.Builder(fotos.this, android.R.style.Theme_Holo_Dialog);
            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
            vista= inflater.inflate(R.layout.full_image, null);
            alert.setTitle(IdAsString);
            alert.setView(vista);
            alert.show();
        }
    });

What I want is that whenever I click on an image it gives me the id of that image. Ex: "one", "two", I just want to give me the id in a string but this method

            IdAsString = arg0.getItemAtPosition(position).toString();

is giving this error

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    
asked by anonymous 23.04.2017 / 22:31

1 answer

1

Instead of

IdAsString = arg0.getItemAtPosition(position).toString();;

use:

IdAsString = "" + imagens[position];
    
24.04.2017 / 01:50