Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自定义适配器搜索视图_Java_Android_Searchview_Custom Adapter_Notesview - Fatal编程技术网

Java 使用自定义适配器搜索视图

Java 使用自定义适配器搜索视图,java,android,searchview,custom-adapter,notesview,Java,Android,Searchview,Custom Adapter,Notesview,我正在尝试使用此适配器创建搜索视图: 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"; p

我正在尝试使用此适配器创建搜索视图:

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 getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) { // inflar componente visual
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_row, parent, false);
        //CardView cardsView = (CardView) convertView.findViewById(R.id.note_cardboard);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);

    } else holder = (ViewHolder) convertView.getTag(); // ya existe, solo es reciclarlo
    // Inicializa la vista con los datos de la nota
    NoteViewWrapper noteViewWrapper = data.get(position);
    holder.noteIdText.setText(String.valueOf(noteViewWrapper.note.getId()));
    holder.noteTitleText.setText(noteViewWrapper.note.getTitle());
    // se la nota è più di 80 caratteri"..."
    holder.noteContentText.setText(noteViewWrapper.note.getContent().length() >= 80 ? noteViewWrapper.note.getContent().substring(0, 80).concat("...") : noteViewWrapper.note.getContent());
    holder.noteDateText.setText(DATETIME_FORMAT.format(noteViewWrapper.note.getUpdatedAt()));
    // Cambia il colore se la nota è cliccata
    if (noteViewWrapper.isSelected) {
        CardView cardsView = (CardView) convertView.findViewById(R.id.note_cardboard);
        // sfondo grigio quando nota selezionata
        cardsView.setCardBackgroundColor(Color.parseColor("#cacaca"));
        //cardsView.setCardBackgroundColor(R.drawable.statelist_item_background);
        //cardsView.setBackgroundResource(R.drawable.selected_note);
        //holder.parent.setBackgroundColor(parent.getContext().getResources().getColor(R.color.selected_note));
        //LinearLayout.LayoutParams imageLayoutParams = new LinearLayout.LayoutParams(
        //   LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    } else {
        CardView cardsView = (CardView) convertView.findViewById(R.id.note_cardboard);
        // sfondo bianco quando nota non selezionata
        cardsView.setCardBackgroundColor(Color.WHITE);
        //holder.parent.setBackgroundColor(parent.getContext().getResources().getColor(android.R.color.transparent));
    }
    return convertView;
}


/**
 * Almacena componentes visuales para acceso rápido sin necesidad de buscarlos muy seguido.
 */
private static class ViewHolder {

    private TextView noteIdText;
    private TextView noteTitleText;
    private TextView noteContentText;
    private TextView noteDateText;

    private View parent;

    /**
     * Constructor. Encuentra todas los componentes visuales en el componente padre dado.
     *
     * @param parent un componente visual.
     */
    private ViewHolder(View parent) {
        this.parent = parent;
        noteIdText = (TextView) parent.findViewById(R.id.note_id);
        noteTitleText = (TextView) parent.findViewById(R.id.note_title);
        noteContentText = (TextView) parent.findViewById(R.id.note_content);
        noteDateText = (TextView) parent.findViewById(R.id.note_date);
    }
}}

更多信息:

请在适配器类中添加类似filterString searchText的公共方法

复制这些ArrayList

将所有项目添加到复制的ArrayList


从SearchView onQueryTextChaged{}方法调用这些函数并传递值。

这对sqlite有效吗?myItem应该是什么?是的。事实上,您正在从sqlite加载数据,所以它将随对象列表而来,myItem我刚刚指定为您的modelObject类。contine DATA您能提出请求吗?因为以我的方式是不起作用的还有什么是删除方法?请注意,拉取请求可能确实有助于删除方法用于从阵列中删除项
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return false;
        }
    });

    searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {
            //Do some magic
        }

        @Override
        public void onSearchViewClosed() {
            //Do some magic
        }
    });
private ArrayList<NotesAdapter.NoteViewWrapper> notesData;
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();
}