Skip to content

Instantly share code, notes, and snippets.

@jtryan
Last active August 10, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtryan/73c0f8ec3e676544efc3f4e9fd012a74 to your computer and use it in GitHub Desktop.
Save jtryan/73c0f8ec3e676544efc3f4e9fd012a74 to your computer and use it in GitHub Desktop.
Android Set click listener in adapter
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context mContext = getContext();
        final ViewHolder holder;
        final Earthquake currentEarthquake = (Earthquake) getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.earthquake_list_item, parent, false);
            holder = new ViewHolder();
            holder.listItemView = convertView;
            holder.magnitudeView = (TextView) convertView.findViewById(R.id.magnitude);
            holder.locationOffsetView = (TextView) convertView.findViewById(R.id.location_offset);
            holder.primaryLocation = (TextView) convertView.findViewById(R.id.primary_location);
            holder.dateView = (TextView) convertView.findViewById(R.id.date);
            convertView.setTag(holder);
            holder.listItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent listItemIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(currentEarthquake.getUrl()));
                    if (listItemIntent.resolveActivity(mContext.getPackageManager()) != null) {
                        mContext.startActivity(listItemIntent);
                    }
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment