20

I'm new to the android development and having a bit of a problem changing activities. I am trying to change activities from within a method but I am getting the error cannot resolve method startActivity and on the parameter end the error Cannot resolve constructor 'Intent (...)'. I found a question here with the same sort of problem and tried to implement their replies into my program but no joy.

Here's the code:

public void open301(View view) {
    startActivity(new Intent(CustomAdapter.this, ThreeZeroOne.class));
}

before looking at the replies from the question linked above the code looked like this with the same errors:

public void open301(View view) {
   Intent openThree = new Intent(this,ThreeZeroOne.class);
    startActivity(openThree);
}

Complete code:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;

public class CustomAdapter extends BaseAdapter {
  String[] result;
  Context context;
  int[] imageId;

  private static LayoutInflater inflater = null;

  public CustomAdapter(selectGame SelectGame, String[] prgmNameList, int[] prgmImages) {
    result = prgmNameList;
    context = SelectGame;
    this.imageId = prgmImages;
    inflater = (LayoutInflater) context.getSystemService(Context.
        LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
    return result.length;
  }

  @Override
  public Object getItem(int position) {
    return position;
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public class Holder {
    TextView tv;
    ImageView img;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.game_selection, null);
    holder.tv = (TextView) rowView.findViewById(R.id.txt);
    holder.img = (ImageView) rowView.findViewById(R.id.img);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(context, "Beginning game " + result[position], Toast.LENGTH_SHORT).show();

      }
    });
    return rowView;
  }

  public void open301(View view) {
    Intent openThree = new Intent(this,ThreeZeroOne.class);
    startActivity(openThree);
  }
}
Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
COYG
  • 1,538
  • 1
  • 16
  • 31

4 Answers4

62

You should use the context of your adapter:

public void open301(View view) {
  Intent openThree = new Intent(context,ThreeZeroOne.class);
  context.startActivity(openThree);
}
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
17

To start a new activity you will need a context to start from, and your current activity "BaseAdapter" is not a Context, luckly every view has a Context, so you can do like this:

public void open301(View view) {
    Intent openThree = new Intent(view.getContext(), ThreeZeroOne.class);
    view.getContext().startActivity(openThree);
}
Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
  • That worked somewhat, would I not need to put the intent name in the parenthesis of startActivity? otherwise how would it know what activity to start. - this was in relation to your old comment. – COYG Oct 16 '15 at 14:42
  • I edit my answear to make it clear, any other doubt? – Pozzo Apps Oct 16 '15 at 14:43
4

First you should get your Context:

private Context context;

public CustomAdapter(Context context) {
    this.context = context;
}

And then:

context.startActivity(openThree);
Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
Patricia
  • 2,885
  • 2
  • 26
  • 32
0

you can also pass messages

 Intent i = new Intent( getContext(),Chat.class);
            i.putExtra("id",user.id);
            i.putExtra("name",user.name);
            getContext().startActivity(i);