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布局文件
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/timeview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"
/>
<TextView
android:id="@+id/dataview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"/>
</LinearLayout>
4 主布局文件 一个listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<ListView
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" />
</RelativeLayout>
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<Map<String, String>> listItemsList=new ArrayList<Map<String,String>>();
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<String,String> map=new HashMap<String, String>();
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);
}
}
运行结果