Android 来自 SQLite 结果的自定义 ArrayAdapter
Android Custom ArrayAdapter from SQLite results
我想根据我的 SQLite 结果制作一个自定义适配器,但是,我一直坚持这样做 :( 我如何根据这段代码制作一个自定义适配器?
我正在通过 DbHelper.java
获取我的数据库记录
这是我的 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;
}
}
这是我的Note.javaClass
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
这就是 returns 数据,我想将这些结果放入列表视图中。
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;
}
不太明白你的问题。
您只需要在适配器中传递您的数据项并在 getView() 中填充您的自定义视图。我真的看不出问题所在。这里有一个关于 ArrayAdapter 用法的很好的教程:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 希望这会有所帮助。
我想根据我的 SQLite 结果制作一个自定义适配器,但是,我一直坚持这样做 :( 我如何根据这段代码制作一个自定义适配器? 我正在通过 DbHelper.java
获取我的数据库记录这是我的 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;
}
}
这是我的Note.javaClass
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 这就是 returns 数据,我想将这些结果放入列表视图中。
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;
}
不太明白你的问题。
您只需要在适配器中传递您的数据项并在 getView() 中填充您的自定义视图。我真的看不出问题所在。这里有一个关于 ArrayAdapter 用法的很好的教程:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 希望这会有所帮助。