问题描述
我有一个具有以下结构的表.
I have a table with following structure.
public static final String TABLE_NAME = "contact";
public static final String ID = "_id";
public static final String DEVICE_ID = "device_id";
public static final String NAME = "name";
public static final String CONTACT = "contact";
public static final String IMAGE = "image";
private static final int VERSION = 1;
private static final String CREATE_TABLE_QUERY = "CREATE TABLE "
+ TABLE_NAME + "( " + ID + " INTEGER PRIMARY KEY , " + DEVICE_ID
+ " TEXT NOT NULL , " + NAME + " TEXT NOT NULL , " + CONTACT
+ " TEXT NOT NULL ,"
+ IMAGE + " BLOB " +
" ) ";
我已将图像以BLOB形式存储在联系人表中.现在,我必须从表中检索device_id,name,image并将结果附加到view上.
I have stored images in contact table in BLOB form . Now i have to retrieve device_id,name , image from table and attach the result to view .
我为此使用了Loadermanager.
I have used Loadermanager for this .
public class FriendList extends Activity implements OnItemClickListener,
LoaderManager.LoaderCallbacks {
ListView friendList;
SimpleCursorAdapter simpleCursorAdapter;
CursorLoader cursorLoader;
LoaderManager.LoaderCallbacks loaderCallback;
static final int CONTACT_LOADER_ID = 77;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friend_list);
friendList = (ListView) findViewById(R.id.lvFriendList);
String[] uiBindFrom = new String[] { MyContacts.NAME,
MyContacts.DEVICE_ID ,MyContacts.IMAGE};
int[] idBindTo = { R.id.tvFriendName, R.id.tvDeviceID ,R.id.ivContactProfile};
loaderCallback = this;
simpleCursorAdapter = new SimpleCursorAdapter(FriendList.this,
R.layout.custom_friend_list, null, uiBindFrom, idBindTo, 0);
friendList.setAdapter(simpleCursorAdapter);
friendList.setOnItemClickListener(this);
getLoaderManager().initLoader(CONTACT_LOADER_ID, new Bundle(),
loaderCallback);
}
R.id.ivContactProfile 是ImageView的ID.
Here R.id.ivContactProfile is id of ImageView .
@Override
public Loader onCreateLoader(int id, Bundle args) {
String[] projectionFields = new String[] { MyContacts.ID,
MyContacts.NAME, MyContacts.DEVICE_ID,MyContacts.IMAGE };
cursorLoader = new CursorLoader(FriendList.this,
MyContacts.CONTENT_URI, projectionFields, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader loader, Cursor cursor) {
switch (loader.getId()) {
case CONTACT_LOADER_ID:
simpleCursorAdapter.swapCursor(cursor);
break;
}
}
基本上,我以字节[]的形式将图像保存在表中,并且我必须将数据附加到视图中,但这会向我返回这样的异常.
Basically i have images save in table in byte[] form , and i have to attach data to the view , but it's returning me an exception like this .
05-21 19:30:05.169: E/AndroidRuntime(22609): android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string
这是完整的日志.
05-21 19:30:05.169: E/AndroidRuntime(22609): FATAL EXCEPTION: main
05-21 19:30:05.169: E/AndroidRuntime(22609): android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWindow.nativeGetString(Native Method)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWindow.getString(CursorWindow.java:450)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:135)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.AbsListView.obtainView(AbsListView.java:2452)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.ListView.onMeasure(ListView.java:1161)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.os.Looper.loop(Looper.java:137)
05-21 19:30:05.169: E/AndroidRuntime(22609): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-21 19:30:05.169: E/AndroidRuntime(22609): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 19:30:05.169: E/AndroidRuntime(22609): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-21 19:30:05.169: E/AndroidRuntime(22609): at dalvik.system.NativeStart.main(Native Method)
谁能告诉我为什么会收到此异常以及如何解决此问题?
Can anyone tell me why am i getting this exception and how can i resolve this problem ?
谢谢!
推荐答案
我已经找到解决问题的方法.正如@pskink在上面的评论中建议的那样,我使用了ViewBinder
.我创建了这样的ViewBinder.
I have found solution of my problem . As suggested by @pskink in his comment above , i used ViewBinder
. I created a ViewBinder like this .
public class MyViewBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewID = view.getId();
switch(viewID){
case R.id.tvFriendName :
TextView friendName = (TextView) view;
String friend_name;
friend_name = cursor.getString(cursor.getColumnIndex(MyContacts.NAME));
friendName.setText(friend_name);
break;
case R.id.tvDeviceID :
TextView deviceID = (TextView) view;
String device_id = cursor.getString(cursor.getColumnIndex(MyContacts.DEVICE_ID));
deviceID.setText(device_id);
break;
case R.id.ivContactProfile :
ImageView contactProfile = (ImageView) view;
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex(MyContacts.IMAGE));
if(imageBytes != null ){
// Pic image from database
contactProfile.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}else {
// If image not found in database , assign a default image
contactProfile.setBackgroundResource(R.drawable.bubble_a);
}
break;
}
return true;
}
}
然后我为我的SimpleCursorAdapter设置了ViewBinder.
And i set ViewBinder for my SimpleCursorAdapter .
simpleCursorAdapter.setViewBinder( new MyViewBinder());
它对我来说正常工作.这是输出.
It worked for me properly . Here is the output.
这篇关于无法在Android中使用Loadermanager将BLOB转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!