Android 简单显示SQlite数据库中的数据
我试图找到一个在布局中显示SQLite数据库数据的简单示例。 我试图查看“Notebook”示例,但它只有一个输出列。 有没有一个例子是多个输出列,如果可能的话,在某些列中包含整数数据?Blitz 你可能想考虑创建自己的客户。对于我的项目,我创建了一个自定义。结果是: 如果您想以自定义游标适配器为例,这里是我的代码:Android 简单显示SQlite数据库中的数据,android,sqlite,listview,Android,Sqlite,Listview,我试图找到一个在布局中显示SQLite数据库数据的简单示例。 我试图查看“Notebook”示例,但它只有一个输出列。 有没有一个例子是多个输出列,如果可能的话,在某些列中包含整数数据?Blitz 你可能想考虑创建自己的客户。对于我的项目,我创建了一个自定义。结果是: 如果您想以自定义游标适配器为例,这里是我的代码: public class ItemAdapter extends CursorAdapter { private LayoutInflater mLayoutInflat
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.items_row, parent, false);
return v;
}
/**
* @author will
*
* @param v
* The view in which the elements we set up here will be displayed.
*
* @param context
* The running context where this ListView adapter will be active.
*
* @param c
* The Cursor containing the query results we will display.
*/
@Override
public void bindView(View v, Context context, Cursor c) {
String title = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_TITLE));
String date = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DATE));
String imagePath = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_IMG));
String reminder = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_REMINDER));
int deletion = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DELETION));
int priority = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_PRIORITY));
/**
* Next set the title of the entry.
*/
TextView title_text = (TextView) v.findViewById(R.id.item_text);
if (title_text != null) {
title_text.setText(title);
}
setPriorityColor(title_text, priority);
/**
* Set Date
*/
TextView date_text = (TextView) v.findViewById(R.id.item_date);
if (date_text != null) {
date_text.setText(date);
}
/**
* Decide if we should display the paper clip icon denoting image attachment
*/
ImageView item_image = (ImageView) v.findViewById(R.id.item_attachment);
item_image.setVisibility(ImageView.INVISIBLE);
if (imagePath != null && imagePath.length() != 0 && item_image != null) {
item_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the deletion indicator
*/
ImageView del_image = (ImageView) v.findViewById(R.id.item_deletion);
del_image.setVisibility(ImageView.INVISIBLE);
if (deletion == 1) {
del_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the reminder indicator
*/
ImageView rem_image = (ImageView) v.findViewById(R.id.item_reminder);
rem_image.setVisibility(ImageView.INVISIBLE);
if(reminder != null && reminder.length() != 0 && rem_image != null) {
rem_image.setVisibility(ImageView.VISIBLE);
}
}
/**
* Set the priority colors based on the SharedPreferences
*
* @author will
*
* @param title
* The particular TextView item we are handling
* @param priority
* The current TextView item's associated priority level
*/
private void setPriorityColor(TextView title, int priority) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Resources res = mContext.getResources();
switch(priority) {
case ItemDbAdapter.PRIORITY_HIGH:
title.setTextColor(prefs.getInt("highColor", res.getColor(R.color.high_priority)));
break;
case ItemDbAdapter.PRIORITY_NORMAL:
title.setTextColor(prefs.getInt("normColor", res.getColor(R.color.norm_priority)));
break;
case ItemDbAdapter.PRIORITY_LOW:
title.setTextColor(prefs.getInt("lowColor", res.getColor(R.color.low_priority)));
break;
}
}
}
private void fillData(String sortOrder) {
Cursor itemsCursor = mDbHelper.fetchAllItems(sortOrder);
startManagingCursor(itemsCursor);
ItemAdapter itemAdapter = new ItemAdapter(this, itemsCursor);
setListAdapter(itemAdapter);
itemAdapter = null;
}
和单个项目的xml:
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.items_row, parent, false);
return v;
}
/**
* @author will
*
* @param v
* The view in which the elements we set up here will be displayed.
*
* @param context
* The running context where this ListView adapter will be active.
*
* @param c
* The Cursor containing the query results we will display.
*/
@Override
public void bindView(View v, Context context, Cursor c) {
String title = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_TITLE));
String date = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DATE));
String imagePath = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_IMG));
String reminder = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_REMINDER));
int deletion = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DELETION));
int priority = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_PRIORITY));
/**
* Next set the title of the entry.
*/
TextView title_text = (TextView) v.findViewById(R.id.item_text);
if (title_text != null) {
title_text.setText(title);
}
setPriorityColor(title_text, priority);
/**
* Set Date
*/
TextView date_text = (TextView) v.findViewById(R.id.item_date);
if (date_text != null) {
date_text.setText(date);
}
/**
* Decide if we should display the paper clip icon denoting image attachment
*/
ImageView item_image = (ImageView) v.findViewById(R.id.item_attachment);
item_image.setVisibility(ImageView.INVISIBLE);
if (imagePath != null && imagePath.length() != 0 && item_image != null) {
item_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the deletion indicator
*/
ImageView del_image = (ImageView) v.findViewById(R.id.item_deletion);
del_image.setVisibility(ImageView.INVISIBLE);
if (deletion == 1) {
del_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the reminder indicator
*/
ImageView rem_image = (ImageView) v.findViewById(R.id.item_reminder);
rem_image.setVisibility(ImageView.INVISIBLE);
if(reminder != null && reminder.length() != 0 && rem_image != null) {
rem_image.setVisibility(ImageView.VISIBLE);
}
}
/**
* Set the priority colors based on the SharedPreferences
*
* @author will
*
* @param title
* The particular TextView item we are handling
* @param priority
* The current TextView item's associated priority level
*/
private void setPriorityColor(TextView title, int priority) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Resources res = mContext.getResources();
switch(priority) {
case ItemDbAdapter.PRIORITY_HIGH:
title.setTextColor(prefs.getInt("highColor", res.getColor(R.color.high_priority)));
break;
case ItemDbAdapter.PRIORITY_NORMAL:
title.setTextColor(prefs.getInt("normColor", res.getColor(R.color.norm_priority)));
break;
case ItemDbAdapter.PRIORITY_LOW:
title.setTextColor(prefs.getInt("lowColor", res.getColor(R.color.low_priority)));
break;
}
}
}
private void fillData(String sortOrder) {
Cursor itemsCursor = mDbHelper.fetchAllItems(sortOrder);
startManagingCursor(itemsCursor);
ItemAdapter itemAdapter = new ItemAdapter(this, itemsCursor);
setListAdapter(itemAdapter);
itemAdapter = null;
}
以下是主要活动的代码:
我调用此函数以使用自定义适配器显示光标:
public class ItemAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.items_row, parent, false);
return v;
}
/**
* @author will
*
* @param v
* The view in which the elements we set up here will be displayed.
*
* @param context
* The running context where this ListView adapter will be active.
*
* @param c
* The Cursor containing the query results we will display.
*/
@Override
public void bindView(View v, Context context, Cursor c) {
String title = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_TITLE));
String date = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DATE));
String imagePath = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_IMG));
String reminder = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_REMINDER));
int deletion = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DELETION));
int priority = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_PRIORITY));
/**
* Next set the title of the entry.
*/
TextView title_text = (TextView) v.findViewById(R.id.item_text);
if (title_text != null) {
title_text.setText(title);
}
setPriorityColor(title_text, priority);
/**
* Set Date
*/
TextView date_text = (TextView) v.findViewById(R.id.item_date);
if (date_text != null) {
date_text.setText(date);
}
/**
* Decide if we should display the paper clip icon denoting image attachment
*/
ImageView item_image = (ImageView) v.findViewById(R.id.item_attachment);
item_image.setVisibility(ImageView.INVISIBLE);
if (imagePath != null && imagePath.length() != 0 && item_image != null) {
item_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the deletion indicator
*/
ImageView del_image = (ImageView) v.findViewById(R.id.item_deletion);
del_image.setVisibility(ImageView.INVISIBLE);
if (deletion == 1) {
del_image.setVisibility(ImageView.VISIBLE);
}
/**
* Decide if we should display the reminder indicator
*/
ImageView rem_image = (ImageView) v.findViewById(R.id.item_reminder);
rem_image.setVisibility(ImageView.INVISIBLE);
if(reminder != null && reminder.length() != 0 && rem_image != null) {
rem_image.setVisibility(ImageView.VISIBLE);
}
}
/**
* Set the priority colors based on the SharedPreferences
*
* @author will
*
* @param title
* The particular TextView item we are handling
* @param priority
* The current TextView item's associated priority level
*/
private void setPriorityColor(TextView title, int priority) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Resources res = mContext.getResources();
switch(priority) {
case ItemDbAdapter.PRIORITY_HIGH:
title.setTextColor(prefs.getInt("highColor", res.getColor(R.color.high_priority)));
break;
case ItemDbAdapter.PRIORITY_NORMAL:
title.setTextColor(prefs.getInt("normColor", res.getColor(R.color.norm_priority)));
break;
case ItemDbAdapter.PRIORITY_LOW:
title.setTextColor(prefs.getInt("lowColor", res.getColor(R.color.low_priority)));
break;
}
}
}
private void fillData(String sortOrder) {
Cursor itemsCursor = mDbHelper.fetchAllItems(sortOrder);
startManagingCursor(itemsCursor);
ItemAdapter itemAdapter = new ItemAdapter(this, itemsCursor);
setListAdapter(itemAdapter);
itemAdapter = null;
}
主要活动的XML:顶部相对布局用于操作栏
Thx。我会仔细查看并尝试一下。你能给我一个例子,说明你是如何将它整合到整个UI屏幕中的吗?添加的代码显示了适配器是如何从主活动中使用的,以及主活动的xml是如何使用的。谢谢。还有一个问题:由于没有setListAdapter,如何将数据放入对话框中。