Get id Imageview on a Gridview

Asked

Viewed 29 times

0

Images I want to put on 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 on 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 forever that I click on a Gridview item it is supposed to create a Alertdialog 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 an image give me the id of that image. Ex:"one","two", I just want you to give me the id in a string but this method

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

is making this mistake

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

1 answer

1


In place of

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

use:

IdAsString = "" + imagens[position];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.