The listview must be inside the fragment, if you want to pass data to the fragment by clicking on different buttons you should do:
EDITED
IMPORTANT!
This class is only for example (it does't follow the best practices in android development).
public class MainActivity extends FragmentActivity implements View.OnClickListener{
public static final String LISTVIEW_DATA = "myData";
public static final String DATA_TYPE1 = "DataType1";
public static final String DATA_TYPE2 = "DataType2";
public static final String DATA_TYPE3 = "DataType3";
Bundle bundle = null;
ArrayList<String> items1 = new ArrayList<String>();
ArrayList<String> items2 = new ArrayList<String>();
ArrayList<String> items3 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set some data to the items
items1 = generateStringData("Unit 1");
items2 = generateStringData("Unit 2");
items3 = generateStringData("Unit 3");
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
private ArrayList<String> generateStringData(String prefix){
ArrayList<String> temp = new ArrayList<String>();
for (int i = 0; i<10; i++){
temp.add(prefix + i);
}
return temp;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE1);
bundle.putStringArrayList(LISTVIEW_DATA, items1);
break;
case R.id.button2:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE2);
bundle.putStringArrayList(LISTVIEW_DATA, items2);
break;
case R.id.button3:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE3);
bundle.putStringArrayList(LISTVIEW_DATA, items3);
break;
default:
break;
}
setListFragment(bundle);
}
private void setListFragment(Bundle arguments){
Fragment fragment = new MainActivityFragment();
if (arguments != null){
fragment.setArguments(arguments);
}
FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack("");
fragmentTransaction.commit();
}
When you click over button this code will put fragment over fragment over fragmen as you can see by tapping the back button, this is not good idea. You should refartor the code by your needs.
After setting the arguments to the fragment you must receive the data inside the fragment:
public class MainActivityFragment extends Fragment {
public final static String DATA_TYPE = "DataType";
private ArrayList<String> data = new ArrayList<>();
private String dataTag = "";
public MainActivityFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null){
data = getArguments().getStringArrayList(MainActivity.LISTVIEW_DATA);
dataTag = getArguments().getString(DATA_TYPE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (dataTag) {
case MainActivity.DATA_TYPE1:
//here is where you can implement your logic for type 1
break;
case MainActivity.DATA_TYPE2:
//here is where you can implement your logic for type 2
break;
case MainActivity.DATA_TYPE3:
//here is where you can implement your logic for type 3
break;
default:
//do something else
break;
}
}
});
return rootView;
}
Hope it helps.