2017-01-03 11 views
0

結果メソッドのアクティビティを開始する場所を渡しています。 placeはストリングであり、ストリングとして宣言されたストリングです。StartActivityForResultメソッド内に文字列値をインテントできません

結果の開始アクティビティこの2つの方法で値を取得しようとしましたが、どちらも機能していません。

final String place = data.getStringExtra("location"); 
final String place = data.getExtras().getString("location"); 

これは、あなたが宣言した場合、このメソッドは

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RESULT_TRUE && resultCode == RESULT_OK) { 
      Uri url = data.getData(); 
      final String place = data.getStringExtra("location"); 
      StorageReference filepath = mstorage.child("photos").child(url.getLastPathSegment()); 
      filepath.putFile(url).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
       @O 
+0

は*です:-)お手伝いを致します*置く**オブジェクトまたは文字列? –

+0

場所は文字列です – user7327850

+0

あなたはonActivityResult内で取得しているインテントデータが異なります –

答えて

0

どのように見えるか、あなたがそのactivityで1 global variableを宣言し、あなたは簡単にその値にアクセスすることができますonActivityResult()に渡すいくつかの特定の値を割り当てることができますです

この場合、PLACEの値は各ボタンのクリニックごとに異なりますK:

public class MyActivity extends AppCompatActivity { 

String PLACE; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.xx); 



    Btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      PLACE = place; 
      //intent.putExtra("location",place); 
      startActivityForResult(intent, RESULT_TRUE); 
     } 
    }); 

    Btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      PLACE = place; 
      //intent.putExtra("location",place); 
      startActivityForResult(intent, RESULT_TRUE); 
     } 
    }); 

    Btn3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      PLACE = place; 
      //intent.putExtra("location",place); 
      startActivityForResult(intent, RESULT_TRUE); 
     } 
    }); 


} 

}

+0

buttonAが場所1 buttonBは場所2 buttonCが行わ値が各ボタン – user7327850

+0

にこの文句を言わない仕事を渡すためにしたい場所3 を持っているがありますbuttonAの値はimage1、placeBの値はimage2です。 – user7327850

0

あなたはfinal String place = data.getStringExtra("location");を取得しようとしているが、誰もがセットにそれを持っていないとして、データは名前のキー「場所」を含んでいません。

ギャラリーを開いたときにのみ設定されます。

あなたはすでにロケーション値を持っていますが、なぜそれを使用しないのですか?

+0

場所のために異なっているので、私は、これらのボタンのそれぞれから結果をonActivityを呼び出し、ロケーション値 – user7327850

+0

私はこれらのボタンからonActvitystartMethodを呼び出すので、どのボタンがクリックされたかに応じて異なる場所の値をonActivityメソッドに送信します。 – user7327850

+0

コードから取得するのは、アプリケーションからギャラリーを開き、キー "場所"の権利?イメージを選択すると、結果(イメージの場所やその他の必要なデータ)が設定されます。onActivtyResultメソッドを呼び出すと、同じアクティビティが呼び出されます。 – Tulsi

0

あなたは概念を正しく理解していないようですね!これは、startActivityForResultを理解するのに役立ちます。あなたは別のアクティビティを開始することができ、その後、onActivityResult() method.So onActivityResult()でその活動から結果を受け取るstartActivityForResult(Intent intent, int requestCode)を使用することにより

は、別のアクティビティを開始するところからです。

public class MainActivity extends Activity { 

     // Use a unique request code for each use case 
     private static final int REQUEST_CODE_EXAMPLE = 0x9988; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      // Create an Intent to start AnotherActivity 
      final Intent intent = new Intent(this, AnotherActivity.class); 

