2016-11-06 65 views
0

我有兩個活動,其中一個(FirstActivity),其中有一個點擊ListView與兩個項目item1item2。另一個(SecondActivity)其中ViewFlipper有兩個子佈局layout1layout2,我想要layout1點擊item1ListView,我的問題是我該怎麼做呢?我已經做了迄今爲止如何從可點擊的ListView去ViewFlipper的特定孩子?

showPoemListLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      if(position == 0){ 
       viewFlipper.setDisplayedChild(0); 
       viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.anamikaSV))); 


      } 

     } 
    }); 

,而是因爲你還沒有開始的SecondActivity yet.You可以做到這一點並沒有在所有的工作:(空指針異常出現點擊item1

回答

0

你得到後NPE這

在FirstActivity

showPoemListLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


     if(position == 0){ 
      Intent intent = new Intent(this, SecondActivity.class); 
      intent.putExtra("POSITION_CLICKED",0); 
      startActivity(intent); 
     } 

    } 
}); 

然後在你的SecondActivity的OnCreate中,後電話,做到這一點

ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.your_viewflipper_id); 
Intent intent = getIntent(); 
int POSITION_CLICKED = intent.getIntExtra("POSITION_CLICKED",0); 
viewFlipper.setDisplayedChild(POSITION_CLICKED); 

只需更換R.id.your_viewflipper_idViewFlipper的ID。

Developer guide on starting a new Activity

+0

我已經得到了相應的答案,非常感謝@Veneet Raddy –