Android Open Source - note-pad Note Adapter






From Project

Back to project page note-pad.

License

The source code is released under:

GNU General Public License

If you think the Android project note-pad 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

/**
 * /*w  w w.j a  va2s  .com*/
 */
package in.anandm.apps.notepad.interfaces;

import in.anandm.apps.notepad.R;
import in.anandm.apps.notepad.domain.model.note.INoteRepository;
import in.anandm.apps.notepad.domain.model.note.Note;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

/**
 * @author anandm
 *
 */
public class NoteAdapter extends BaseAdapter implements Filterable{

  private Context context;
  private List<Note> notes;
  private final INoteRepository noteRepository;

  public NoteAdapter(Context context,INoteRepository noteRepository) {

    this.context = context;
    this.noteRepository = noteRepository;
    this.notes = noteRepository.getAll();

    //sort 
    sort();
  }

  //View Holder pattern for avoiding findViewById call
  static class NoteViewHolder{
    TextView noteTitle;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    //reuse convertView and create new only if it's null 
    View rowView = convertView;

    if(rowView == null){
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      rowView = inflater.inflate(R.layout.notes_list_row, parent, false);

      NoteViewHolder viewHolder = new NoteViewHolder();
      viewHolder.noteTitle = (TextView) rowView.findViewById(R.id.noteTitle);

      rowView.setTag(viewHolder);

    }

    NoteViewHolder viewHolder = (NoteViewHolder) rowView.getTag();

    viewHolder.noteTitle.setText(notes.get(position).getTitle());

    return rowView;
  }

  @Override
  public int getCount() {

    return notes.size();
  }

  @Override
  public Object getItem(int position) {

    return notes.get(position);
  }

  @Override
  public long getItemId(int position) {
    return notes.get(position).getId();
  }

  @Override
  public Filter getFilter() {
    return new Filter() {

      @Override
      protected void publishResults(CharSequence constraint, FilterResults results) {
        notes = (List<Note>) results.values;
        //sort values
        sort();
        NoteAdapter.this.notifyDataSetChanged();
      }

      @Override
      protected FilterResults performFiltering(CharSequence constraint) {
        List<Note> filteredNotes = new ArrayList<Note>();
        for (Note note : noteRepository.getAll()) {
          if(note.getTitle().contains(constraint)){
            filteredNotes.add(note);
          }
        }
        FilterResults filterResults = new FilterResults();
        filterResults.count = filteredNotes.size();
        filterResults.values = filteredNotes;
        return filterResults;
      }
    };
  }

  private void sort(){
    Collections.sort(this.notes, new Comparator<Note>() {

      @Override
      public int compare(Note lhs, Note rhs) {

        return (int) (lhs.getId() - rhs.getId());
      }
    });
  }
  
  
}




Java Source Code List

in.anandm.apps.notepad.domain.model.note.INoteRepository.java
in.anandm.apps.notepad.domain.model.note.Note.java
in.anandm.apps.notepad.infrastructure.persistence.inmemory.NoteRepository.java
in.anandm.apps.notepad.infrastructure.persistence.sqlite.NoteRepository.java
in.anandm.apps.notepad.interfaces.NoteActivity.java
in.anandm.apps.notepad.interfaces.NoteAdapter.java
in.anandm.apps.notepad.interfaces.NotePadMainActivity.java