Service Notes Team February 2016

Search View with my custom adapter

i am trying to make a search view with this adapter:

public class NotesAdapter extends BaseAdapter implements Filterable {
/**
 * Wrapper para notas. Util para cambiar el fondo de los item seleccionados.
 */
Paint paint = new Paint();
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_CATID = "id";
private static final String DATABASE_TABLE = "notes_schema-v%s.sql";
private SQLiteDatabase mDb;
private ArrayList<NotesAdapter.NoteViewWrapper> notesData;
public Context context;


public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public Filter getFilter() {
    return null;
}

public static class NoteViewWrapper {

    private final Note note;
    private boolean isSelected;

    /**
     * Contruye un nuevo NoteWrapper con la nota dada.
     *
     * @param note una nota.
     */
    public NoteViewWrapper(Note note) {
        this.note = note;
    }

    public Note getNote() {
        return note;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
}

private static final DateFormat DATETIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

private final List<NoteViewWrapper> data;

/**
 * Constructor.
 *
 * @param data la lista de notas a usar como fuente de datos para este adaptador.
 */
public NotesAdapter(List<NoteViewWrapper> data) {
    this.data = data;
}

/**
 * @return cuantos datos hay en la lista de notas.
 */
@Override
public int getCount() {
    return data.size();
}

/**
 * @param position la posición de la nota que se quiere
 * @return la nota en la posición dada.
 */
@Override
public NoteViewWrapper getItem(int position) {
    return data.get(position);
}

/**
 * @param position una posición
 * @return la misma posición dada
 */
@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getVie        

Answers


Muhammed Thasneem February 2016

Please add a public method like filter(String searchText) in adapter class;

private ArrayList<NotesAdapter.NoteViewWrapper> notesData;

Make a copy of these ArrayList

add all item to copied ArrayList

public void filter(String charItem) {
  String searchItem = charItem.toLowerCase(Locale.getDefault);
  notesData.remove();
  if(searchItem.length()<0){
     notesData.addAll(copyList);
  } else{
    for(myItem item : copyList){
      if(item.name.contines(searchItem){
         notesData.add(item);
      }
    }
  notifyDatasetChanged();
}

Call these function from your SearchView onQueryTextChaged(){} method and pass the value.

Post Status

Asked in February 2016
Viewed 1,975 times
Voted 9
Answered 1 times

Search




Leave an answer