我有一张包含以下结构的表格。
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。
我已经使用了Loadermanager。
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。
@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; } }
基本上我将图像以byte []形式保存在表格中,并且我必须将数据附加到视图中,但它会给我一个像这样的例外。
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)
任何人都可以告诉我为什么我得到这个例外,我该如何解决这个问题呢?
谢谢!
答案 0 :(得分:1)
我找到了解决问题的方法。正如@pskink在上面的评论中所建议的,我使用了ViewBinder
。我创建了一个像这样的ViewBinder。
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。
simpleCursorAdapter.setViewBinder( new MyViewBinder());
它对我有用。这是输出。