2015-12-06 4 views
0

Я написал код андроида, используя фрагменты (Im an newbe), и я хочу, когда фрагменты переключались, чтобы поместить текст в текстовое представление. мои фрагменты делают переключатель (например, намерение), но текст в тексте не изменяется. вот код: общественный класс Map_Frag расширяет фрагмент {андроид фрагмент версия 4 текст выпуск

---interface--- 
    Communicator callback; 
    TextView tv; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.frag_map, container, false); 

      tv = (TextView)v.findViewById(R.id.textView); 

     return v; 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     Bundle bundle = getArguments(); 
     if (bundle != null) { 
      if (bundle.getBoolean("str1")) 
       changTextToWalla1("str1",bundle.getInt("x")); 
     } 
    } 

    public void changTextToWalla1(String str1, int x) { 
     switch (x){ 
      case 1: 
       tv.setText("number one"); 
       break; 
      case 2: 
       tv.setText("number two"); 
       break; 
     } 

    } 

} 

/* 



     if (bundle != null) { 
      if (bundle.getBoolean("button1")) 
       changTextToWalla1("Selected", bundle.getInt("num")); 
     } 
    } 
---control class-- 
public class Control_Frag extends Fragment { 
    Communicator callback; 
    private Button button1; 
    private Button button2; 



    @Nullable 
    @Override 

    public void onAttach(Context context){ 
     super.onAttach(context); 
     callback= (Communicator) context; 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.frag_control,container,false); 
     button1 = (Button)v.findViewById(R.id.button_1); 
     button2 = (Button)v.findViewById(R.id.button_2); 

     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       callback.button1Clicked("button1", 1); 
      } 
     }); 

     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       callback.button2clicked("button2", 2); 
      } 
     }); 
     return v; 

    } 
----communicator--- 
public interface Communicator { 
    //here i write the functions that im going to use in the project - 
    //all the functions that move from one fragmant to another 
public void button1Clicked (String str1,int x); 
public void button2clicked (String str2,int x); 


} 
-----main--- 
public class MainActivity extends FragmentActivity implements Communicator{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (findViewById(R.id.small_device) != null) { 
      Control_Frag cf = new Control_Frag(); 

      FragmentManager fm = getSupportFragmentManager(); 
      fm.beginTransaction().add(R.id.small_device, cf , "cf").addToBackStack("cf").commit(); 
      // In small device it will show only Control_Frag to begin with 
     } 
    } 

    @Override 
    public void button1Clicked(String str1, int x) { 
     //When button 1 is pressed - change text to walla 1 
     // Identifying Map_Frag in Fragment Manager 
     Map_Frag ma = (Map_Frag) getSupportFragmentManager().findFragmentById(R.id.frag_content); 


     // TABLET 
     if(ma != null){ 
      ma.changTextToWalla1(str1 ,x); 
     } 
     else { 
      ma = new Map_Frag(); 
      Bundle bundle = new Bundle(); 
      bundle.putBoolean(str1, true); 
      bundle.putInt(str1 ,x); 
      ma.setArguments(bundle); 
      FragmentManager fm = getSupportFragmentManager(); 
      fm.beginTransaction().replace(R.id.small_device,ma,"sadf").addToBackStack("sadf").commit(); 
     } 
    } 



    @Override 
    public void button2clicked(String str2, int x) { 
     Map_Frag ma = (Map_Frag)getSupportFragmentManager().findFragmentById(R.id.frag_content); 

     if (ma != null) { 
      ma.changTextToWalla1(str2, x); 
     }else{ 
      ma = new Map_Frag(); 
      Bundle bundle = new Bundle(); 
      bundle.putBoolean(str2, true); 
      bundle.putInt(str2, x); 
      ma.setArguments(bundle); 
      FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.small_device,ma,"asd").addToBackStack("asd").commit(); 
     } 

    } 


} 
I could use some help to get it done 
+0

Добавить контрольные точки и запустить отладчик –

ответ

0

Почему вы не создать отдельный фрагмент XML с дизайном вы хотите, и просто вызвать фрагмент, когда пользователь решает переключаться между ними.

public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    Fragment fragment = null; 
    switch (position) { 
     case 0: 
      fragment = new Fragment1(); 
      break; 
     case 1: 
      fragment = new Fragment2(); 
      break; 
     default: 
      break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, fragment).commit(); 

    } 
} 

public void onSectionAttached(int number) { 
    switch (number) { 
     case 1: 

      mTitle = getString(R.string.title_section1); 
      break; 
     case 2: 
      mTitle = getString(R.string.title_section2); 
      break; 
     case 3: 
      mTitle = getString(R.string.title_section3); 
      break; 
    } 
} 
Смежные вопросы