Joestoen
Joestoen

Reputation: 605

Android Custom ArrayAdapter from SQLite results

I want to make a custom adapter out of my SQLite results, however, I'm stuck doing so :( How would I make a custom adapter out of this code? I'm getting my Database records through my DbHelper.java

Here's my ListAdapter Class

public class ListAdapter extends ArrayAdapter<Note> {

Context mContext;
int layoutResourceId;
Note notes[] = null;

public ListAdapter(Context context, int layoutResourceId, Note[] notes) {
    super(context, layoutResourceId, notes);

    this.mContext = context;
    this.layoutResourceId = layoutResourceId;
    this.notes = notes;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    NoteHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new NoteHolder();
        holder.noteSubject = (TextView)row.findViewById(R.id.editTextSubject);
        holder.noteDesc = (TextView)row.findViewById(R.id.editTextTODO);

        row.setTag(holder);
    }
    else
    {
        holder = (NoteHolder)row.getTag();
    }

    Note note = notes[position];
    holder.noteSubject.setText(note.noteSubject);
    holder.noteDesc.setText(note.noteDescription);

    return row;
}

static class NoteHolder
{
    TextView noteSubject;
    TextView noteDesc;
}

}

Here's my Note.java Class

public class Note {
int id;
String noteSubject;
String noteDescription;

public Note(){}

public Note(String note_subject, String note_desc){
    super();
    this.noteSubject = note_subject;
    this.noteDescription= note_desc;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNoteSubject() {
    return noteSubject;
}

public void setNoteSubject(String noteSubject) {
    this.noteSubject = noteSubject;
}

public String getNoteDescription() {
    return noteDescription;
}

public void setNoteDescription(String noteDescription) {
    this.noteDescription = noteDescription;
}

}

MY DbHelper.java This is what returns the data, i want to get these results into a Listview.

public List<Note> getAllNotes() {
    List<Note> notes = new ArrayList<>();

    String query = "SELECT * FROM " + TABLE_NOTES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()){
        do {
            int id = Integer.parseInt(cursor.getString(0));
            String noteSubject = cursor.getString(1);
            String noteDesc = cursor.getString(2);

            Note note = new Note();
            note.id = id;
            note.noteSubject = noteSubject;
            note.noteDescription = noteDesc;

            notes.add(note);

        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    Log.d("getAllNotes()", notes.toString());

    return notes;
}

Upvotes: 0

Views: 697

Answers (1)

Yurii Tsap
Yurii Tsap

Reputation: 3744

Don't quite caught your question.
You just need to pass your data items inside the adapter and fill your custom views inside the getView(). I can't really see the problem. Here you have a nice tutorial about Usage of ArrayAdapter:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView Hope this will help.

Upvotes: 1

Related Questions