0

Sorry, this is my first question... I have an ArrayAdapter class and i want to call startActivity from an internal anonymous class, but i'm taking the error "cannot resolve method startActivity"

public View getView(final int position, @Nullable final View convertView, @NonNull ViewGroup parent) {

    //inflating the layout
    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.itemview_antliostasia, parent, false);

    //get the reference to the view objects
    final TextView myName = (TextView)row.findViewById(R.id.itemViewAntliostasiaName);
    final TextView myID = (TextView)row.findViewById(R.id.itemViewAntliostasiaID);
    final TextView myAntlies = (TextView)row.findViewById(R.id.itemViewAntliostasiaAntlies);
    ImageView myEdit = (ImageView) row.findViewById(R.id.imageAntliostasiaEdit);
    ImageView myDelete = (ImageView)row.findViewById(R.id.imageAntliostasiaDelete);

    //providing the element of an ArrayList by specifying its position
    myName.setText(dataList.get(position).getName());
    myID.setText(dataList.get(position).getId());
    myAntlies.setText(dataList.get(position).getAntlies());
    myEdit.setImageResource(R.drawable.imgedit);
    myDelete.setImageResource(R.drawable.imgdelete);

    myEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public  void onClick(View v) {
            Intent intent = new Intent(getContext(), EditAntliostasioActivity.class);
            String[] editValues = new String[2];
            editValues[0] = myName.getText().toString();
            editValues[1] = myAntlies.getText().toString();
            intent.putExtra("edit values", editValues);
            startActivity(intent);

        }
    });

I think something is going wrong with getContext()...Please help me

  • Possible duplicate of [Get context inside onClick(DialogInterface v, int buttonId)?](https://stackoverflow.com/questions/5447092/get-context-inside-onclickdialoginterface-v-int-buttonid) – MaJoR Jul 06 '18 at 05:02

3 Answers3

0

Try this

Intent intent = new Intent(view.getContext(), EditAntliostasioActivity.class);

The reason for this error was because the getContext() was returning the context of the activity instead of the anonymous class. Hence, you have to use view.getContext().

MaJoR
  • 954
  • 7
  • 20
0

startActivity() is a method of Context so you need to call it on the Context object:

getContext().startActivity(intent);

Note that if the Context was not an Activity, then you would need also to set FLAG_ACTIVITY_NEW_TASK on the Intent. But startActivity() is overridden in Activity to not need that, and you have an activity context here.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Create an instance variables of Context..

Class Vehicle
{
Context mContext;

Initialize Context...

public Vehicle(Context mContext)
{
this.mContext = mContext;
}

Whenever you are calling startActivity do like this..in your case..

mContext.startActivity(intent);
Sachin Soma
  • 3,432
  • 2
  • 10
  • 18