android listview显示数据库内容
生活随笔
收集整理的這篇文章主要介紹了
android listview显示数据库内容
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.?? 創(chuàng)建數(shù)據(jù)庫與數(shù)據(jù)表的類
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; /*** 用于創(chuàng)建數(shù)據(jù)庫 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}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL(CREAT_RABLE_SQL);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}2? 向數(shù)據(jù)庫添加內(nèi)容的類?
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 dataspublic 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" ><TextViewandroid: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
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;@Overrideprotected 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));//數(shù)據(jù)庫中第一列的內(nèi)容顯示在listview的左邊map.put("data0", cursor.getColumnName(2));//數(shù)據(jù)庫的第三列內(nèi)容顯示在listview的右邊listItemsList.add(map);}listView.setAdapter(adapter);FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();}});}@Overridepublic 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;}@Overridepublic 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 SimplifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);} }
運行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的android listview显示数据库内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习中的有监督学习,无监督学习,半监
- 下一篇: Android 操作SQLite基本用法