user2909006
user2909006

Reputation: 253

the type getIntent() is undefined for adapter

I am getting a byteArray from another activity via Intent like this:

        if (view == null) {
        view = new ImageView(mContext);
    }
    Bundle extras = getIntent().getExtras();
      byte[] byteArray = extras.getByteArray("picture");
      Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
      view.setImageBitmap(default_b);
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;

But I get the error that getIntent() is undefined for the type GridViewAdapter (this is in my base adapter class for a gridView)

I create the intent here:

Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);

How can I fix this error?

ADDED:

Here is my full part where I create the intent:

        Log.d("AppInfoAdapter", "Data Set To Display");
    addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                            .getApplicationIcon(entry.activityInfo.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                byte[] byteArray = stream.toByteArray();
                                Log.d("AppInfoAdapter", "Scale Bitmap to Array");

                                Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

Upvotes: 1

Views: 5012

Answers (3)

Uzair Qaiser
Uzair Qaiser

Reputation: 156

Here we go:

 Intent intent = ((Activity) context).getIntent();
 int value = intent.getIntExtra("myvalue", 0);

Upvotes: 1

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

Pass your Activity context to the Adapter constructor

there you can access your intent like this

((Activity)mContext).getIntent()

Upvotes: 3

codeMagic
codeMagic

Reputation: 44571

getIntent() is used to get the Intent used to start an Activity. Since you aren't in an Activity then there is no Intent to "get" and getIntent(), as it says, is not a function of the Adapter class.

Use that code in the Activity that calls the Adapter class and pass the data needed to that class

Upvotes: 1

Related Questions