どのように私はいくつかの目的のために異なる.putextrasを転送することができますか?

大気プロダクション:

私はいくつかのマーカーやカスタム情報Windowsとputextrasを持っているので。私はずっとそのputextrasを使用するのか分からないが、私は彼らに少しを使用する方法を知っています。私は、私が説明しますカントリーアダプタへMapsActivityからの転送データへのプットの余分を使用して:私の考えは、そのCustomInfoWindow意思をユーザーがクリックするの命名開きカントリーアダプタですので。その国のアダプタは、タイトルとコンテンツの2 textviewsを持っています。現在、私は単なるデータの1セットを転送することに成功していますが、他のだけでは動作しません。私は何かを間違ってやっているかもしれませんが、私は本当に入れエキストラの設定で混乱します。MapsActivity:

@Override
    public void onInfoWindowClick(Marker marker) {

        if("India".equals(marker.getTitle())) {
            Intent intent = new Intent(this,Country.class);
            intent.putExtra("India","text");

            startActivity(intent);

        }
        if("Australia".equals(marker.getTitle())){
          Intent intent = new Intent(this,Country.class);
          intent.putExtra("Austrailia","text");
// want to create  a method that when this is clicked it gives different texts.
          startActivity(intent);
        }

私の国アダプタ:

Button bt = findViewById(R.id.button);
        bt.setOnClickListener(v -> openMap());
        TextView countryName = findViewById(R.id.textView);
        TextView Main = findViewById(R.id.textView2);
        Bundle bundle = getIntent().getExtras();

        if(bundle != null) {
            String India = bundle.getString("India");
            countryName.setText("India,South Asia");

        }
        if(bundle != null) {
            String Australia = bundle.getString("Australia");
            countryName.setText("Australia,Oceania");

        }

    }
    public void openMap(){
        finish();
    }
}

私はそれらの両方のための異なるテキストを表示したいが、それは動作しません。私は詳細にこのようにしてください答えに非常に新しいです..

カスパーDavidsen:

あなたは正しい軌道に乗っているが、あなたはテントバンドルからデータを抽出方法で、あなたの主な問題の嘘。

まず、あなたのコードを考えてみます。

if(bundle != null) {
    String India = bundle.getString("India");
    countryName.setText("India,South Asia");
}
if(bundle != null) {
    String Australia = bundle.getString("Australia");
    countryName.setText("Australia,Oceania");
}

インドについてあなたは、基本的に最初の抽出情報。あなただけのインドに設定されたテキストを上書き - かかわらず、これが成功したかどうかの、あなたはその後、オーストラリアのために同じことを行います。あなたは今まで、出力としてオーストラリアを見ることができます。

あなたはテントのエキストラを使う方法で、あなたの第二の問題の嘘。Androidのドキュメント

公共テントputExtra(文字列名、文字列値)

名前の文字列:余分なデータの名前、パッケージの接頭辞を持ちます。

値文字列:文字列データ値。この値はnullになることがあります。

次のような余分の名前を使用してオフに優れているのでcom.mypackage.CountryName、その後に値を設定しますIndiaあなたはその後、という別の意図余分持つことができcom.mypackage.CountryDetailsますが、表示したい記述値とを。あなたのMapsActivityで:

Intent intent = new Intent(this,Country.class);
if("India".equals(marker.getTitle())) {
    intent.putExtra("com.mypackage.CountryName", "India,South Asia");    
    intent.putExtra("com.mypackage.CountryDetails", "...");
} else if("Australia".equals(marker.getTitle())){
    intent.putExtra("com.mypackage.CountryName", "Australia,Oceania");
    intent.putExtra("com.mypackage.CountryDetails", "...");
}
startActivity(intent);

そして、あなたの国アダプタで:

        Button bt = findViewById(R.id.button);
        bt.setOnClickListener(v -> openMap());
        TextView countryName = findViewById(R.id.textView);
        TextView Main = findViewById(R.id.textView2);
        Bundle bundle = getIntent().getExtras();

        if(bundle != null) {
            String name = bundle.getString("com.mypackage.CountryName");
            countryName.setText(name);
            String details = bundle.getString("com.mypackage.CountryDetails");
            countryDetails.setText(details);

            // If you absolutely must do something on a per-country basis, then use a
            // switch or chained if..else inside of this if statement
        }

        // Notice how the second if statement is gone

    }
    public void openMap(){
        finish();
    }
}

あなたは責任と建築の代表団についての長い議論を持つことができます。私は今のところこれを無視します。しかし、あなたの意思エキストラに名前を付けるための適切なコーディングプラクティスの観点から、見ていこれに関連する答えを

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=31472&siteId=1