使用错误的数字列创建数据库

时间:2011-12-29 03:30:41

标签: android database

我正在尝试创建一个包含4列(包括_id)的表,无论我做什么,它都会继续创建一个只有3列的表(包括_id)。

我的表定义如下:

private static final String DATABASE_CREATE =
        "create table collections (_id integer primary key autoincrement, "
        + "name text not null, description text not null, image integer not null);";

我的onCreate:

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

我如何访问数据:

public View getView(int position, View convertView, ViewGroup parent) {

        cursor.moveToPosition(position);

        LinearLayout ll = new LinearLayout(mContext); 
        ll.setOrientation(LinearLayout.VERTICAL);

        ImageView i = new ImageView(mContext);


        i.setImageResource(cursor.getInt(3));  //access to the image column
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new Gallery.LayoutParams(150, 150));

        // The preferred Gallery item background
        i.setBackgroundResource(mGalleryItemBackground);


        ll.addView(i); 

        TextView tv = new TextView(ll.getContext());

        tv.setTag(mText[position]);
        tv.setText(cursor.getString(1)); //access to the name column
        tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
        ll.addView(tv); 

        return ll;
    }

无论我尝试什么,它都会不断将列图像从表格中删除......我会感激任何帮助。

3 个答案:

答案 0 :(得分:1)

这是一个非常愚蠢的事情,我真的很抱歉浪费任何人的时间。我非常感谢大家的帮助。

我没有正确获取数据。所以将来如果有人重复我的错误,请先检查你的获取方法。

答案 1 :(得分:0)

您是否尝试在表格和列名称周围添加反引号?这将为你做一些逃避:

    private static final String DATABASE_CREATE =
    "create table `collections` (`_id` integer primary key autoincrement, "
    + "`name` text not null, `description` text not null, `image` integer not null);";

答案 2 :(得分:0)

未经证实的猜测:由于_id可能会被忽略或被识别为默认_ID列,因此可能无法计算。

尝试动态获取列号:

public View getView(int position, View convertView, ViewGroup parent) {

    cursor.moveToPosition(position);

    LinearLayout ll = new LinearLayout(mContext); 
    ll.setOrientation(LinearLayout.VERTICAL);

    ImageView i = new ImageView(mContext);


    i.setImageResource(cursor.getInt(cursor.getColumnIndex("image")));  //access to the image column
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setLayoutParams(new Gallery.LayoutParams(150, 150));

    // The preferred Gallery item background
    i.setBackgroundResource(mGalleryItemBackground);


    ll.addView(i); 

    TextView tv = new TextView(ll.getContext());

    tv.setTag(mText[position]);
    tv.setText(cursor.getString(cursor.getColumnIndex("name"))); //access to the name column
    tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
    ll.addView(tv); 

    return ll;
}

其余的(非主题):

  1. 您应该阅读有关在适配器中回收视图的信息
  2. 您也可以尝试使用SimpleCursorAdapter类