android-ListActivity:在将数据放入行之前更改来自SQLite的数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android-ListActivity:在将数据放入行之前更改来自SQLite的数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2337字,纯文字阅读大概需要4分钟。
内容图文
![android-ListActivity:在将数据放入行之前更改来自SQLite的数据](/upload/InfoBanner/zyjiaocheng/880/716ceacbef1c4cc48c81bd9dd12e8d32.jpg)
这是我的ListActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
mDbHelper = new MyDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
// Get all of the rows from the database and create the item list
Cursor c = mDbHelper.fetchAllTransactions();
startManagingCursor(c);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{MyDbAdapter.KEY_DATE, MyDbAdapter.KEY_NAME};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.row_date, R.id.row_name};
// Now create a simple cursor adapter and set it to display
setListAdapter(new SimpleCursorAdapter(this, R.layout.my_list_row, c, from, to));
}
这是我的行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/row_date" android:layout_width="wrap_content" android:layout_height="fill_parent" />
<TextView android:id="@+id/row_name" android:layout_width="wrap_content" android:layout_height="fill_parent" />
</LinearLayout>
该日期实际上以int形式存储在SQLLite数据库中,因此它显示为整数.
有谁知道从int创建日期的方法,因此它不会显示为1216544611(或其他),而是显示为“ 12/11/2009 11:32”?
解决方法:
我怀疑您将需要编写自己的CursorAdapter来将int格式化为日期字符串.诀窍是实现bindView()在列表项显示之前对其进行格式化:
private final class MyCursorAdapter extends CursorAdapter {
MyCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override public void bindView(View view, Context context, Cursor cursor) {
// Format the date string
TextView dateView = (TextView) view.findViewById(R.id.row_date);
long millis = cursor.getLong(cursor.getColumnIndex(MyDbAdapter.KEY_DATE));
dateView.setText(DateFormat.getDateInstance().format(new Date(milis)));
// Do the rest of your view formatting
...
}
@Override View public newView(Context context, Cursor cursor, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.my_list_row, null);
bindView(view, context, cursor);
return view;
}
}
并将其连接到onCreate()中的Activity:
setListAdapter(new MyCursorAdapter(this, c));
内容总结
以上是互联网集市为您收集整理的android-ListActivity:在将数据放入行之前更改来自SQLite的数据全部内容,希望文章能够帮你解决android-ListActivity:在将数据放入行之前更改来自SQLite的数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。