Android Open Source - sloop Data Record List Adapter






From Project

Back to project page sloop.

License

The source code is released under:

NON-LICENSE The Sloop data-browser source code is hereby released into the Public Domain. The original author, David Megginson, Megginson Technologies Ltd., and Acclar Open Ltd. provide no warranty:...

If you think the Android project sloop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.megginson.sloop.ui;
/*w w  w . ja  v  a2  s.  com*/
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.megginson.sloop.R;
import com.megginson.sloop.activities.MainActivity;
import com.megginson.sloop.model.DataEntry;
import com.megginson.sloop.model.DataRecord;

/**
 * Adapt a single {@link DataRecordImpl} for display as a list of entries.
 * 
 * The {@link DataRecordFragment} class uses this adapter to display the data
 * record in a {@link ListView}.
 * 
 * @author David Megginson
 */
public class DataRecordListAdapter extends BaseAdapter {
  
  /**
   * Regular expression (case-insensitive) for a link that Sloop can handle.
   */
  public final static String SLOOP_LINK_REGEX = "^https?.*\\.csv(\\?.*)?$";

  private Context mContext;

  private DataRecord mDataRecord;
  
  // Don't make this static, in case it's not threadsafe.
  private Pattern mLinkPattern = Pattern.compile(SLOOP_LINK_REGEX, Pattern.CASE_INSENSITIVE);

  public DataRecordListAdapter(Context context, DataRecord dataRecord) {
    mContext = context;
    mDataRecord = dataRecord;
    System.err.println("Got a data record with "
        + dataRecord.getEntries().size() + " entries");
  }

  @Override
  public int getCount() {
    return mDataRecord.getEntries().size();
  }

  @Override
  public Object getItem(int position) {
    return mDataRecord.getEntries().get(position);
  }

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

  @Override
  public View getView(int position, View convertView, final ViewGroup parent) {
    View layout;
    ImageView checkView;
    TextView labelView;
    TextView valueView;

    // Reuse existing components if we can
    if (convertView == null) {
      LayoutInflater inflater = LayoutInflater.from(mContext);
      layout = inflater.inflate(R.layout.component_data_entry, null);
    } else {
      layout = convertView;
    }

    // Set the appropriate text for this entry
    DataEntry entry = mDataRecord.getEntries().get(position);

    labelView = (TextView) layout.findViewById(R.id.field_name);
    labelView.setText(entry.getKey());

    final String value = entry.getValue();
    valueView = (TextView) layout.findViewById(R.id.field_value);
    valueView.setText(value);
    // replace some of the links with internal Sloop links
    stealURLClicks(valueView);

    checkView = (ImageView) layout.findViewById(R.id.image_checkbox);

    if (entry.hasFilter()) {
      checkView.setVisibility(View.VISIBLE);
    } else {
      checkView.setVisibility(View.INVISIBLE);
    }

    if (position % 2 == 1) {
      layout.setBackgroundColor(Color.rgb(0xee, 0xee, 0xee));
    } else {
      layout.setBackgroundColor(Color.TRANSPARENT);
    }

    return layout;
  }

  /**
   * Replace some URL spans in a TextView with internal links.
   * 
   * This function walks through all URLSpans in a text view, and replaces any
   * ending in ".csv" with internal-linking spans.
   * 
   * @param textView
   *            the text view to modify.
   */
  private void stealURLClicks(TextView textView) {
    Spannable text = (Spannable) textView.getText();
    for (URLSpan span : textView.getUrls()) {
      if (mLinkPattern.matcher(span.getURL()).matches()) {
        int start = text.getSpanStart(span);
        int end = text.getSpanEnd(span);
        text.removeSpan(span);
        text.setSpan(new SloopURLSpan(span.getURL()), start, end,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      }
    }
  }

  /**
   * Special class to replace URLSpan for an internal link.
   * 
   * When the Android libraries linkify a TextView, they provide no way to
   * intercept some links for internal use in the application. This class
   * provides an alternative span type that opens URLs in Sloop rather than
   * sending out a view intent.
   * 
   * @author David Megginson
   */
  private class SloopURLSpan extends ClickableSpan {
    private String mUrl;

    public SloopURLSpan(String url) {
      mUrl = url;
    }

    @Override
    public void onClick(View widget) {
      Intent intent = new Intent(widget.getContext(), MainActivity.class);
      intent.setAction(Intent.ACTION_MAIN);
      intent.putExtra(MainActivity.PARAM_URL, mUrl);
      widget.getContext().startActivity(intent);
    }
  }

}




Java Source Code List

com.megginson.sloop.activities.ActivitiesUtil.java
com.megginson.sloop.activities.AddressActionProvider.java
com.megginson.sloop.activities.BookmarkEditActivity.java
com.megginson.sloop.activities.BookmarkListActivity.java
com.megginson.sloop.activities.InfoBarFragment.java
com.megginson.sloop.activities.MainActivity.java
com.megginson.sloop.activities.MainDisplayFragment.java
com.megginson.sloop.activities.TextFilterFragment.java
com.megginson.sloop.activities.package-info.java
com.megginson.sloop.model.Bookmark.java
com.megginson.sloop.model.DataCollection.java
com.megginson.sloop.model.DataEntry.java
com.megginson.sloop.model.DataRecord.java
com.megginson.sloop.model.Util.java
com.megginson.sloop.model.ValueFilter.java
com.megginson.sloop.model.impl.ContainsStringFilter.java
com.megginson.sloop.model.impl.DataCollectionIO.java
com.megginson.sloop.model.impl.DataCollectionImpl.java
com.megginson.sloop.model.impl.DataEntryImpl.java
com.megginson.sloop.model.impl.DataRecordImpl.java
com.megginson.sloop.model.impl.EqualsStringFilter.java
com.megginson.sloop.model.impl.package-info.java
com.megginson.sloop.model.package-info.java
com.megginson.sloop.ui.BookmarkListAdapter.java
com.megginson.sloop.ui.DataCollectionLoader.java
com.megginson.sloop.ui.DataCollectionPagerAdapter.java
com.megginson.sloop.ui.DataCollectionResult.java
com.megginson.sloop.ui.DataRecordFragment.java
com.megginson.sloop.ui.DataRecordListAdapter.java
com.megginson.sloop.ui.package-info.java