c#怎么使查询到的一个表中的所有随机数据在listview中随机活动显示,类似抽奖屏幕滚动的效果

2010年 总版技术专家分年内排行榜第一2009年 总版技术专家分年内排行榜第一
2011年 总版技术专家分年内排行榜第二
本帖子已过去太久远了,不再提供回复功能。本帖子已过去太久远了,不再提供回复功能。我们有了数据库的内容之后,需要将其显示到手机屏幕上时,要怎样进行界面布局呢?
那么第一个知识点就是,对于ListView的布局,我们需要创建两个界面,一个用来显示表头与ListView,另一个用来显示ListView中每个item。我们现在需要显示的是一个具有三个属性:学号,姓名,年龄的表格,所以第一个界面布局的设计方法是:整个界面是一个垂直属性的线性布局,表头用一个线性布局表示,学号、姓名、年龄三列平均分,剩下的部分用来显示ListView;第二个界面的设计方法:与表头一致。代码如下:
activity_my_open_helper.xml:
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.administrator.sqlite.MyOpenHelper"&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"&
android:id="@+id/stu_number"
android:text="学号"
android:textSize="25sp"
android:layout_weight="1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
android:id="@+id/stu_name"
android:text="姓名"
android:textSize="25sp"
android:layout_weight="1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
android:id="@+id/stu_age"
android:text="年龄"
android:textSize="25sp"
android:layout_weight="1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
&/LinearLayout&
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/background_dark"/&
android:id="@+id/student_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"&&/ListView&
55 &/LinearLayout&
studentlayout.xml:
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"&
android:id="@+id/stu_number"
android:textSize="20dp"
android:gravity="center"
android:layout_weight="1"
android:text="学号"
android:layout_width="0dp"
android:layout_height="50dp" /&
android:id="@+id/stu_name"
android:textSize="20dp"
android:gravity="center"
android:layout_weight="1"
android:text="姓名"
android:layout_width="0dp"
android:layout_height="50dp" /&
android:id="@+id/stu_age"
android:textSize="20dp"
android:gravity="center"
android:layout_weight="1"
android:text="年龄"
android:layout_width="0dp"
android:layout_height="50dp" /&
&/LinearLayout&
37 &/LinearLayout&
  第二个知识点就是通过ListView显示,我们需要先把数据库的信息取到一个数组中然后一条条读取数组中的数据,并把它输出到ListView的一个个条目中。因为每一条信息都有三个属性,建立一个类把数据模型化,并在这个类中实现对属性的初始化、get与set,因为属性都是私有成员变量,需要利用公共的方法对其进行赋值与读取。利用游标与一个while训话可以扫描整个数据库,并把扫描的每一行添加到数组中,这样之后,我们就成功把数据库的信息写入到数组中,接下来我们要为ListView设置一个适配器,它有四个方法,我们主要对getCount(),getView()方法进行操作。代码及注释见下:
1 package com.administrator.
3 import android.app.A
4 import android.database.C
5 import android.database.sqlite.SQLiteD
6 import android.os.B
7 import android.os.PersistableB
8 import android.view.V
9 import android.view.ViewG
10 import android.widget.BaseA
11 import android.widget.ListV
12 import android.widget.TextV
14 import java.util.ArrayL
* Created by Administrator on .
19 public class student extends Activity {
private MyOpenH
private SQLiteD
private ArrayList&student_info&
private ListV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_open_helper);
//创建或打开数据库
moh=new MyOpenHelper(this,"student.db",null,2);
sd= moh.getReadableDatabase();
studentlist = new ArrayList&&();
//扫描数据库,将数据库信息放入studentlist
Cursor cursor = sd.rawQuery("select * from student",null);
while (cursor.moveToNext()){
String number = cursor.getString(cursor.getColumnIndex("number"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
student_info st = new student_info(number,name,age);
//student_info存一个条目的数据
studentlist.add(st);//把数据库的每一行加入数组中
//获取ListView,并通过Adapter把studentlist的信息显示到ListView
//为ListView设置一个适配器,getCount()返回数据个数;getView()为每一行设置一个条目
lv=(ListView)findViewById(R.id.student_lv);
lv.setAdapter(new BaseAdapter() {
public int getCount() {
return studentlist.size();
//ListView的每一个条目都是一个view对象
public View getView(int position, View convertView, ViewGroup parent) {
//对ListView的优化,convertView为空时,创建一个新视图;convertView不为空时,代表它是滚出
//屏幕,放入Recycler中的视图,若需要用到其他layout,则用inflate(),同一视图,用fiindViewBy()
if(convertView==null){
view = View.inflate(getBaseContext(),R.layout.studentlayout,null);
view = convertV
//从studentlist中取出一行数据,position相当于数组下标,可以实现逐行取数据
student_info st = studentlist.get(position);
TextView number = (TextView)view.findViewById(R.id.stu_number);
TextView name = (TextView)view.findViewById(R.id.stu_name);
TextView age = (TextView)view.findViewById(R.id.stu_age);
number.setText(st.getNumber());
name.setText(st.getName());
age.setText(st.getAge());
public Object getItem(int position) {
return null;
public long getItemId(int position) {
下面我们就来看一下结果吧~~
是不是感觉很美~~哈哈哈~~
阅读(...) 评论()09-1209-1209-1209-12
01-1710-1703-0611-14
◇本站云标签C#中使用listview显示数据库表信息时,实时刷新是怎么做到的?_百度知道
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。
C#中使用listview显示数据库表信息时,实时刷新是怎么做到的?
我有更好的答案
最简单的是 timer控件定期刷新。还有就是,做个服务,在数据库有新数据时候发送消息控制刷新。
我的SQL数据库中那张表实时地增添着新的行,我也想让显示表的listview实时地增添新的行,有办法吗?用timer控件倒是不麻烦,问题是我怎么样能找出SQL表中的新行呢?
- - 如果数据库只是不断新增,没有删除和修改的话,用timer来查找新增加的行就行了。每次记录一个NUMER,初始为0.每次查找数据库中ID大于这个NUMBER的记录。然后新增到LISTVIEW里。完事记录最大的那个ID给NUMBER.
采纳率:57%
重新给绑定新的data就可以了,Items重新加载绑定一遍,就是麻烦一些
我的SQL数据库中那张表实时地增添着新的行,我也想让显示表的listview实时地增添新的行,有办法吗?
这个如果你真的想要这样的话,我只想到了使用timer控件,繁琐的去添加item
用timer控件倒是不麻烦,问题是我怎么样能找出SQL表中的新行呢?
你SQL新添加的数据 从哪里执行的?可以在哪里一并解决了,传个数据回来也OK
为您推荐:
其他类似问题
listview的相关知识
等待您来回答}

我要回帖

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信