2013-03-10 3 views
14

첫 번째 적절한 Android 앱을 만들기 위해 파편을 사용하려고합니다. 기본 xml 있습니다. 두 개의 수직 조각으로 구성된 최상위 조각은 단 두 개의 TextView로 구성됩니다. 첫 번째는 정적 텍스트를 포함하고 두 번째는 결국 SQL에서 동적으로 가져 오는 값을 포함합니다.Android : 내 텍스트 뷰를 조각에서 업데이트하려면 어떻게합니까

난이 다음 행복하게 내 첫 번째 조각의 텍스트 뷰의 값을 업데이트 내 MainActivity.java입니다 넣을 경우 각각 그래서 넣을 지원하기 위해 나는 두 조각 XMLS이

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    // Set the Text to try this out 
    TextView t = (TextView)findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 
} 

와 자바 이 필드를 MainActivity에 대한 Java가 아닌 조각을 지원하는 Java로 설정합니다.

public class FragmentMainTop extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState){ 
    // -- inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragmentmt, container,false); 

    // Set the Text to try this out 
    TextView t = (TextView)findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 
} 

하지만 이렇게하면 나는 텍스트 뷰 라인에 오류가 얻을 :

"방법 findViewById를 (INT) 인을 - : 나는 조각에 넣을 때

그것은 다음과 같습니다 FragmentMainTop 유형에 대해 정의되지 않음 "

그렇다면이 방법을 모르는 이유는 무엇입니까? 나는 ctl/shift/o를 가지고 있으므로 모든 올바른 수입이 있다는 것을 알고있다. MainActivity에 모든 코드를 넣고 싶지는 않습니다. 왜냐하면 다른 활동에서 Fragment를 사용하고 싶다면 모든 코드를 반복해야하기 때문입니다.

답변

45

findViewById() 메서드에 액세스 할 수 있도록 가변 된 단편 레이아웃을 변수에 지정해야합니다. 그런 다음 위젯 업데이트를 완료하면 반환합니다. 당신이 ActivityfindViewById()를 호출 할 때

@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState){ 
    // -- inflate the layout for this fragment 
    View myInflatedView = inflater.inflate(R.layout.fragmentmt, container,false); 

    // Set the Text to try this out 
    TextView t = (TextView) myInflatedView.findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 

    return myInflatedView; 
} 

일반적으로, 당신은 Activity.findViewById()를 호출하고 있습니다. 같은 Fragment.findViewById() 메서드가 없으므로 Fragment 클래스에서 동일한 호출을 시도하면 실패합니다. 따라서 위젯에 대한 참조를 얻으려면 팽창 된 뷰와 함께 View.findViewById()을 사용해야합니다.

+0

참조가 fragment.getActivity()에 의해 반환 된 Activity에서 findViewByID를 수행하는 경우에도 참조가 성공적으로 반환됩니다. 그러나 뷰를 동적으로 업데이트하는 것은 여전히 ​​작동하지 않습니다. –

2

나는 전문가가 아니다 그러나 나는 방법 내에서 돌려 문이 뭔가를 반환하고이 문장 후 다음 라인에 연결할 수 있습니다 말할 것입니다 : O

그래서 순서를 변경하려고! :)

0

내 활동에서이 코드를 사용하여 xml 태그에 추가 된 텍스트 뷰를 업데이트합니다. 텍스트 뷰는 클래스 수준의 객체입니다. 몇 가지 코드가 있습니다 :

<fragment 
    android:id="@+id/fragmentRegister" 
    android:name="com.proshore.loveis.RegisterFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/view1" /> 

Heres는 클래스 수준의 변수를 보여주는 코드입니다.

public class RegisterFragment extends Fragment implements OnClickListener { 

TextView textViewDOB, textViewLanguage; 

public RegisterFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
//eluded methods here 
} 
} 

지금은 내 조각 업데이트 곳에서 활동

public class RegisterActivity extends FragmentActivity { 
Button buttonEnter; 
RegisterFragment fragmentRegister; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 
    buttonEnter = (Button) findViewById(R.id.buttonEnter); 
    fragmentRegister = (RegisterFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.fragmentRegister); 
} 

@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    buttonEnter.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Log.d("dontknowitwillowrk", fragmentRegister.textViewLanguage 
        .getText().toString()); 
      fragmentRegister.textViewLanguage.setText("hello mister how do you do"); 


     } 
    }); 
} 
} 

참고 :이 좋은 방법입니다 있는지 확실하지 않습니다.

관련 문제