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 " +
" ) ";
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); }
@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; } }
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)
谁能告诉我为什么我会得到这个例外,我该如何解决这个问题?
谢谢!
我找到了解决我问题的办法。正如@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());
这对我很有效。下面是输出。
问题内容: 我已从API以2017-04-23T19:47:39 + 00:00的形式获取字符串,如何将其转换为格式为“ 2017-04-23T19:47:39-0000”的日期? 我尝试了以下操作,但它给出了ParseException 例外: 尝试使用TimeZone在SimpleDateFormat中给出的内容 仍然获得相同的解析异常 问题答案: 你不需要 您要求的转换很容易是多余的。您要输
问题内容: 我有一个字符串。我希望它成为“ Uri”,这样我就可以与 如何将Songchoice转换为Uri? 问题答案: 这是文档http://developer.android.com/reference/android/net/Uri.html#parse%28java.lang.String%29
我想将字符串转换为int。 例如: 如果是,则应将其转换为与相同的 为什么我需要这个。因为我要将unicode int转换为表情符号,所以我使用的是这段代码。 你能让我知道我该怎么做吗?
问题内容: 您如何将此String转换为gson.JsonArray? 这是我的代码: 这是将这个Collections字符串转换为JSonArray的方法吗? 问题答案: 要在JSON数组中包含字符串值,必须记住在Java程序中用反斜杠转义双引号。请参见下面的声明。 您在main()方法中的代码可以正常工作。以下只是对main()方法中的代码的微小修改。 最后,请记住在语句“ com.googl
我将XML作为字符串传递给一个方法,并再次将其转换为XML来完成我的工作。 其正常工作正常,但当出现特殊字符时,如<代码> 我的XML字符串: 我的代码是: 错误: “=”是意外标记。预期标记为“;”。第1行,位置150。 完全错误为: 系统Xml。XmlException未由用户代码处理HResult=-2146232000消息=“=”是意外令牌。预期标记为“;”。第1行,位置150。源=系统。
问题内容: 我正在编写一些代码,并且我需要它来捕获参数并将它们传递给 我(我想要它的默认行为,编写由空格分隔并后跟换行符的参数)。但是它需要而返回。 这是代码示例: 这将返回以下错误: 这是一个错误吗?不应该接受 任何 数组吗?顺便说一句,我也尝试这样做: 但出现以下错误: 有“解决方案”解决方法吗? 问题答案: 这不是错误。需要一个类型。这意味着它必须是值的切片,而不是“任何切片”。为了转换切片