2010-10-19 45 views
1

我刚刚开始与Android和我尝试​​我的第一个测试应用程序,但我有点卡住了。Android从SQLite绑定到图库

我有一个SQLite数据库,并且用户正在选择一张图片和一个对此的引用,并且一个简短的描述被存储在数据库中。 这一切都工作正常,现在我想要将图像从数据库绑定到一个画廊,但我有点困惑,我如何做到这一点。

我的布局XML如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <Gallery 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/Weeksgallery"  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

<Button android:id="@+id/ok"  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text1"  
     android:layout_marginLeft="10dip"  
     android:text="Take a Picture" 
     android:onClick="TakePicture" /> 
</LinearLayout> 

我想用一个按钮下方

我在走廊上创建功能我有以下几点:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mDbHelper = new DbAdapter(this); 
    mDbHelper.open(); 
    Cursor cur = mDbHelper.fetchDaysImages(); 

    //array of fields to display 
    String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF}; 

    int[] to = new int[] { R.id.Weeksgallery }; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter images = new SimpleCursorAdapter(this, 
    R.layout.main, cur, from, to); 
    setListAdapter(images); 

    mDbHelper.close(); 
} 

我现在用的是记事本教程为例,我的课程扩展了ListActivity。

当我运行它,我得到的错误

您的内容必须有一个ListView其id属性为“android.R.id.list”

我假设我使用错误的绑定类型,但我似乎无法找到任何告诉我如何从sqlite数据库绑定到图库的地方。 我也想在图库中添加对rowId的引用,所以当图片被点击时它会打开相关页面(所以它可以显示消息)

任何人都可以帮忙吗?

感谢

贝克斯

+0

这些CAPS真的让我觉得更倾向于帮助.... – 2010-10-19 09:39:34

+0

对不起,他们在那里使它突出,所以有人会看到它,也许应该用粗体代替..现在已经删除它们摆弄,并得到文本格式正确 – Bex 2010-10-19 09:40:34

回答

0

你的活动继承ListActivity,对不对?当你使用ListActivity的时候,你必须确保在Activity的布局中有一个ListView的id是“android.R.id.list”,这就是日志告诉你的。

如果Activity中没有ListView,则不要使用ListActivity。如果您要将适配器设置为图库,请使用图库的setAdapter方法。

+0

我已经更新了类,现在我得到了一个不同的错误“android.widget.Gallery不是可以由此SimpleCursorAdapter绑定的视图”错误是否意味着我无法使用simplecursoradapter绑定它,还是我在simplecursoradapter中做错了什么?我也需要以某种方式使用hello图库教程中使用的imageadapter类吗? – Bex 2010-10-19 12:39:18

+0

我不认为你可以在这里使用SimpleCursorAdapter。因为SimpleCursorAdapter是一个将游标的列映射到TextViews或ImageViews的适配器。你可以实现你自己的适配器来存档这个。试试吧,不会太难。 – Tony 2010-10-19 14:00:37

+0

嗯...我会给它之前,你有什么指针,因为我对Android(和Java)非常陌生,所以我仍然在与最基本的东西苦苦挣扎!我原以为会有一个教程在某处,因为我曾想过,我正在尝试的是很常见的。 – Bex 2010-10-19 14:37:01