1

クリックしたデータの ID をリストビューから 2 番目のクラスの新しいアクティビティに渡そうとしています。つまり、アイテムをクリックしlistviewます。onListItemClickメソッドが呼び出され、新しいインテントが開始されます。id は、 のオブジェクトとともに渡されますi.getExtra。次に、id が 2 番目のクラスの新しい変数に格納され、後で使用されます。

ID を渡す方法まではわかったのですが、それを 2 番目のクラスの新しい変数に格納する方法がわかりません。

私のコードは次のとおりです。

public void onListItemClick(ListView list, View v, int list_posistion, long item_id)
{


    long id = item_id;
    Intent i = new Intent("com.example.sqliteexample.SQLView");
    i.putExtra(null, id);
    startActivity(i);
}

2番目のクラスでそれを参照する方法を誰か教えてもらえますか?

4

4 に答える 4

0

とてもシンプルです。
変更するだけです:

i.putExtra(null, id);

と :

i.putExtra("myId", id);

2番目のアクティビティでは、次を使用します:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getInt("myId");
}
于 2013-01-11T20:37:11.270 に答える
0

特定の要素を取得するには、Intent から Bundle を取得してから get... を実行する必要があります。

Bundle extras = getIntent().getExtras(); 
String id;

if (extras != null) {
    id= extras.getString("key");  //key should be what ever used in invoker.
}

驚くべきことの 1 つは、なぜnullキーとして使用しているのかということです。予約語の使用は避け、代わりに適切な名前userIDなどを使用します。

于 2013-01-11T20:32:37.083 に答える
0
Intent intent = new Intent("com.example.sqliteexample.SQLView");
                    Bundle bundle = new Bundle();
                    bundle.putString("position", v.getTag().toString());
                    intent.putExtras(bundle);
                    context.startActivity(intent);

セカンドクラスで

 Bundle intent= getIntent().getExtras(); 

       if (intent.getExtras() == null) {
    id= intent.getString("position");
    }

お役に立てれば

于 2013-01-11T20:33:34.350 に答える
0

の最初のパラメーターIntent.putExtra()は、Extra を識別するために使用される文字列キーです。i.putExtra(null, id)試してみる代わりにi.putExtra("SomeString", id)

次に、2 番目のアクティビティ (またはその中の任意の場所) の onCreate で、次のようにインテントから ID を取得できます。

Intent intent = getIntent();
long id = intent.getLongExtra("SomeString");

String、Char、Booleans、Int、およびより複雑なデータ構造を取得するためのメソッドもあります。Intent クラスのメソッドの詳細については、 http: //developer.android.com/reference/android/content/Intent.htmlを確認してください。

于 2013-01-11T20:38:12.373 に答える