Android:在 CursorAdapter 中使用旋转文本
我有一个 ListView,里面充满了 SimpleCursorAdapter。应在此视图中显示的字段之一 (TBTsqlHelperBudgets.KEY_INTERVAL) 以 int 形式存储在数据库中(微调器的索引,用于输入)。但我想在 ListView 中显示文本而不是索引。有什么建议吗?
public class TBTTManageBudgetsActivity extends Activity {
// ...
// some stuff
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
// some stuff
// ...
cursor = getBudgets();
String[] uiBindFrom = { TBTsqlHelperBudgets.KEY_NAME, TBTsqlHelperBudgets.KEY_BUDGET, TBTsqlHelperBudgets.KEY_INTERVAL };
int[] uiBindTo = { R.id.name, R.id.budget, R.id.interval};
cursor.moveToFirst();
CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);
listView.setAdapter(adapter);
// ...
// some stuff
// ...
}
// ...
// some stuff
// ...
}
顺便说一句:微调器是使用 xml 数组设置的:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
使用 xml:
<resources>
<string-array name="intervals">
<item>per day</item>
<item>per week</item>
<item>per month</item>
<item>per year</item>
</string-array>
</resources>
I've got an ListView that is filled with an SimpleCursorAdapter. One of the Fields that should be displayed in this view (TBTsqlHelperBudgets.KEY_INTERVAL) is stored in the DB as int (index of the spinner, that is used for input). But I would like to show the text rather than the index in the ListView. Any Suggestions?
public class TBTTManageBudgetsActivity extends Activity {
// ...
// some stuff
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
// some stuff
// ...
cursor = getBudgets();
String[] uiBindFrom = { TBTsqlHelperBudgets.KEY_NAME, TBTsqlHelperBudgets.KEY_BUDGET, TBTsqlHelperBudgets.KEY_INTERVAL };
int[] uiBindTo = { R.id.name, R.id.budget, R.id.interval};
cursor.moveToFirst();
CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);
listView.setAdapter(adapter);
// ...
// some stuff
// ...
}
// ...
// some stuff
// ...
}
By the way: The spinner is set up using an xml array:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.intervals, android.R.layout.simple_spinner_item);
with the xml:
<resources>
<string-array name="intervals">
<item>per day</item>
<item>per week</item>
<item>per month</item>
<item>per year</item>
</string-array>
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想显示文本(而不是能够编辑它),您可以使用类似的方法来即时进行转换或查找。它只是简单地包装您的光标,然后您设置哪些列需要特殊处理。所有其他的都由底层 Cursor 对象处理:
If you just want to display the text (as opposed to being able to edit it), you can use something like this to do conversions or lookups on the fly. It simply wraps your Cursor and you set which columns need special handling. All others are handled by the underlying Cursor object: