Basil Battikhi Basil Battikhi - 2 months ago 11
Android Question

How to control a fragment from another one

I have written this code ,i have data on data base ,i want to sort listview by age when the button is clicked, the listView should be sorted again by age , now the methods work perfect but i can't resort the list view by age,here is the list fragment code :

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class FragmentList extends Fragment {
ListView lst;
DB db;
public static ArrayList<String> LIST;

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

lst = (ListView) rootView.findViewById(R.id.lstViewf);
db = new DB(getActivity());
LIST = new ArrayList<String>();

for (int i = 0; i < db.getAllContacts().size(); i++) {
LIST.add(db.getAllContacts().get(i).name.toLowerCase());

}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, LIST);
lst.setAdapter(adapter);

return rootView;
}
}


here is Radio fragment that i want to click on one of them to make a change

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class FragmentRadio extends Fragment {
RadioButton rad1, rad2, rad3;
RadioGroup radioGroup;
DB db;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_radio, container,
false);
db = new DB(getActivity());
rad1 = (RadioButton) rootView.findViewById(R.id.frad1);
rad2 = (RadioButton) rootView.findViewById(R.id.frad2);
rad3 = (RadioButton) rootView.findViewById(R.id.frad3);
radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (rad1.getId() == checkedId) {
Toast.makeText(getActivity(), "!", Toast.LENGTH_SHORT)
.show();

startActivity(new Intent(getActivity(), ListActivity.class));
}

else if (rad2.getId() == checkedId) {

Toast.makeText(getActivity(), "!", Toast.LENGTH_SHORT)
.show();

startActivity(new Intent(getActivity(), ListActivity.class));

// }

} else if (rad3.getId() == checkedId) {

}
}
});

return rootView;
}
}


finally here is the layout that contains the two fragments

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class ListActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
getSupportFragmentManager().beginTransaction()
.add(R.id.f1, new FragmentList()).commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.f2, new FragmentRadio()).commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


}


XML Files
Radio fragment



<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >

<RadioButton
android:id="@+id/frad1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sort By Name" />

<RadioButton
android:id="@+id/frad2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sort By age" />

<RadioButton
android:id="@+id/frad3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sort By ID" />
</RadioGroup>

</RelativeLayout>


here is the listfragment xml


<ListView
android:id="@+id/lstViewf"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Answer

You can set a TAG for the FragmentList as follow

getSupportFragmentManager().beginTransaction().add(R.id.f1, new FragmentList(),"YOUR_TAG").commit();

Then in FragmentRadio class you can get a reference to FragmentList as follow

FragmentList fragmentList = (FragmentList) getActivity().getSupportFragmentManager().findFragmentByTag("YOUR_TAG");

After that you can create sortList() method in your FragmentList class and call it directly from the other fragment fragmentList.sortList();

But i don't recommend the direct communication between fragments, it's better to create a callback interface and use the activity as intermediate between the two fragments.

Check this to learn how to implement the callback interface and use it to communicate betweem fragments http://developer.android.com/guide/components/fragments.html#EventCallbacks