android listview 绑定数据库数据,android listview显示数据库内容

1.   创建数据库与数据表的类

package com.mybook.listdemo;

/**

* Created by 182 on 2016/2/18.

*/

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

/**

* 用于创建数据库 NO time0=年月日 time1=时间 data0=温度 data1=湿度

* @author Administrator

*

*/

public class MySQLite extends SQLiteOpenHelper{

final String CREAT_RABLE_SQL="create table weather(_id integer primary key autoincrement ,time0,time1,data0,data1)";

public MySQLite(Context context,String name,int version) {

super(context,name,null,version);

// TODO Auto-generated constructor stub

}

@Override

public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub

db.execSQL(CREAT_RABLE_SQL);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub

}

}2  向数据库添加内容的类

package com.mybook.listdemo;

import android.database.sqlite.SQLiteDatabase;

/**

* Created by yf182 on 2016/2/18.

*/

public class ManageSQL {

//MySQLite mySQLite;

/*public void creatSQL(){

mySQLite=new MySQLite(this, "MyWeather.db3",1);

}*/

//insert the temperature and humidity datas

public void insertT(SQLiteDatabase db,String time0,String time1,String data0,String data1){

db.execSQL("insert into weather values (null , ? , ? , ? , ?)", new String[]{time0,time1,data0,data1});

}

}

3 单个item布局文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/timeview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello_world"

/>

android:id="@+id/dataview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello_world"/>

4 主布局文件 一个listview

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:showIn="@layout/activity_main" tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/listView"

android:layout_alignParentTop="true"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true" />

5  初始化与 adapter 文件

package com.mybook.listdemo;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.view.Menu;

import android.view.MenuItem;

import android.app.Activity;

import android.database.Cursor;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

ListView listView;

MySQLite mySQLite;

ManageSQL manageSQL;

String time0, time1, data0, data1;

Cursor cursor;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

List> listItemsList=new ArrayList>();

time0="time0";

time1="time1";

data0="data0";

data1="data1";

listView=(ListView)findViewById(R.id.listView);

mySQLite=new MySQLite(this, "mydb.db3", 1);

manageSQL=new ManageSQL();

manageSQL.insertT(mySQLite.getReadableDatabase(), time0, time1, data0, data1);

cursor=mySQLite.getReadableDatabase().rawQuery("select * from weather" , null);

SimpleAdapter adapter=new SimpleAdapter(this, listItemsList, R.layout.activity_listview,

new String[]{"time0","data0"},

new int[]{R.id.timeview,R.id.dataview});

while(cursor.moveToNext()){

Map map=new HashMap();

map.put("time0", cursor.getString(0));//数据库中第一列的内容显示在listview的左边

map.put("data0", cursor.getColumnName(2));//数据库的第三列内容显示在listview的右边

listItemsList.add(map);

}

listView.setAdapter(adapter);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

运行结果

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值