gpt4 book ai didi

java - 来自 SQLite 结果的 Android 自定义 ArrayAdapter

转载 作者:行者123 更新时间:2023-12-01 10:34:41 24 4
gpt4 key购买 nike

我想从我的 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;
}

最佳答案

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

关于java - 来自 SQLite 结果的 Android 自定义 ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com