我正在关注sql lite的学校教程,输出是一个按钮列表。是否可以将其输出到ListView? 我已经尝试使用谷歌搜索和youtube但不知何故,我关注的教程似乎有更多的文件? 困惑的新手在这里!
提前致谢!
此代码显示在onCreate(Bundle savedInstanceState)
中private void ShowExistingIdeas() {
Idea[] ideas = new Ideas(this).getAllIdeas();
if(ideas != null && ideas.length > 0)
{
for(int index = 0; index < ideas.length; index++)
{
Button ideaButton = new Button(this);
ideaButton.setText(ideas[index].getTitle());
final int id = ideas[index].getId();
ideaButton.setTag(id);
ideaButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(OverviewActivity.this,
EditIdeaActivity.class);
i.putExtra("id", id);
startActivityForResult(i, 0);
}
});
//!!! The bit I'm trying to change. the xml with LinearLayout1 is completely empty****
((LinearLayout) findViewById(R.id.LinearLayout1)).
addView(ideaButton);
}
}
}
供您参考 - Ideas文件
public class Ideas {
private Idea[] ideas;
private Context context;
public Ideas(Context context) {
this.context = context;
}
public Idea[] getAllIdeas() {
return getIdeasWithKeyword(null);
}
public Idea[] getIdeasWithKeyword(String keyword) {
ideas = null;
SQLiteDatabase db = new DbHelper(context).getWritableDatabase();
String where = null;
String[] whereArgs = null;
if (keyword != null) {
where = DbHelper.COL_TITLE + " LIKE '%?%' OR " + DbHelper.COL_DESC
+ " LIKE '%?%'";
whereArgs = new String[] { keyword };
}
Cursor c = db.query(DbHelper.TABLE_IDEAS, DbHelper.COLUMNS, where,
whereArgs, null, null, null);
if (c != null && c.moveToFirst()) {
ideas = new Idea[c.getCount()];
boolean hasMore = true;
for (int index = 0; hasMore; hasMore = c.moveToNext(), index++) {
int id = c.getInt(c.getColumnIndex(DbHelper.COL_ID));
String title = c
.getString(c.getColumnIndex(DbHelper.COL_TITLE));
String description = c.getString(c
.getColumnIndex(DbHelper.COL_DESC));
int priority = c
.getInt(c.getColumnIndex(DbHelper.COL_PRIORITY));
ideas[index] = new Idea(id, title, description, priority);
}
}
if (db != null && db.isOpen())
db.close();
return ideas;
}
public Idea getIdea(int id) {
SQLiteDatabase db = new DbHelper(context).getWritableDatabase();
Cursor c = db.query(DbHelper.TABLE_IDEAS, DbHelper.COLUMNS, "_id = "
+ id, null, null, null, null);
Idea idea = null;
if (c != null && c.moveToFirst()) {
String title = c.getString(c.getColumnIndex(DbHelper.COL_TITLE));
String description = c.getString(c
.getColumnIndex(DbHelper.COL_DESC));
int priority = c.getInt(c.getColumnIndex(DbHelper.COL_PRIORITY));
idea = new Idea(id, title, description, priority);
}
if (db != null && db.isOpen())
db.close();
return idea;
}
public void addIdea(Idea idea) {
SQLiteDatabase db = new DbHelper(context).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DbHelper.COL_TITLE, idea.getTitle());
values.put(DbHelper.COL_DESC, idea.getDescription());
values.put(DbHelper.COL_PRIORITY, idea.getPriority());
db.insert(DbHelper.TABLE_IDEAS, null, values);
if (db != null && db.isOpen())
db.close();
}
public void deleteIdea(int ideaId) {
SQLiteDatabase db = new DbHelper(context).getWritableDatabase();
db.delete(DbHelper.TABLE_IDEAS, DbHelper.COL_ID + "=" + ideaId, null);
if (db != null && db.isOpen())
db.close();
}
}