content: package allen.town.focus.twitter.adapters;
  /*
   * 
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import android.app.Activity;
  import android.content.Context;
  import android.database.Cursor;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CursorAdapter;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import androidx.appcompat.widget.ListPopupWindow;
  
  import allen.town.focus.twitter.R;
  import allen.town.focus.twitter.data.sq_lite.HashtagSQLiteHelper;
  import allen.town.focus.twitter.utils.AutoCompleteHelper;
  
  public class AutoCompleteHashtagAdapter extends CursorAdapter {
  
      private Cursor cursor;
      private AutoCompleteHelper helper;
      private ListPopupWindow listPopupWindow;
  
      public AutoCompleteHashtagAdapter(ListPopupWindow listPopup, Context context, Cursor cursor, EditText text) {
          super(context, cursor);
  
          this.listPopupWindow = listPopup;
          this.cursor = cursor;
          this.context = context;
          this.text = text;
  
          inflater = LayoutInflater.from(context);
          helper = new AutoCompleteHelper();
      }
  
      protected Context context;
  
      private EditText text;
  
      private LayoutInflater inflater;
  
      public static class ViewHolder {
          public TextView text;
      }
  
      @Override
      public int getCount() {
          try {
              return cursor.getCount();
          } catch (Exception e) {
              return 0;
          }
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          try {
              if (!cursor.moveToPosition(cursor.getCount() - 1 - position)) {
                  throw new IllegalStateException("couldn't move cursor to position " + position);
              }
          } catch (Exception e) {
              ((Activity)context).recreate();
              return null;
          }
  
          View v;
          if (convertView == null) {
              v = newView(context, cursor, parent);
          } else {
              v = convertView;
          }
  
          bindView(v, context, cursor);
  
          return v;
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
          View v;
          final ViewHolder holder;
  
          v = inflater.inflate(R.layout.text, viewGroup, false);
  
          holder = new ViewHolder();
  
          holder.text = (TextView) v.findViewById(R.id.text);
  
          // sets up the font sizes
          holder.text.setTextSize(24);
  
          v.setTag(holder);
          return v;
      }
  
      @Override
      public void bindView(View view, Context context, Cursor cursor) {
          final ViewHolder holder = (ViewHolder) view.getTag();
  
          final String tag = cursor.getString(cursor.getColumnIndex(HashtagSQLiteHelper.COLUMN_TAG));
  
          holder.text.setText(tag);
  
          holder.text.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  helper.completeTweet(text, tag, '#');
                  text.setText(text.getText().toString().replace("# #", "#"));
                  if (listPopupWindow != null && listPopupWindow.isShowing()) {
                      listPopupWindow.dismiss();
                  }
              }
          });
      }
  }
  