如何从我的数据库中将图像添加到 ListView 中?
How to add image in a ListView from my database?
你能告诉我或给我一些我需要添加到这些代码中以从我的数据库中检索图像的代码吗?我是 android 编程的初学者,我们将不胜感激。
DBHelper Class:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.imagelist/databases/";
private static final String DATABASE_NAME = "DbImage.db";
private static final int SCHEMA_VERSION = 1;
private static final String TABLE_NAME = "image";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_IMAGE = "img";
private static final String COLUMN_NAME = "name";
public SQLiteDatabase dbSqlite;
private final Context myContext;
public DBHelper(Context context){
super(context, DATABASE_NAME, null, SCHEMA_VERSION );
this.myContext= context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
public void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try{
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null){
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource(){
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try{
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte [] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e){
throw new Error("Problem copying database from resource file.");
}
}
public void openDatabase () throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close () {
if (dbSqlite !=null){
dbSqlite.close();
}
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder ();
queryBuilder.setTables(TABLE_NAME);
String [] asColumnsToReturn = new String [] { COLUMN_ID, COLUMN_IMAGE, COLUMN_NAME};
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null, null, null, null, "_id ASC");
return mCursor;
}
public String getName (Cursor c){
return(c.getString(2));
}
}
主要Class:
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
private DBHelper dblistHelper = null;
private Cursor ourCursor = null;
private recipeAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.listView);
dblistHelper = new DBHelper(this);
dblistHelper.createDatabase();
dblistHelper.openDatabase();
ourCursor=dblistHelper.getCursor();
startManagingCursor(ourCursor);
adapter = new recipeAdapter(ourCursor);
myListView.setAdapter(adapter);
}
catch (Exception e)
{
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
class recipeAdapter extends CursorAdapter {
recipeAdapter(Cursor c){
super(Main.this, c);
}
@Override
public void bindView (View row, Context ctxt, Cursor c)
{
Holder holder = (Holder)row.getTag();
holder.populateFrom(c, dblistHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
Holder holder = new Holder(row);
row.setTag(holder);
return(row);
}
}
static class Holder {
private TextView name=null;
private ImageView image=null;
Holder(View row){
name=(TextView)row.findViewById(R.id.textView1);
image=(ImageView)row.findViewById(R.id.imageView1);
}
void populateFrom(Cursor c, DBHelper r){
name.setText(r.getName(c));
image.setImageBitmap(bm);
}
}
}
为列表视图中的元素创建一个 XML 文件。沿着这些线的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@id/icon"
android:textColor="#FF0000" />
</RelativeLayout>
创建某种类型的适配器 class 从您的数据库中提取内容。然后将列表视图中的任何字段设置为数据库中的适当数据,如下所示:
public class CustomAdapter {
public CustomAdapter(Context context){
// connect to you database
}
public View getItemView(/*pass your db object*/, View v, ViewGroup parent){
if (v == null) {
v = View.inflate(getContext(), R.layout.row, null);
}
super.getItemView(object, v, parent);
TextView textView = (TextView) v.findViewById(R.id.text1);
textView.setText(/*dataObject.getdata*/);
// Do this as much as necessary
}
public void loadObject(){
//Get all of your data, set each row in list view by calling getView
}
然后在您的 activity 调用中创建您对象的实例,获取列表视图的实例,设置列表视图的适配器并调用 loadobjects
private CustomAdapter customAdapter;
//...
customAdapter = new CustomAdapter(this);
ListView listView = (ListView) findViewById(R.id.main_list_view);
listView.setAdapter(customAdapter);
customAdapter.loadObjects();
这就是我的做法,也是基于使用 Parse 数据库发现的信息:https://parse.com/tutorials/parse-query-adapter
这可能对您有所帮助,因为它与 SQL 相关:http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
基本思想是为设置数据的列表视图创建自己的适配器。然后将列表视图适配器设置为该适配器。
你能告诉我或给我一些我需要添加到这些代码中以从我的数据库中检索图像的代码吗?我是 android 编程的初学者,我们将不胜感激。
DBHelper Class:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.imagelist/databases/";
private static final String DATABASE_NAME = "DbImage.db";
private static final int SCHEMA_VERSION = 1;
private static final String TABLE_NAME = "image";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_IMAGE = "img";
private static final String COLUMN_NAME = "name";
public SQLiteDatabase dbSqlite;
private final Context myContext;
public DBHelper(Context context){
super(context, DATABASE_NAME, null, SCHEMA_VERSION );
this.myContext= context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
public void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try{
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null){
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource(){
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try{
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte [] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e){
throw new Error("Problem copying database from resource file.");
}
}
public void openDatabase () throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close () {
if (dbSqlite !=null){
dbSqlite.close();
}
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder ();
queryBuilder.setTables(TABLE_NAME);
String [] asColumnsToReturn = new String [] { COLUMN_ID, COLUMN_IMAGE, COLUMN_NAME};
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null, null, null, null, "_id ASC");
return mCursor;
}
public String getName (Cursor c){
return(c.getString(2));
}
}
主要Class:
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
private DBHelper dblistHelper = null;
private Cursor ourCursor = null;
private recipeAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.listView);
dblistHelper = new DBHelper(this);
dblistHelper.createDatabase();
dblistHelper.openDatabase();
ourCursor=dblistHelper.getCursor();
startManagingCursor(ourCursor);
adapter = new recipeAdapter(ourCursor);
myListView.setAdapter(adapter);
}
catch (Exception e)
{
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
class recipeAdapter extends CursorAdapter {
recipeAdapter(Cursor c){
super(Main.this, c);
}
@Override
public void bindView (View row, Context ctxt, Cursor c)
{
Holder holder = (Holder)row.getTag();
holder.populateFrom(c, dblistHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
Holder holder = new Holder(row);
row.setTag(holder);
return(row);
}
}
static class Holder {
private TextView name=null;
private ImageView image=null;
Holder(View row){
name=(TextView)row.findViewById(R.id.textView1);
image=(ImageView)row.findViewById(R.id.imageView1);
}
void populateFrom(Cursor c, DBHelper r){
name.setText(r.getName(c));
image.setImageBitmap(bm);
}
}
}
为列表视图中的元素创建一个 XML 文件。沿着这些线的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/icon"
android:layout_toRightOf="@id/icon"
android:textColor="#FF0000" />
</RelativeLayout>
创建某种类型的适配器 class 从您的数据库中提取内容。然后将列表视图中的任何字段设置为数据库中的适当数据,如下所示:
public class CustomAdapter {
public CustomAdapter(Context context){
// connect to you database
}
public View getItemView(/*pass your db object*/, View v, ViewGroup parent){
if (v == null) {
v = View.inflate(getContext(), R.layout.row, null);
}
super.getItemView(object, v, parent);
TextView textView = (TextView) v.findViewById(R.id.text1);
textView.setText(/*dataObject.getdata*/);
// Do this as much as necessary
}
public void loadObject(){
//Get all of your data, set each row in list view by calling getView
}
然后在您的 activity 调用中创建您对象的实例,获取列表视图的实例,设置列表视图的适配器并调用 loadobjects
private CustomAdapter customAdapter;
//...
customAdapter = new CustomAdapter(this);
ListView listView = (ListView) findViewById(R.id.main_list_view);
listView.setAdapter(customAdapter);
customAdapter.loadObjects();
这就是我的做法,也是基于使用 Parse 数据库发现的信息:https://parse.com/tutorials/parse-query-adapter
这可能对您有所帮助,因为它与 SQL 相关:http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
基本思想是为设置数据的列表视图创建自己的适配器。然后将列表视图适配器设置为该适配器。