2010-11-24 10 views

答えて

1

intent.putExtraを使用して、1つのアクティビティから別のアクティビティにプリミティブ型を渡すことができます。 startActivity()でアクティビティを開始する前にこれを行います。

1

あなたはその

**Intent intent = new Intent(current.getContext(), desired.class);       
intent.putExtra("id", id);      
startActivityForResult(intent, 0);** 

内のメソッド をonListItemClick実装するために持って、あなたが値を渡すと言うとき、このコード

**Long id = getIntent().getLongExtra("id", 0);** 

置く目的のクラスにあなたがこの

1

を試してみることができます別の「クラス」に別のアクティビティ、または別の標準Javaクラスを意味しますか?

このIDを他のアンドロイドのアクティビティに渡す場合は、それをインテントに配置し、ターゲットアクティビティでstartActivityを呼び出すことができます。 this

Intent i = new Intent(this, ClassToSendTo.class); 
i.putExtra("id", id); 
startActivity(i); 

を見ても、あなたは(最良の選択肢ではないかもしれません)が、あなたが他の場所にアクセスできるようにアプリケーションコンテキストに追加することができます。私はこれがあなたの最高の選択肢だとは思わないが、その価値があることを心に留めておく。私はそれを行う方法を書いたhere。あなたのケースで

MyClass myClass = new MyClass(); 
myClass.doSomething(id); 
1

あなただけの活動ではない別のクラスにそれを渡したい場合は

、その後、次のような、あなたは通常、Javaでどうなるのか行うことができます。 、新しい活動に

case R.id.edit: 
      AdapterView.AdapterContextMenuInfo infoCode = (AdapterView.AdapterContextMenuInfo) item 
      .getMenuInfo(); 

      addId(infoCode.id); 
      return (true); 

は、あなたの現在の活動で作成したコード、ADDID()メソッド

private void addCode(final long rowId) { 
     if (rowId > 0) { 
      // load your sqlite 
      // your database handler 
      DatabaseHelper db = new DatabaseHelper(getApplicationContext()); 

      SQLiteDatabase dbs = db.getReadableDatabase(); 
        //query 
      Cursor cursor = dbs.rawQuery("SELECT * FROM your_table where _ID=" 
        + rowId + "", null); 
      if (cursor.moveToFirst()) { 
       do { 
        String numberId = cursor.getString(0); // Here you can get data 
                 // from table and stored 
                 // in string if it has 
                 // only one string. 
         // if you add another field 
        //String fieldCode = cursor.getString(1); 
        //String fieldCode = cursor.getString(2); 

        Intent intent = new Intent(CurrentActivity.this, NewActivity.class); 
        intent.putExtra("new_variable_name",numberId); 
        startActivity(intent); 

       } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
       cursor.close(); 
      } 
      if (db != null) { 
       db.close(); 
      } 

を追加するのonCreateのコードを追加します。

//if your send it to edittext (Edit) 
TextView txt = (TextView) findViewById(R.id.youreditextid); 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String value = extras.getString("new_variable_name"); 
      txt.setText(value); 
     } 
関連する問題