显示项目名称以及存储在sqlite for android中的图像时出现问题
我将项目及其详细信息存储在sqlite中,并显示项目的图像以及我正在使用的项目名称显示项目名称以及存储在sqlite for android中的图像时出现问题,android,sqlite,Android,Sqlite,我将项目及其详细信息存储在sqlite中,并显示项目的图像以及我正在使用的项目名称 private void getDataAndPopulate() { Cursor cursor = getEvents("bcuk_book"); while (cursor.moveToNext()) { String temp_id = cursor.getString(0);
private void getDataAndPopulate()
{
Cursor cursor = getEvents("bcuk_book");
while (cursor.moveToNext())
{
String temp_id = cursor.getString(0);
final String temp_name = cursor.getString(1);
String temp_author=cursor.getString(2);
String temp_desc=cursor.getString(3);
byte[] temp_image = cursor.getBlob(4);
String temp_rating = cursor.getString(5);
String temp_price = cursor.getString(6);
String temp_update=cursor.getString(7);
mapId.add(temp_id);
mapName.add(temp_name);
mapAuthor.add(temp_author);
mapDesc.add(temp_desc);
mapRating.add(temp_rating);
mapPrice.add(temp_price);
map.add(temp_image);
mapUpdated.add(temp_update);
Log.e("temp_id from the sqlite", temp_id);
Log.i(temp_name, temp_name);
Log.i(temp_desc, temp_desc);
Log.i(temp_rating, temp_rating);
Log.e(temp_update,temp_update);
String[] captionArray = (String[]) mapName.toArray(new String[mapName.size()]);
itemsAdapter = new ItemsAdapter(
Bookicon.this, R.layout.item,captionArray);
gridView=(GridView)findViewById(R.id.list);
gridView.setAdapter(itemsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
}
显示时的错误为::
12-21 10:12:28.792: W/dalvikvm(741): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-21 10:12:28.842: E/AndroidRuntime(741): FATAL EXCEPTION: main
12-21 10:12:28.842: E/AndroidRuntime(741): java.lang.IndexOutOfBoundsException: Invalid index 56, size is 56
12-21 10:12:28.842: E/AndroidRuntime(741): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-21 10:12:28.842: E/AndroidRuntime(741): at java.util.ArrayList.get(ArrayList.java:304)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.books.bcukbook.Bookicon$ItemsAdapter.getView(Bookicon.java:254)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.AbsListView.obtainView(AbsListView.java:2012)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.makeAndAddView(GridView.java:1323)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.makeRow(GridView.java:328)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.moveSelection(GridView.java:885)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.layoutChildren(GridView.java:1230)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.setSelectionInt(GridView.java:1476)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.arrowScroll(GridView.java:1729)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.commonKey(GridView.java:1543)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.widget.GridView.onKeyDown(GridView.java:1494)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.View.dispatchKeyEvent(View.java:5500)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1242)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.os.Looper.loop(Looper.java:137)
12-21 10:12:28.842: E/AndroidRuntime(741): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-21 10:12:28.842: E/AndroidRuntime(741): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:12:28.842: E/AndroidRuntime(741): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 10:12:28.842: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 10:12:28.842: E/AndroidRuntime(741): at dalvik.system.NativeStart.main(Native Method)
代码的结果显示如下
在您的第一个代码块中,您需要在设置
网格视图之前关闭while
循环,如下代码所示:
private void getDataAndPopulate()
{
//assuing that they are not null at this point..
mapId.clear();
mapName.clear();
mapAuthor.clear();
mapDesc.clear();
mapRating.clear();
mapPrice.clear();
map.clear();
mapUpdated.clear();
Cursor cursor = getEvents("bcuk_book");
while (cursor.moveToNext())
{
String temp_id = cursor.getString(0);
final String temp_name = cursor.getString(1);
String temp_author=cursor.getString(2);
String temp_desc=cursor.getString(3);
byte[] temp_image = cursor.getBlob(4);
String temp_rating = cursor.getString(5);
String temp_price = cursor.getString(6);
String temp_update=cursor.getString(7);
mapId.add(temp_id);
mapName.add(temp_name);
mapAuthor.add(temp_author);
mapDesc.add(temp_desc);
mapRating.add(temp_rating);
mapPrice.add(temp_price);
map.add(temp_image);
mapUpdated.add(temp_update);
Log.e("temp_id from the sqlite", temp_id);
Log.i(temp_name, temp_name);
Log.i(temp_desc, temp_desc);
Log.i(temp_rating, temp_rating);
Log.e(temp_update,temp_update);
}//this would close the while loop
String[] captionArray = (String[]) mapName.toArray(new String[mapName.size()]);
itemsAdapter = new ItemsAdapter(
Bookicon.this, R.layout.item,captionArray);
gridView=(GridView)findViewById(R.id.list);
gridView.setAdapter(itemsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
private void getDataAndPopulate()
{
//假设此时它们不是空的。。
mapId.clear();
mapName.clear();
mapAuthor.clear();
mapDesc.clear();
mapRating.clear();
mapPrice.clear();
map.clear();
mapdated.clear();
游标=获取事件(“bcuk_书”);
while(cursor.moveToNext())
{
String temp_id=cursor.getString(0);
最终字符串temp_name=cursor.getString(1);
String temp_author=cursor.getString(2);
String temp_desc=cursor.getString(3);
字节[]temp_image=cursor.getBlob(4);
String temp_rating=cursor.getString(5);
String temp_price=cursor.getString(6);
String temp_update=cursor.getString(7);
添加映射id(临时id);
mapName.add(临时名称);
添加(临时作者);
地图描述添加(临时描述);
添加(温度等级);
mapPrice.add(临时价格);
添加地图(临时图像);
地图更新。添加(临时更新);
Log.e(“来自sqlite的临时id”,临时id);
Log.i(临时名称、临时名称);
日志i(临时说明、临时说明);
日志i(温度等级、温度等级);
Log.e(临时更新,临时更新);
}//这将关闭while循环
字符串[]captionArray=(字符串[])mapName.toArray(新字符串[mapName.size()]);
itemsAdapter=新的itemsAdapter(
Bookicon.this,R.layout.item,captionArray);
gridView=(gridView)findViewById(R.id.list);
setAdapter(itemsAdapter);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
Intent i=新的Intent(getApplicationContext(),DetailsActivity.class);
i、 putExtra(“id”,位置);
星触觉(i);
}
});
}
在进入while
循环之前,请清除所有列表
假设您使用的是ArrayList
,则在进入while循环之前,我必须更新代码以清除列表数据
private void getDataAndPopulate()
{
//assuing that they are not null at this point..
mapId.clear();
mapName.clear();
mapAuthor.clear();
mapDesc.clear();
mapRating.clear();
mapPrice.clear();
map.clear();
mapUpdated.clear();
Cursor cursor = getEvents("bcuk_book");
while (cursor.moveToNext())
{
String temp_id = cursor.getString(0);
final String temp_name = cursor.getString(1);
String temp_author=cursor.getString(2);
String temp_desc=cursor.getString(3);
byte[] temp_image = cursor.getBlob(4);
String temp_rating = cursor.getString(5);
String temp_price = cursor.getString(6);
String temp_update=cursor.getString(7);
mapId.add(temp_id);
mapName.add(temp_name);
mapAuthor.add(temp_author);
mapDesc.add(temp_desc);
mapRating.add(temp_rating);
mapPrice.add(temp_price);
map.add(temp_image);
mapUpdated.add(temp_update);
Log.e("temp_id from the sqlite", temp_id);
Log.i(temp_name, temp_name);
Log.i(temp_desc, temp_desc);
Log.i(temp_rating, temp_rating);
Log.e(temp_update,temp_update);
}//this would close the while loop
String[] captionArray = (String[]) mapName.toArray(new String[mapName.size()]);
itemsAdapter = new ItemsAdapter(
Bookicon.this, R.layout.item,captionArray);
gridView=(GridView)findViewById(R.id.list);
gridView.setAdapter(itemsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}