2011-12-29 10 views
0

4列(_idを含む)でテーブルを作成しようとしていますが、3列(_idを含む)のテーブルを作成しても問題ありません。間違った番号の列を持つdbを作成します。

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; 
    } 

を、それはテーブルの外に列のイメージを残し続ける...私が得る助けに感謝します。

+0

'_image'という列に名前を付けてみましたか? –

+0

さて、私は同じ問題を抱えています。http://stackoverflow.com/questions/8656146/column-not-created-in-sqlite3-tableを参照してください。理由を見つけたら教えてください。私は自分のコードとカラム名を何度も書き直しましたが、何も動作していないようです。それは幽霊の列のようなものです。 –

+0

イメージの列名を別のものに変更して試してください。 – kosa

答えて

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);"; 
+0

この解決策を試してもうまくいかず、代わりにテーブルが存在しないという警告メッセージが表示されました。 – Luthien

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クラス
+0

動作しませんでした。この場合SimpleCursorAdapterを使用することはできません。これは、ギャラリーを使用してデータを表示しているためです。 – Luthien

+0

しかし、あなたのためにgetColumnIndex()が機能しましたか? – WarrenFaith

1

これは本当にばかなことでした。本当に申し訳ありません誰の時間を無駄にする。私は本当に皆の助けに感謝します。

データを正しく取得できませんでした。だから将来誰かが私の過ちを繰り返すならば、まずフェッチ方法をチェックしてください。

関連する問題