如何在 android studio 的列表中显示 sqlite 实体
how can show sqlite entity on a list in android studio
我是 android 的新人。
我只是编写了一个应用程序,它从 EditText 中提供一个字符串并将其保存在 SQLite 中。
知道我想在 ListView 中查看数据库的内容。
但是我不知道你怎么能帮我写一个方法来完成这项工作。
那是我的 DBManager class
package com.sara.app.savetextapp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBManager extends SQLiteOpenHelper {
public static final String DB_NAME = "Data";
public static final String TABLE_NAME = "test";
public static final String COLUMN_NAME= "text";
private static final String COLUMN_TEXT_ID = "_id";
public DBManager(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_TEXT_ID + " integer primary key autoincrement, "
+ COLUMN_NAME + " varchar(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS");
onCreate(db);
}
public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select text from Data", null);
}
}
应该是 "select text from test" 因为你的 table 名字叫 test。获得游标后,遍历它以读取数据。How to retrieve data from cursor class showing data in ListView use SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html as suggested in comment. To improve code, instead of harcoding and using raw query try using projections - http://developer.android.com/training/basics/data-storage/databases.html
获取您要显示的列的字符串(将产品替换为您的列)。
public List<String> getAllContacts() {
MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
List<String> contactList = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM products WHERE 1";
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Product contact = new Product();
String c;
c=(cursor.getString(1));
// Adding contact name to list
contactList.add(c);
} while (cursor.moveToNext());
}// return contact list
cursor.close();
db.close();
return contactList;
}
或者您可以使用 List 而不是 List(在我的例子中它将是 Product):
public List<Product> getAllContacts() {
MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
List<Product> contactList = new ArrayList<Product>();
// Select All Query
String selectQuery = "SELECT * FROM products WHERE 1";
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Product contact = new Product();
String c;
contact = new Product((cursor.getString(1)),cursor.getString(2) and so on)
//From here you just fill your product
contactList.add(contact);
} while (cursor.moveToNext());
}// return contact list
cursor.close();
db.close();
return contactList;
}
如果您正在编写笔记应用程序之类的东西并使用 SQLite 来存储它们,我在我的博客上就此主题写了一些相当冗长的文章 tutorials (Code | App) 可能会对您有所帮助.这是它的要点:
我建议您将 getDetails()
方法中的 rawQuery
替换为 query()
。这是因为 SQL 语法是无情的,而 query()
方法会为您进行微调。
public Cursor getDetails() {
return db.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
//This will return all data in your table.
}
仅供参考,最好在 UI 线程之外进行查询,例如使用 AsyncTask,否则它们可能会导致应用程序冻结,甚至如果您的 table 特别大则崩溃.
关于将此数据分配给 ListView
,最直接的方法是使用 SimpleCursorAdapter
:
//Where "data" is the Cursor return by getDetails()
ListAdapter listAdapter = new SimpleCursorAdapter(this,
R.layout.view_notes_row,
data,
new String[] {DBManager.COLUMN_NAME},
new int[] {R.id.text_view},
0);
为您的行定义一个 layout
,并确保您的整体布局中有一个 ListView
:
布局 - list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view" />
</FrameLayout>
行 - view_notes_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text_view" />
</LinearLayout>
在你的 Activitiy
里面:
setContentView(R.layout.list_layout);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(listAdapter);
我是 android 的新人。 我只是编写了一个应用程序,它从 EditText 中提供一个字符串并将其保存在 SQLite 中。 知道我想在 ListView 中查看数据库的内容。 但是我不知道你怎么能帮我写一个方法来完成这项工作。
那是我的 DBManager class
package com.sara.app.savetextapp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBManager extends SQLiteOpenHelper {
public static final String DB_NAME = "Data";
public static final String TABLE_NAME = "test";
public static final String COLUMN_NAME= "text";
private static final String COLUMN_TEXT_ID = "_id";
public DBManager(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_TEXT_ID + " integer primary key autoincrement, "
+ COLUMN_NAME + " varchar(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS");
onCreate(db);
}
public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select text from Data", null);
}
}
应该是 "select text from test" 因为你的 table 名字叫 test。获得游标后,遍历它以读取数据。How to retrieve data from cursor class showing data in ListView use SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html as suggested in comment. To improve code, instead of harcoding and using raw query try using projections - http://developer.android.com/training/basics/data-storage/databases.html
获取您要显示的列的字符串(将产品替换为您的列)。
public List<String> getAllContacts() {
MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
List<String> contactList = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM products WHERE 1";
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Product contact = new Product();
String c;
c=(cursor.getString(1));
// Adding contact name to list
contactList.add(c);
} while (cursor.moveToNext());
}// return contact list
cursor.close();
db.close();
return contactList;
}
或者您可以使用 List 而不是 List(在我的例子中它将是 Product):
public List<Product> getAllContacts() {
MyDBHandler helper = new MyDBHandler(getActivity().getApplicationContext(),null,null,1);
List<Product> contactList = new ArrayList<Product>();
// Select All Query
String selectQuery = "SELECT * FROM products WHERE 1";
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Product contact = new Product();
String c;
contact = new Product((cursor.getString(1)),cursor.getString(2) and so on)
//From here you just fill your product
contactList.add(contact);
} while (cursor.moveToNext());
}// return contact list
cursor.close();
db.close();
return contactList;
}
如果您正在编写笔记应用程序之类的东西并使用 SQLite 来存储它们,我在我的博客上就此主题写了一些相当冗长的文章 tutorials (Code | App) 可能会对您有所帮助.这是它的要点:
我建议您将 getDetails()
方法中的 rawQuery
替换为 query()
。这是因为 SQL 语法是无情的,而 query()
方法会为您进行微调。
public Cursor getDetails() {
return db.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
//This will return all data in your table.
}
仅供参考,最好在 UI 线程之外进行查询,例如使用 AsyncTask,否则它们可能会导致应用程序冻结,甚至如果您的 table 特别大则崩溃.
关于将此数据分配给 ListView
,最直接的方法是使用 SimpleCursorAdapter
:
//Where "data" is the Cursor return by getDetails()
ListAdapter listAdapter = new SimpleCursorAdapter(this,
R.layout.view_notes_row,
data,
new String[] {DBManager.COLUMN_NAME},
new int[] {R.id.text_view},
0);
为您的行定义一个 layout
,并确保您的整体布局中有一个 ListView
:
布局 - list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view" />
</FrameLayout>
行 - view_notes_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text_view" />
</LinearLayout>
在你的 Activitiy
里面:
setContentView(R.layout.list_layout);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(listAdapter);