如何获取字段值而不是列名?
我想通过这种方式用光标中的数据填充列表:
myCursor = myDBA.getAllCompanies();
startManagingCursor(myCursor);
myListAdapter = new SimpleCursorAdapter(this, R.layout.company_row, myCursor, FROM, TO);
myListAdapter.setViewBinder(VIEW_BINDER);
companiesListView.setAdapter(myListAdapter);
我使用自定义 ViewBinder
来填充每行中的两个 TextViews
和一个 ImageView
:
private final ViewBinder VIEW_BINDER = new ViewBinder() {
/**
* Binds the Cursor column defined by the specified index to the
* specified view.
*/
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch (viewId) {
case R.id.company_name:
case R.id.company_desc:
Log.d(TAG, "Binding TextView");
TextView venueText = (TextView) view;
venueText.setText(cursor.getString(columnIndex));
return true;
case R.id.company_icon:
Log.d(TAG, "Binding ImageView");
ImageView venueIcon = (ImageView) view;
String iconName;
iconName = cursor.getString(columnIndex); //Here I get the column name not the field value
Resources resources = myContext.getResources();
Drawable drawable = resources.getDrawable(resources
.getIdentifier("my.package:drawable/"
+ iconName, null, null));
venueIcon.setImageDrawable(drawable);
return true;
}
return false;
}
};
我的问题是 cursor.getString()
方法调用返回列名称而不是字段的值,因此我得到了数百行带有列名称的行。
更新:
我的 getAllCompanie 包含对 Cursor 的 moveToFirst 调用,但我仍然得到列名称:
public Cursor getAllCompanies(){
Cursor s = myDB.query(TABLE, KEYS, WHERE, null, null, null, null);
s.moveToFirst();
return s;
}
I want to fill a list with data from a cursor this way:
myCursor = myDBA.getAllCompanies();
startManagingCursor(myCursor);
myListAdapter = new SimpleCursorAdapter(this, R.layout.company_row, myCursor, FROM, TO);
myListAdapter.setViewBinder(VIEW_BINDER);
companiesListView.setAdapter(myListAdapter);
I use a custom ViewBinder
to fill two TextViews
and one ImageView
in each row:
private final ViewBinder VIEW_BINDER = new ViewBinder() {
/**
* Binds the Cursor column defined by the specified index to the
* specified view.
*/
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch (viewId) {
case R.id.company_name:
case R.id.company_desc:
Log.d(TAG, "Binding TextView");
TextView venueText = (TextView) view;
venueText.setText(cursor.getString(columnIndex));
return true;
case R.id.company_icon:
Log.d(TAG, "Binding ImageView");
ImageView venueIcon = (ImageView) view;
String iconName;
iconName = cursor.getString(columnIndex); //Here I get the column name not the field value
Resources resources = myContext.getResources();
Drawable drawable = resources.getDrawable(resources
.getIdentifier("my.package:drawable/"
+ iconName, null, null));
venueIcon.setImageDrawable(drawable);
return true;
}
return false;
}
};
My problem is the cursor.getString()
method call returns the column name not the value of the field and so I get hundreds of rows with column names.
Update:
My getAllCompanie
s includes a moveToFirst
call on the Cursor
but still I get column names:
public Cursor getAllCompanies(){
Cursor s = myDB.query(TABLE, KEYS, WHERE, null, null, null, null);
s.moveToFirst();
return s;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试将光标移动到第一个索引
cursor.moveToFirst();
然后执行cursor.getString(columnIndex);
?还有另一种方法,创建一个列表或数组来保存游标中的值,通过迭代游标访问其中的所有数据
,然后执行
此解决方案不是最好的,但无论如何都能达到目的。
Did you tried to move the cursor to the first index
cursor.moveToFirst();
then docursor.getString(columnIndex);
?There is another way, create one List or array that will hold the values from the cursor, access all the data inside it by iterating through the cursor
then just do
This solution is not the best but how ever serves the purpose.
你尝试过吗?
它会从column_name中获取字符串
Did you try?
it will get you the string from the column_name