2013-01-01 4 views
1

je suis rentré à la maison pour le faire quand j'écris un pays dans une nouvelle activité (forresult) il va l'écrire dans l'activité principale et la couleur que j'écris et la nouvelle activité va changer la couleur du texte à quoi J'ai écrit. mais Ok et Annuler le transfert wont toute information recherchée solution sans succèsAndroid | StartActivityForResult ne fait rien

MainActivity -

public class MainActivity extends Activity implements OnClickListener { 
TextView tvDisplayCountry; 
Random crazy; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    initsialize(); 

} 

private void initsialize() { 
    Button bAddCountry = (Button) findViewById(R.id.bAddCountry); 
    bAddCountry.setOnClickListener(this); 
    tvDisplayCountry = (TextView) findViewById(R.id.tvDisplayCountry); 
    // ImageView ivCountryPhoto = 
    // (ImageView)findViewById(r.id.ivCountryPhoto); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.bAddCountry: 
     Intent countryactivityIntent = new Intent(MainActivity.this, 
       CountryActivity.class); 
     startActivityForResult(countryactivityIntent, 12); 

     break; 
    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


    switch(resultCode){ 
    case 12: 
     if (resultCode == Activity.RESULT_OK) { 
      String Country = data.getStringExtra("country"); 
      String Color = data.getStringExtra("color"); 
      tvDisplayCountry.setText(Country); 
      if (Color.equalsIgnoreCase("blue")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.BLUE); 
      } else if (Color.equalsIgnoreCase("yellow")) { 
       tvDisplayCountry 
       .setTextColor(android.graphics.Color.YELLOW); 
      } else if (Color.equalsIgnoreCase("gray")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.GRAY); 
      } else if (Color.equalsIgnoreCase("red")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.RED); 
      } 
      else{ 
       Toast.makeText(MainActivity.this, "canceld", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
     } 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 

}

CountryActivity -

public class CountryActivity extends Activity implements OnClickListener { 
EditText etCountry, etColor; 
String getCountry, getColor; 
Button bOk,bCancel; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_country); 



    init(); 


} 

private void init() { 
    etColor = (EditText)findViewById(R.id.etColor); 
    etCountry = (EditText)findViewById(R.id.etCountry); 
    bOk = (Button)findViewById(R.id.bOk); 
    bCancel = (Button)findViewById(R.id.bCancel); 
    bOk.setOnClickListener(this); 
    bCancel.setOnClickListener(this); 


} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.bOk: 
     Intent myIntet = new Intent(); 
     getCountry = etCountry.getText().toString(); 
     myIntet.putExtra("country",getCountry); 
     getColor = etColor.getText().toString(); 
     myIntet.putExtra("color", getColor); 

     setResult(Activity.RESULT_OK, myIntet); 
     finish(); 

     break; 

    case R.id.bCancel: 
     Intent myIntent = new Intent(); 

     setResult(Activity.RESULT_CANCELED, myIntent); 
     finish(); 
     break; 
    } 

} 
+0

Avez-vous eu une erreur dans LogCat? –

+0

Veuillez ajouter quelques Log.i ("", "Quelque chose d'utile") dans votre code et vérifier où il y a un problème. Nous ne pouvons pas tester votre code entier! –

+0

aucun problème dans le chat de journal en poussant ok ou annuler et l'activité principale s'ouvre sans changement –

Répondre

2

Il y a une erreur dans votre onActivityResult - vous allumez le resultCode (qui sera RESULT_OK ou RESULT_CANCELED, pas 12).

Remplacez simplement switch(resultCode) par switch(requestCode).

+0

merci d'avoir attiré mon attention! problème résolu –