Android Open Source - BART Text Plus Icon Array Adapter

From Project

Back to project page BART.


The source code is released under:

GNU General Public License

If you think the Android project BART 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 pro.dbro.bart;
/*from ww w  .j a va 2s.  co  m*/
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

//  Intended to be used on AutoCompleteTextViews for origin and destination station
//  Assume that objects is a 2d array of [String value, String type]. i.e: ["Downtown Berkeley", "nearby"], ["Downtown Berkeley", "recent"]
public class TextPlusIconArrayAdapter extends ArrayAdapter<StationSuggestion> implements Filterable{
  private ArrayList<StationSuggestion> stations;
  private final Context context;
  private Filter mFilter;
  // Custom constructor ignores view resource id, since it will always be R.layout.dropdown_item
  public TextPlusIconArrayAdapter(Context context, ArrayList<StationSuggestion> values) {
      super(context, R.layout.dropdown_item);
      this.stations = values;
      this.context = context; //necessary?
  public int getCount() {
      return stations.size();

  public StationSuggestion getItem(int position) {
      return (StationSuggestion)stations.get(position);
  public Filter getFilter() {
      if (mFilter == null) {
          mFilter = new CustomFilter();
      return mFilter;
  // For the recent/nearby stations, don't perform any filtering
  // When search text entered
  // Eventually let this ArrayAdapter handle both station suggestions and 
  // all stations
  private class CustomFilter extends Filter {

    // Always return All results. No filtering 
      protected FilterResults performFiltering(CharSequence constraint) {
          FilterResults results = new FilterResults();
          if(constraint == null || constraint.length() == 0) {
              ArrayList<String> list = new ArrayList<String>(mOrigionalValues);
              results.values = list;
              results.count = list.size();
          } else {
              ArrayList<String> newValues = new ArrayList<String>();
              for(int i = 0; i < mOrigionalValues.size(); i++) {
                  String item = mOrigionalValues.get(i);
                  if(item.contains(constraint)) {
              results.values = newValues;
              results.count = newValues.size();
          results.values = stations;
          results.count = stations.size();
          return results;

      protected void publishResults(CharSequence constraint,
              FilterResults results) {
          stations = (ArrayList<StationSuggestion>) results.values;
          //Log.d("CustomArrayAdapter", String.valueOf(results.values));
          //Log.d("CustomArrayAdapter", String.valueOf(results.count));


      public View getView(int position, View convertView, ViewGroup parent) {
     LayoutInflater inflater = (LayoutInflater) this.getContext()
     View rowView = inflater.inflate(R.layout.dropdown_item, null, true);
     TextView nameView = (TextView)rowView.findViewById(;
     ImageView iconView = (ImageView)rowView.findViewById(;
     //  Assume objects is a 2d String array i.e: ["Downtown Berkeley", "nearby"]
     //  Set nameView to station name
     nameView.setText( ((StationSuggestion)this.getItem(position)).station );
     //  Set iconView to icon representing type
     if( ((StationSuggestion)this.getItem(position)).type.compareTo("nearby") == 0 ){
     else if( ((StationSuggestion)this.getItem(position)).type.compareTo("recent") == 0 ){
       if(((StationSuggestion)this.getItem(position)).hits > StationSuggestion.LOVE_THRESHOLD)
     return rowView;


Java Source Code List