2017-05-20 1 views
0

tab1과 tab3도 클래스가 있으며 tab1에서 단추를 클릭하고 tab3에서 textview를 변경하려고하지만 어쨌든 찾을 수 없습니다. 이 내 TAB1 클래스입니다하나의 탭에서 단추를 클릭하고 anoter 탭의 텍스트 뷰를 변경하십시오.

public class tab1Contacts extends Fragment{ 

    TextView tv; 
    EditText et; 
    TextView tv3; 
    personInfo pı; 

    public personInfo returnpı(){ 
     return pı; 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab1contents, container, false); 

     Button btn_jog = (Button) rootView.findViewById(R.id.jogging_button); 


     tv = (TextView) rootView.findViewById(R.id.newRecordText); 
     et = (EditText) rootView.findViewById(R.id.durationtext) ; 
     pı = new personInfo(); 
     pı.eyesPower = 100; 
     pı.brainPower = 100; 
     pı.armsPower = 100; 
     pı.legsPower = 100; 
     pı.hearthPower = 100; 
     pı.energyLevel = 100; 
     pı.calorie = 2000; 
     pı.condition = 0; 

      btn_jog.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        int duration = Integer.parseInt(et.getText().toString()); 
        pı.jogging(duration); 
        //I want to change here textview in the tab3. 
       } 
      }); 
     return rootView; 
    } 
} 

이 또한 내 tab3에 클래스 : tab3에이에서 이벤트를 수신하는

public class Tab3Contacts extends Fragment { 

    TextView tv3; 
    double newBrainpower; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab3contents, container, false); 
     tv3 = (TextView) rootView.findViewById(R.id.list_text) ; 


     return rootView; 
    } 
} 

답변

0
btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    //pager.setCurrentItem(yourindex);// if you use pager 
    getTabHost().setCurrentTab(yourindex); 

    } 
}); 
+0

getTabHost 메서드를 해결할 수 없습니다. –

+0

사용자가 단추를 클릭하면 탭을 변경하고 싶지 않습니다. 나는 anoter 탭에서 textview를 변경하고 싶다. –

0

내가 제대로 질문을 읽고 있어요 경우에, 당신이해야 할 것은 tab1. 이를 위해서는 내부 알림/이벤트 시스템을 구현해야합니다. 일반적으로 관찰자/수신기를 등록하는 알림 처리 클래스를 통해 처리됩니다.

내가 유지하고 한 프로젝트에서 예 :

public class NotificationManager { 
    public interface Observer { 
     public void update(String notificationName, Bundle data); 
    } 

    private static NotificationManager singletonNotifier = null; 
    private HashMap<String, ArrayList<Observer>> mObservables = null; 

    private NotificationManager() { 
     mObservables = new HashMap<String, ArrayList<Observer>>(); 
    } 

    //enforce singleton 
    public static NotificationManager getInstance() { 
     if (singletonNotifier == null) { 
      singletonNotifier = new NotificationManager(); 
     } 
     return singletonNotifier; 
    } 

    public void addObserver(String notificationName, Observer observer) { 
     // add to map 
     // in multi-threaded apps make sure you use synchronized or a mutex 
    } 

    public void removeObserver(String notificationName, Observer observer) { 
     // remove from map; mind threading 
     // overload as necessary for your design 
    } 

    public void notifyObservers(String notificationName, Bundle data) { 
     // go through your map of observers, build an array of observers 
     // that need to update, then for each observer, call 
     // observer.update(notificationName, data); 
    } 
} 

이 그런 다음 tab3에 클래스가 관찰자 인터페이스와 객체 생성에이 유형의 문자열 값과 노 티피 케이션 자신을 등록 구현해야 통지 그것은 y를 모든 변경 사항을 만들 것입니다 update(String, Bundle) 방법을 구현해야합니다 호출

NotificationManager.getInstance().addObserver("Tab1DataChange", this); 

를 사용하여, (대신 문자열 리터럴 인수의 상수에 대한 모범 사례를 사용) 원 너 필요해. 그런 다음 TAB1 객체의 클래스에 클릭 리스너에이 호출을 추가

데이터가 관찰자의 응답을 알 필요가 있다는 어떤 정보가
NotificationManager.getInstance().notifyObservers("Tab1DataChange", data); 

. 코드 디커플링 (decoupling)이라는 개념에 따라 하나의 리스너에 대해 명시 적으로 데이터 번들을 구성하지 마십시오. 동일한 시점에서 수신 대기해야 할 부분이 있기 때문입니다. 누가 이벤트를 소비하는지에 관계없이 업데이트해야하는 것을 포함하도록 데이터 번들을 설계함으로써 이제는 슬픔을 덜어 줘야합니다.

나를 위해 배운 교훈 : Android 수명주기에 유의하십시오. OnPause 및 OnDestroy는 관찰자 개체를 사용할 수없는 동안 무언가가 해당 이벤트를 트리거하면 null 포인터 예외로 끝나지 않도록 수신기를 등록 취소해야합니다. OnCreate 및 OnResume이 다시 등록해야합니다. 어떤 경우에는 OnPause/OnResume에 대해 걱정할 필요가 없었지만 앱에 따라 필요할 수도 있습니다.

관련 문제