Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix The method startActivity(Intent) is undefined for the type new View.OnClickListener() syntax error

I have a syntax errors with my code , in the "getView" I want to make a listener for the button "update" to move to another activity :

enter image description here

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


        LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View rowView = l.inflate(R.layout.temp, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
        Button update = (Button) rowView.findViewById(R.id.button1);

//      update.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//
//                 Intent redirect = new Intent(getApplicationContext(),Update.class);
//                 startActivity(redirect);
//              
//          }
//      });




        textView.setText(sa.get(position));


        return rowView;
    }

I've tried to fix these errors about "Intent" but I failed :(

  1. The method startActivity(Intent) is undefined for the type new View.OnClickListener()

  2. The method getApplicationContext() is undefined for the type new View.OnClickListener()

and even whene I moved these statements from "onClick" method the problem didn't change !! I imported "Intent" library , how to solve that ????

like image 859
Akari Avatar asked Jul 06 '13 18:07

Akari


2 Answers

If your adapter is in a different file you will need activity context.

You need to pass the activity context from your activity class to your adapter constructor.

http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

startActivity is a method of your activity class. So you need activity context to start the activity.

Also instead of getApplicationContext use activity context.

When to call activity context OR application context?

Check the answer by commonsware.

     update.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

            Intent redirect = new Intent(context,Update.class);
            context.startActivity(redirect);    
      }
      });
like image 173
Raghunandan Avatar answered Sep 26 '22 01:09

Raghunandan


Try

final Activity thisActivity = this;

update.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {

                 Intent redirect = new Intent(thisActivity,Update.class);
                 thisActivity.startActivity(redirect);

          }
      });
like image 39
Wand Maker Avatar answered Sep 26 '22 01:09

Wand Maker