可以向 SimpleCursorAdapter 添加附加列
我已成功使用 SimpleCursorAdapter 列出显示名称和值(均在我的数据库中)并将它们显示在我的列表活动中。
我现在想做的是为列表活动中的每个项目添加一个新视图(ImageView)。最后应该看起来像这样。
Image_1_NotInDB -- DisplayName1FromDB -- DisplayName1FromDB
Image_2_NotInDB -- DisplayName2FromDB -- DisplayName2FromDB.
图像将会有所不同(基于 DisplayName1FromDB)。我认为 SimpleCursorAdapter 不再适合此目的。
我尝试创建一个扩展SimpleCursorAdapter的customSimpleCursorAdapter,并尝试使用“newView”和“bindView”方法来实现。我几乎遵循了这个:自定义 CursorAdapters。
问题是;我使用的图像基于数据库中的值(我打算在构造函数或 customSimpleCursorAdapter 中传递该值)
public View newView(Context pContext, Cursor pCursor, ViewGroup pParent)
{
Cursor lCursor = getCursor();
final LayoutInflater inflater = LayoutInflater.from(pContext);
View lView = inflater.inflate(layout, pParent, false);
int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
// "variable" is a member variable (set at the constructor)
ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
if (lImageView != null)
{
lImageView.setImageResource(lImage);
}
return pParent;
}
public void bindView(View pView, Context pContext, Cursor pCursor)
{
int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
// "variable" is a member variable (set at the constructor)
ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
if (lImageView != null)
{
lImageView.setImageResource(lImage);
}
}
这就是我尝试使用“customSimpleCursorAdapter”的方法
String[] lDisplay = new String[] {KEY_NAME, KEY_TIME};
int[] lValues = new int[] {R.id.name, R.id.time};
CustomRowCursorAdapter lCursorAdapter = new CustomRowCursorAdapter(this, R.layout.row, lSTime, lDisplay, lValues, "MY IDEA WAS TO PASS THE ANIMAL NAME HERE, BUT NOT LUCK as I am not sure How ");
lCursorAdapter.newView(this, lSTime, getListView());
lCursorAdapter.bindView(getListView(), this, lSTime);
setListAdapter(lCursorAdapter);
ArrayAdapter 是答案吗?如果是,您能分享一下您将传递给它的参数吗?
I have successfully used a SimpleCursorAdapter to list displayname and value (both in my DB) and displayed them in my list activity.
What I am was trying to do now was add a new view (ImageView) for each item in my list activity. It is supposed to look like this in the end.
Image_1_NotInDB -- DisplayName1FromDB -- DisplayName1FromDB
Image_2_NotInDB -- DisplayName2FromDB -- DisplayName2FromDB.
The image is going to different (based on DisplayName1FromDB). I don't think SimpleCursorAdapter is good anymore for this purpose.
I tried creating a customSimpleCursorAdapter extending SimpleCursorAdapter and tried to use 'newView' and 'bindView' methods to achieve. I pretty much followed this: Custom CursorAdapters.
The problem is; what Image I use is based on a value from DB (which I intended to pass in the constructor or customSimpleCursorAdapter)
public View newView(Context pContext, Cursor pCursor, ViewGroup pParent)
{
Cursor lCursor = getCursor();
final LayoutInflater inflater = LayoutInflater.from(pContext);
View lView = inflater.inflate(layout, pParent, false);
int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
// "variable" is a member variable (set at the constructor)
ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
if (lImageView != null)
{
lImageView.setImageResource(lImage);
}
return pParent;
}
public void bindView(View pView, Context pContext, Cursor pCursor)
{
int lImage = "dog".equals(variable) ? R.drawable.dog : R.drawable.cat;
// "variable" is a member variable (set at the constructor)
ImageView lImageView = (ImageView) lView.findViewById(R.id.appImage);
if (lImageView != null)
{
lImageView.setImageResource(lImage);
}
}
This is how I tried using the "customSimpleCursorAdapter"
String[] lDisplay = new String[] {KEY_NAME, KEY_TIME};
int[] lValues = new int[] {R.id.name, R.id.time};
CustomRowCursorAdapter lCursorAdapter = new CustomRowCursorAdapter(this, R.layout.row, lSTime, lDisplay, lValues, "MY IDEA WAS TO PASS THE ANIMAL NAME HERE, BUT NOT LUCK as I am not sure How ");
lCursorAdapter.newView(this, lSTime, getListView());
lCursorAdapter.bindView(getListView(), this, lSTime);
setListAdapter(lCursorAdapter);
Is ArrayAdapter the answer? If yes, could you share what parameters would you pass to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,你不能在这里使用
ArrayAdapter
。由于您需要显示数据库中的数据,因此您必须在此处使用自定义CursorAdapter
。No you can not use the
ArrayAdapter
here. Because you need to display data from the database therefore you must use here a CustomCursorAdapter
.您可以向数据库中的投影添加一个附加字段。
这是我用来根据商品价格(以美分存储)和关联单位来格式化成本的投影示例。
将其用作列,您可以使用
column
到R.id.*
的法线映射将其与视图中的字段配对。You could add an additional field to the projection from the database.
Here is an example of a projection I have used to format a cost based off of the a price of an item (stored in cents) and an associated unit.
Use that as a column and you can pair it with a field in your view using the normal mapping of
column
toR.id.*
.我建议使用定制适配器
I recommend to go for custom adapter