android 数据库存储+listView+SimpleCursorAdapter 范例demo(亲测)
来源:互联网 发布:钓鱼 知乎 编辑:程序博客网 时间:2024/06/05 14:48
MainActivity代码如下:
package example.com.myapplication;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends Activity{ SQLiteDatabase db; Button bn = null; ListView listView; String TAG = "TAG"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建或打开数据库(此处需要使用绝对路径) db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir() .toString() + "/test.db3" , null); Log.i(TAG, "onCreate: "+ this.getFilesDir() .toString()); listView = (ListView)findViewById(R.id.show); bn = (Button)findViewById(R.id.ok); bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View source) { //获取用户输入 String title = ((EditText)findViewById(R.id.title)) .getText().toString(); String content = ((EditText)findViewById(R.id.content)) .getText().toString(); try { insertData(db , title , content); Cursor cursor = db.rawQuery("select * from news_inf", null); inflateList(cursor); } catch(SQLiteException se) { //执行DDL创建数据表 db.execSQL("create table news_inf(_id integer primary key autoincrement," + " news_title varchar(50)," + " news_content varchar(255))"); //执行insert语句插入数据 insertData(db , title , content); //执行查询 Cursor cursor = db.rawQuery("select * from news_inf", null); inflateList(cursor); } } }); } private void insertData(SQLiteDatabase db , String title , String content) { //执行插入语句 db.execSQL("insert into news_inf values(null , ? , ?)" , new String[]{title , content}); } private void inflateList(Cursor cursor) { //填充SimpleCursorAdapter SimpleCursorAdapter adapter = new SimpleCursorAdapter( MainActivity.this , R.layout.ine, cursor , new String[]{"news_title" , "news_content"} , new int[]{R.id.my_title , R.id.my_content}); //显示数据 listView.setAdapter(adapter); } @Override public void onDestroy() { super.onDestroy(); //退出程序时关闭SQLiteDatabase if (db != null && db.isOpen()) { db.close(); } }}
activity_main布局文件的代码:
<!--?xml version="1.0" encoding="utf-8"?--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/parent" android:orientation="vertical"> <EditText android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="2"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="insert"/> <ListView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="fill_parent"/></LinearLayout>
ListView的子项目的布局文件::
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment"> <TextView android:id="@+id/my_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/my_content" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
0 0
- android 数据库存储+listView+SimpleCursorAdapter 范例demo(亲测)
- Android + sqlite simplecursorAdapter Demo
- android ListView,SimpleCursorAdapter
- Android入门:ListView(SimpleCursorAdapter实现)
- Android---UI篇---ListView之SimpleCursorAdapter(列表)---3
- Android新手入门2016(9)--ListView之SimpleAdapter和SimpleCursorAdapter
- listview利用SimpleCursorAdapter显示sqlit数据库列表
- Android SQLiteDatabase数据库, SimpleCursorAdapter, Cursor
- Android列表视图(ListView--SimpleCursorAdapter)学习
- android——ListView(使用simpleCursorAdapter)
- 【android】ListView+SimpleCursorAdapter+checkbox实现批量删除
- android]ListView+SimpleCursorAdapter+checkbox实现批量删除
- android listview 用simplecursoradapter适配器数据刷新
- Android之ListView使用方法-SimpleAdapter-SimpleCursorAdapter
- ListView(2)SimpleAdapter与SimpleCursorAdapter详解
- listview利用SimpleCursorAdapter显示sqlit数据库中的图片
- ListView显示数据库的数据-SimpleCursorAdapter的使用
- [Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAdapter 与ListView的用法
- Android 插件化原理解析(5):Activity 生命周期管理(上)
- Bug系列----HS key测试发生误报
- Linux+树莓派3+Python开发总结——python3+pyqt5+matplotlib环境搭建
- JZ2440TFTP下载
- TestNg依赖 dependsOnGroups
- android 数据库存储+listView+SimpleCursorAdapter 范例demo(亲测)
- js之this
- memcached教程
- Java Annotation实现原理
- Android 中的 MVP 模式(带实例)
- Java语法
- 相册图片
- React Native android 按钮的波纹效果
- Android APK反编译