5

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 ????

Akari
  • 758
  • 8
  • 20
  • 35

5 Answers5

14

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);    
      }
      });
Community
  • 1
  • 1
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
2

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);

          }
      });
Wand Maker
  • 17,377
  • 6
  • 43
  • 79
2

for similar problem for me just this helped me :

  @Override
  public void onClick(View v) {
                    Intent intent = new Intent(G.context, SecondActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    G.context.startActivity(intent);
  }
  });

add context in G Class :

public class G extends Activity {

    public static Context context;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = getApplicationContext();

    }
}
hadi
  • 247
  • 2
  • 12
1

You just need to call context.startActivity instead, can follow below steps

Three simple steps

1) Declare local Variable Context mCtx

2) intialize it with Activity context either by passing a parameter to constructor/method, or in the onCreate method if you are using above code in activity.

3) call mCtx.startActivity(intent);

or

you can call ActivityName.this.startActivity()

Edit:- As par dmon comment, You simply can use your local context instance

AAnkit
  • 26,030
  • 10
  • 55
  • 68
0

The root of your problem is that you cannot start an Activity from an application Context (only from an activity context you have the right to do that). The reason for that is that android bundles all activities of your application as a group (task) in order to allow multitasking between different apps. When you launch a new app, or press the home button for example, you are application (together with its activities backstack) is going to the background and leaves the scene to a new task (with a different activity backstack and so on and so forth). For more info on this mechanism you can take a look here (http://developer.android.com/guide/components/tasks-and-back-stack.html).

In order to gain a context that is ok to lauch a new activity from, you can always get the context of the parent of the view you are inflating, by calling viewgroup.getContext() - eventhough you will have to declare the viewgroup as final, which is ok since you shouldn't be touching the parent.

a draft of your getView:

@Override
public View getView(int position, View convertView, final 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(parent.getContext(),Update.class);
             startActivity(redirect);
      }
  });




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


    return rowView;
}