我将项目及其详细信息存储在 sqlite 中,并显示项目的图像以及我正在使用的项目名称
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);
}
});
}
}
我的 Items 适配器类是::
private class ItemsAdapter extends BaseAdapter implements ListAdapter
{
String[] items;
public ItemsAdapter(Context context,int textViewResourceId,String[] items)
{
this.items = items;
}
public View getView( int POSITION, View convertView,ViewGroup parent)
{
TextView cap = null ;
View view = convertView;
ImageView img = null;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.item, null);
}
img = (ImageView) view.findViewById(R.id.image);
Log.i("iiiiii","iiiii");
cap = (TextView) view.findViewById(R.id.caption);
System.err.println("Position of the book is "+mapName.get(POSITION));
bookTitle=mapName.get(POSITION);
System.err.println("title of the book is "+bookTitle);
cap.setText(mapName.get(POSITION));
img.setImageBitmap(BitmapFactory.decodeByteArray(map.get(POSITION), 0, map.get(POSITION).length));
return view;
}
public int getCount()
{
return items.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
}
显示时的错误是::
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)
代码的结果是这样的
最佳答案
在您的第一个代码块中,您需要在设置 GridView 之前关闭 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);
}
});
}
在进入 while 循环之前清除所有列表..
假设您正在使用 ArrayList 我已经更新代码以在进入 while 循环之前清除列表数据..
关于android - 显示项目名称以及存储在 sqlite for android 中的图像时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13985162/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i