2012-01-25 11 views
0

私は2つの列(_id、name)のテーブル(カテゴリ)を持っています。最初の列のスピナーをテーブルに挿入します

私はカラム名を示すスピナーを持っています。

今、私は列を挿入する必要があります: "_id"別のテーブルに...しかし、私はそれを行う方法がわかりません。

提案がありますか?


私は私が使用しているコードを貼り付けます。

public Cursor recuperaCategoria() 
    { 
    final UsuariosSQLiteHelper usdbh =new UsuariosSQLiteHelper(this, "DBLlistaCompra", null, 1); 
    final SQLiteDatabase db = usdbh.getWritableDatabase(); 
    String tableName = "Categorias"; 
    String[] columns = {"_id","Nombre"}; 

    return db.query(tableName, columns, null, null, null, null, null); 
    } 

public void recCatSpinner() { 
     final Spinner addCatSpinner = (Spinner) findViewById(R.id.spIdCategoria); 
     catCursor = recuperaCategoria(); 
     catCursor.moveToPosition(1); 
     catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); 
     addCatSpinner.setAdapter(catAdapter); 
     if (catCursor.moveToFirst()) { 
      do { 

      catAdapter.add(catCursor.getString(1)); 
      Valor = catCursor.getString(1); 
      Log.v("valor Add Cat 100", Valor); 
     } 
      while (catCursor.moveToNext()); 
      int id = catCursor.getInt(0); 
      String name = catCursor.getString(1); 

      Log.v("Dentro cursor","1"); 
      if (db != null) { 
      Toast.makeText(getBaseContext(),catCursor.getString(0),Toast.LENGTH_SHORT).show(); 

      db.close(); 
      } 
     } 
     startManagingCursor(catCursor); 
     //catCursor.close(); 
     addCatSpinner.setOnItemSelectedListener(
       new AdapterView.OnItemSelectedListener() { 
        public void onItemSelected(AdapterView<?> parent, 
         View view2, int pos, long id) { 
         //recID(); 
         valor = parent.getItemAtPosition(pos).toString(); 
         Log.v("valor Add Cat", valor); 

         } 
        public void onNothingSelected(AdapterView<?> parent) { 
         // view.setText("nada"); 
        } 
       }); 
     catCursor.close(); 
} 

私は(それがコラム「ノンブル」と表示されます)をスピナーから1つの項目を選択すると、私は1つの変数に保存することを希望カーソル "_id"の最初の列。コレクションに

+0

別のテーブルにそれらを挿入するためにスクリプトを書きます別のテーブル。またはあなたの質問を絞り込む。 – Maxim

+0

ありがとう、それはsqlliteです。私はスピナーで選択された名前で新しいカーソルを書いています。 – user1107620

答えて

1

読む値、あなたはSQLiteのdbテーブルを意味する場合は、IDを読み、それらを追加するSQLスクリプトを書く

SQLiteDatabase db = getReadableDatabase(); 
String[] columns = { _id }; 
String selection = "column_name" + "=?"; 
String orderBy = "_id ASC"; 
String[] selectionArgs = new String[] { "something" }; 
String groupBy = null; 
String having = null; 

Cursor cursor = db.query("table_name", "columns", selection, selectionArgs, groupBy, having, orderBy); 

while(cursor.moveToNext()) { 

    int id = cursor.getInt(0); 
    String name = cursor.getString(1); 

    // Add values to a collection 
} 
if(db.isOpen()) db.close(); 

SQLiteDatabase db = getWritableDatabase(); 

for(int id : idCollection) { 

    ContentValues values = new ContentValues(); 
    values.put("_id", id); 

    try { 

    int id = db.insertOrThrow(TABLE_NAME, null, values); 
    } 
    catch (SQLException sqlEx) { 

    // do something with exception 
    } 
} 

if(db.isOpen()) db.close(); 
+0

ありがとう – user1107620

関連する問題