如何仅将数据库中的部分数据显示到列表视图中
这是我的代码,用于将数据从数据库检索到列表视图。
private void fillData() {
Cursor c = viewDB.fetchAllRows();
startManagingCursor(c);
String[] from = new String[] { ViewComplaintsDataDB.KEY_NUMBER,
ViewComplaintsDataDB.KEY_DESCRIPTION,
ViewComplaintsDataDB.KEY_LOGGEDBY }; //these three are the fields in my db
int[] to = new int[] { R.id.com_num, R.id.com_desc , R.id.user }; //these three are the textview id's in my listitem.xml file
SimpleCursorAdapter simple = new SimpleCursorAdapter(this,
R.layout.listitem, c, from, to);
setListAdapter(simple);
}
这是有效的。
我的数据库第二列下的每一行都有很多数据。我如何才能将第二个字段中的 50 个字符的数据显示到列表视图中。请帮我。
Here is my code to retriev the data from db to a listview.
private void fillData() {
Cursor c = viewDB.fetchAllRows();
startManagingCursor(c);
String[] from = new String[] { ViewComplaintsDataDB.KEY_NUMBER,
ViewComplaintsDataDB.KEY_DESCRIPTION,
ViewComplaintsDataDB.KEY_LOGGEDBY }; //these three are the fields in my db
int[] to = new int[] { R.id.com_num, R.id.com_desc , R.id.user }; //these three are the textview id's in my listitem.xml file
SimpleCursorAdapter simple = new SimpleCursorAdapter(this,
R.layout.listitem, c, from, to);
setListAdapter(simple);
}
This is working.
There is a lot of data in each row under the second column in my DB. How can i display only 50 characters of data from the second field to the listview. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将 textView 的 maxLength 属性设置为 50。
You could just set the maxLength atribute from the textView to 50.
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="50"></TextView>
为此,您需要
ArrayAdapter
而不是SimpleCursorAdapter
并从DB
获取显示所需的数据并将其设置为适配器......for that you need to
ArrayAdapter
instead ofSimpleCursorAdapter
and fetch the data required to show from theDB
and set it to the adapter....