      // Start AnotherActivity with the request code 
      startActivityForResult(intent, REQUEST_CODE_EXAMPLE); 
     } 

     //-------- When a result is returned from another Activity onActivityResult is called.--------- // 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 

      // First we need to check if the requestCode matches the one we used. 
      if(requestCode == REQUEST_CODE_EXAMPLE) { 

       // The resultCode is set by the AnotherActivity 
       // By convention RESULT_OK means that what ever 
       // AnotherActivity did was successful 
       if(resultCode == Activity.RESULT_OK) { 
        // Get the result from the returned Intent 
        final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA); 

        // Use the data - in this case, display it in a Toast. 
        Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show(); 
       } else { 
        // AnotherActivity was not successful. No data to retrieve. 
       } 
      } 
     } 
    } 

AnotherActivity < - この私たちはMainActivity

public class AnotherActivity extends Activity { 

     // Constant used to identify data sent between Activities. 
     public static final String EXTRA_DATA = "EXTRA_DATA"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_another); 

      final View button = findViewById(R.id.button); 
      // When this button is clicked we want to return a result 
      button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        // Create a new Intent as container for the result 
        final Intent data = new Intent(); 

        // Add the required data to be returned to the MainActivity 
        data.putExtra(EXTRA_DATA, "Some interesting data!"); 

        // Set the resultCode to Activity.RESULT_OK to 
        // indicate a success and attach the Intent 
        // which contains our result data 
        setResult(Activity.RESULT_OK, data); 

        // With finish() we close the AnotherActivity to 
        // return to MainActivity 
        finish(); 
       } 
      }); 
     } 

     @Override 
     public void onBackPressed() { 
      // When the user hits the back button set the resultCode 
      // to Activity.RESULT_CANCELED to indicate a failure 
      setResult(Activity.RESULT_CANCELED); 
      super.onBackPressed(); 
     } 
    } 

そして、あなたのタスクのためにあなたはそれがあります上記の方法を必要としませんにデータを送信するために使用するものあなたの知識のために、以下のようにしてください

<Button 
    android:onClick="gatherResults" <--- method name in your class 
    android:id="@+id/button_id_one" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" /> 

あなたの好きなボタンをお持ちください!そして彼らはあなたがの意図に値を入れている

public void gatherResults(View view){ 
     // Perform action on click 
     switch(view.getId()) { 
      case R.id.button_id_one: 
       //DO what you want for specific id button_id_one 

       break; 
      case R.id.button_id_two: 
       //DO what you want specific id button_id_two 
       break; 
     } 
    } 
+0

ああ、ありがとう、ありがとう。私はonActivity開始メソッドを削除します..明日これを試してあなたを更新します。あまりにも遅いです..ありがとう&おやすみ – user7327850

+0

@ user7327850あなたが望むように:)あなたが何かを学ぶことを願って –

+0

@ user7327850フラグメントを使用している場合、この 'gatherResults'をフラグメントの内部ではなく親のActivityクラスで使用します –

0

、そして今、あなたのクラスでそのメソッドを記述し、そのID

を使用してクリックしてしまった一つのフィルタ、上記のいずれかのようgatherResultsと呼ばれる方法を指しますあなたが起動しているactivityです。だから、あなたはその新しい活動だけでこの価値を得ることができます。現在のアクティビティでこの値を使用したい場合は、グローバル変数を維持し、ボタンをクリックするだけで値を更新します。

0

このお試しください: - 断片で

を: - 活動の

View v; 
public static String Place; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    v = inflater.inflate(R.layout.yourLayout, container, false); 
    return v; 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

//Initialize your buttons 

btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Place=yourPlace; 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      startActivityForResult(intent, your_Request_code); 
     } 
    }); 

btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Place=yourPlace; 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      startActivityForResult(intent, your_Request_code); 
     } 
    }); 

btn3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Place=yourPlace; 
      Intent intent = new Intent(Intent.ACTION_PICK); 
      intent.setType("image/*"); 
      startActivityForResult(intent, your_Request_code); 
     } 
    }); 

} 

: -

String str_place; 

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(requestCode == REQUEST_CODE_EXAMPLE) { 
      if(resultCode == Activity.RESULT_OK) {   
       str_place=yourFragment.Place; 
       //Do your stuff here and use this place object where you want 
      } else { 

      } 
     } 
    } 

希望これは:-)

関連する問題