|
||
|
|
|
|||||||
| Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android |
![]() |
|
|
Herramientas |
|
#1
|
||||
|
||||
|
Pasar datos desde Activity a Fragment
Buenas.
Hace bastante que no entro en el foro y he retomado esto de Android despues de un tiempo y acudo a vosotros a ver si me echais una mano para ver donde esta el error. Quiero pasar dato desde una Activity a un Fragment pero no me deja. Error en esta linea : transaction.add(R.id.fragment_container, fragment1).commit(); -> no puede resolver el metodo add. DOnde esta el error? a la hora de instanciar Fragment1 ? hay algo que no cuadra. MainActivity: Código:
public class MainActivity extends FragmentActivity {
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Traigo el fragment
Fragment1 fragment1= new Fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment1).commit();
Bundle data = new Bundle();
data.putString("dato", "lo que sea");
fragment1.setArguments(data);
}
Código:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc"
android:layout_weight="1"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#eee"
android:orientation="vertical">
<fragment android:name="com.example.chukk.fragmentpassingdata.Fragment2"
android:id="@+id/frag_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
Código:
public class Fragment1 extends Fragment {
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
String data =getArguments().getString("dato");
Toast.makeText(getActivity(),data, Toast.LENGTH_LONG).show();
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment1, null);
return root;
}
Código:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass data" />
</LinearLayout>
|
|
|
|
#2
|
||||
|
||||
|
sorry.
Se me olvido incluir la librería suppor v4 en las dependencias. solucionado |
![]() |
Estás aquí
|
||||||
|
||||||