Ander Enzunza Ander Enzunza - 1 month ago 17
Android Question

Multiple click in list row

I have a

ListView
in Android. Each listitem has 2 textviews. If I click one
TextView
I want to open an
Activity
and if I click another one I want to do another thing.

My adapter´s code is lil this:

public class LazyAdapterHelpCentersAlava extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
HashMap<String, String> song;
Typeface tf;

Context context;

public LazyAdapterHelpCentersAlava(Activity a, ArrayList<HashMap<String, String>> d, String font) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

tf = Typeface.createFromAsset(activity.getAssets(), font);
}

public int getCount() {
return data.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row_help_centers, null);

//TextView id = (TextView)vi.findViewById(R.id.id); // title
TextView title = (TextView)vi.findViewById(R.id.nombre); // title
TextView localizacion = (TextView)vi.findViewById(R.id.localizacion);
TextView email = (TextView)vi.findViewById(R.id.email);
TextView web = (TextView)vi.findViewById(R.id.web);

song = new HashMap<String, String>();
song = data.get(position);

// Setting all values in listview
title.setText(song.get(Help.TAG_NOMBRE));
web.setText(song.get(Help.TAG_TELEFONO));
localizacion.setText(song.get(Help.TAG_DIRECCION));
email.setText(song.get(Help.TAG_EMAIL));

title.setTypeface(tf);
web.setTypeface(tf);
localizacion.setTypeface(tf);
email.setTypeface(tf);

web = (TextView)vi.findViewById(R.id.web);
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Need to start an Activity here
}
});

return vi;
}
}


I have put click listener on
web
but, I don´t know how to open an Activity, because it says error like this The method
startActivity(Intent)
is undefined for the type new
View.OnClickListener(){}


Can somebody help me? Thank you so much.

Answer

Use context of adapter

        public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null){
            context = parent.getContext();
         //other code
        }

then

        web.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
               Intent redirect = new Intent(context,New.class);
               context.startActivity(redirect);  
                }
           });