【问题标题】:Android Custom ArrayAdapter from SQLite results来自 SQLite 结果的 Android 自定义 ArrayAdapter
【发布时间】:2016-01-17 10:33:52
【问题描述】:

我想从我的 SQLite 结果中制作一个自定义适配器,但是,我坚持这样做:( 我如何使用这段代码制作一个自定义适配器? 我正在通过我的 DbHelper.java 获取我的数据库记录

这是我的 ListAdapter 类

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;
}

}

这是我的 Note.java 类

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;
}

}

我的 DbHelper.java 这是返回数据的内容,我想将这些结果放入 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;
}

【问题讨论】:

    标签: java android sqlite custom-adapter


    【解决方案1】:

    不太明白你的问题。
    您只需在适配器中传递数据项并在 getView() 中填充您的自定义视图。我真的看不出问题。这里有一个很好的关于 ArrayAdapter 用法的教程:
    https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多