Reputation: 199
I am trying to build an ArrayAdapter that allows a user to access individual TextView
fields which link to the internet, googlemaps, etc. I am unable to resolve an issue in which the error states
Cannot resolve method 'startActivity (android.content.Intent).
After looking through quite a few posts on here, I've established that it's an issue with context
, I'm not sure how I get this from the code below. Any help would be greatly appreciated :)
package com.example.stuart.listitemviewtest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AttractionAdapter extends ArrayAdapter {
public AttractionAdapter(Context context, ArrayList<Attraction> attractions) {
super(context, 0, attractions);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link currentAttraction} object located at this position in the list
final Attraction currentAttraction = (Attraction) getItem(position);
final TextView attractionName = (TextView) listItemView.findViewById(R.id.attraction_name);
attractionName.setText(currentAttraction.getmName());
attractionName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("attraction Name", "the name of attraction: " + currentAttraction.getmName());
}
});
TextView attractionURL =(TextView) listItemView.findViewById(R.id.attraction_URL);
attractionURL.setText(currentAttraction.getmWebAddress());
attractionURL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//get web address
Log.i("Web Address", "The web address is: " + currentAttraction.getmWebAddress());
//load URL in new window
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(currentAttraction.getmWebAddress()));
startActivity(intent);
}
});
return listItemView;
}
}
Upvotes: 0
Views: 49
Reputation: 4132
You need an context to start a new activity,i.e activity context
//load URL in new window
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(currentAttraction.getmWebAddress()));
(listItemView.getContext()).startActivity(intent);
Upvotes: 0
Reputation: 1621
Change your constructor like this:-
Context mContext;
public AttractionAdapter(Context context, ArrayList<Attraction> attractions) {
super(context, 0, attractions);
mContext = context;
}
now use mContext before startActivity();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(currentAttraction.getmWebAddress()));
mContext.startActivity(intent);
Upvotes: 1