任何人都请帮助我。我正在尝试创建一个字典应用程序,其中我想用每个单词保存单词,它们的含义和图片。我试图在sqlite数据库中保存所有的单词,图像和含义。问题是,当我点击列表视图中的任何单词时,它会显示单词和定义,但图像字段为空白。这是我创建数据库的类。
public class DictionaryDataBase extends SQLiteOpenHelper {
byte[][] img = {
DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
DbBitmapUtility
.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) };
public DictionaryDataBase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE Dict_Table (id INTEGER PRIMARY KEY, word TEXT NOT NULL, definition TEXT NOT NULL, image BLOB NOT NULL);");
try {
new DictionaryDB(context).addEntry(words, def, db, img);
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
这是添加条目方法:
public void addEntry(String[] word, String[] def, SQLiteDatabase db, byte[][] img) throws SQLiteException {
for (int i = 0; i < word.length; i++) {
db.execSQL("INSERT INTO Dict_Table VALUES (" + i + 1 + ", '" + word[i] + "', '" + def[i] + "', '" + img[i]
+ "');");
}
这是我用来获取图像的方法:
public byte[] Getimage() throws SQLException {
dbhelper = new DictionaryDataBase(context);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM Dict_Table WHERE word='" + MainActivity.item + "'", null);//MainActivity.item is the item which was clicked in the list view
res.moveToFirst();
byte[] img = res.getBlob(res.getColumnIndex(IMG_COL));
if (!res.isClosed()) {
res.close();
}
return img;
}
DbBitmapUtility.java:
public class DbBitmapUtility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
activity_meaning.xml(这是我的单词定义和图像的布局文件):
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shehryar.dictionary.Meaning"
android:background="#2196F3" >
<TextView
android:id="@+id/tvword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word"
android:textStyle="bold"
android:textSize="20dip"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/tvdefinition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvword"
android:layout_below="@+id/tvword"
android:layout_marginTop="14dp"
android:text="Definition"
android:textColor="@android:color/white"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvdefinition"
android:layout_below="@+id/tvdefinition"
android:layout_marginTop="30dp"
android:src="@drawable/abc_btn_radio_material" />
<TextView
android:id="@+id/tvcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_marginTop="40dp"
android:text="TextView" />
这是我的listview的onitemclick方法,它显示了单词列表。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
try {
item =(String) parent.getItemAtPosition(position);
String def = db.GetDefinition();
img=db.Getimage();
Bundle basket = new Bundle();
basket.putString("word", item);
basket.putString("def", def);
basket.putByteArray("img", img);
Intent a= new Intent(this,Meaning.class);
a.putExtras(basket);
startActivity(a);
//Toast.makeText(this, def, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
这是显示结果的意义活动:
Meaning.java:
package com.shehryar.dictionary;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class Meaning extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meaning);
String word,def;
byte[] img;
Bundle extras=getIntent().getExtras();
word=extras.getString("word");
def=extras.getString("def");
img=extras.getByteArray("img");
Bitmap image=DbBitmapUtility.getImage(img);
TextView tvword=(TextView) findViewById(R.id.tvword);
TextView tvdef=(TextView) findViewById(R.id.tvdefinition);
TextView tvcheck= (TextView) findViewById(R.id.tvcheck);
ImageView iv= (ImageView) findViewById(R.id.imageView1);
tvword.setText(word);
tvdef.setText(def);
iv.setImageBitmap(image);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.meaning, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如果您需要任何其他信息,我会提供给您,但在这种情况下请帮助我。它已经2-3天了,我一直被困在这里。
答案 0 :(得分:-1)
理想情况下,您不应该在SQLite中执行此操作。 SQLite是一个只有250KB大小的轻量级数据库。说到字典,SQLite将无法满足您的要求。 尝试将其保存在服务器或内部存储器上,并分别在DB中保存URL或路径。