2017-03-21 5 views
0
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if (position == 0) { 
     Intent i = new Intent(this,second.class); 
     startActivity(i); 
    } else if (position == 1) { 
     Intent i1 = new Intent(this,second.class); 
     startActivity(i); 
    } 
} 

私はこの方法を知っていますが、例えば20のサブアイテムがあるので、20のアクティビティが必要です。どのように私はそれを1つだけのアクティビティが渡されますが、その中のデータは、(その中のデータによって、私は単純なTextviewsを意味する)クリックしたサブアイテムに応じて変更申し訳ありません私の英語は非常に悪いアンドロイドグリッドビューのサブアイテムの開始アクティビティonclick

+0

テキストが異なる場合、または活動のレイアウトが同様に異なります

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(this, second.class); i.putExtra("position", position); startActivity(i); } 

は、その後、あなたの活動に、あなたは、このように意図を得ることができます各要素について? – Ancuta

+0

同じレイアウト! textviewsの中のちょうど異なるテキスト –

+0

次に、以下の答えを見てください。 – Ancuta

答えて

1

あなたはどこから来たのかindentifyする活動の意図:

int position; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); //if your activities have different layouts depending the given position, you can move this line inside the switch function 
    ... 
    if(getIntent().getExtras() != null) { 
     position = getIntent().getExtras().getInt("position", 0); 
    } 
    switch(position){ 
     case 1: 
      //do something 
      break; 
     case 2: 
      //do another thing 
      break; 
     default: 
      //default behaviour 
      break; 
    } 
} 
関連する問題