2017-04-20 35 views
0

我已經有一個名爲SI的活動,帶有GridView,其中包含可讓您在單擊時進入另一活動的單元。我想知道如何得到相同的活動,只是根據哪個意圖啓動它來更改TextView。如何根據哪些意圖啓動它來更改活動內容?

public class SI extends AppCompatActivity { 

    GridView gridView; 
    String[] units; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_zakladnijednotky); 
     gridView = (GridView) findViewById(R.id.gridView1); 
     units = getResources().getStringArray(R.array.units); 
     ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, units); 
     gridView.setAdapter(adapter); 
     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, 
            int position, long id) { 

       if (gridView.getItemAtPosition(position).toString().contains("amper")) { 
        Intent mintent = new Intent(SI.this, MainActivity.class); 
        startActivity(mintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("kilogram")) { 
        Intent kilintent = new Intent(SI.this, MainActivity.class); 
        startActivity(kilintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("metre")) { 
        Intent metrintent = new Intent(SI.this, MainActivity.class); 
        startActivity(metrintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("second")) { 
        Intent mintent = new Intent(SI.this, MainActivity.class); 
        startActivity(mintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("kelvin")) { 
        Intent mintent = new Intent(SI.this, MainActivity.class); 
        startActivity(mintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("candela")) { 
        Intent mintent = new Intent(SI.this, MainActivity.class); 
        startActivity(mintent); 
       } 
       else if (gridView.getItemAtPosition(position).toString().contains("mol")) { 
        Intent mintent = new Intent(SI.this, MainActivity.class); 
        startActivity(mintent); 
       } 

      } 
     }); 
    } 
    ... 
} 
+1

的可能的複製[我如何從意向o獲得額外的數據[Android的]](http://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android) – Gary99

回答

1

您可以傳遞意圖中的一些數據並在新的活動中讀取該數據。

Intent mintent = new Intent(SI.this, MainActivity.class); 
String strName = "kilogram"; 
i.putExtra("KEY_NAME_FOR_STRING_I_NEED", strName); 
startActivity(mintent); 

閱讀它

Bundle extras = getIntent().getExtras(); 
newString= extras.getString("KEY_NAME_FOR_STRING_I_NEED"); 

,或者另一種方式來做到這一點

newString= getIntent().getStringExtra("KEY_NAME_FOR_STRING_I_NEED"); 

並改變取決於至極值的TextViews都在裏面newString

if(newString.equals("kilogram")) { 
    //do something... 
} 
+0

謝謝你的回答,我會嘗試它,但我代表什麼? –

+0

對不起,我不知道你的意思。也許這[文檔鏈接](https://developer.android.com/guide/components/intents-filters.html)解決您的疑惑 – jorgeavilae

+0

謝謝,它幫助了我。 –