I have an Activity
with a ViewPager
. I also have a Dialog
that will retrieve a list with some items that the user will choose. Now, how can I update the Fragment
where the Items are supposed to display as new views attached to this Fragment?
Here is my ViewPager
's adapter:
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 1;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
default:
return null;
}
}
}
Now, here is the Fragment
:
public class Fragment1 extends Fragment{
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public ViewGroup onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup view = (ViewGroup)inflater.inflate(R.layout.screen1, container, false);
}
return view;
}
}
This is the method that I want to use to add Items to my Fragment
private void addItem() {
// Instantiate a new "row" view.
final ViewGroup[] newViews = new ViewGroup[selectedItems.length];
for(int i = 0; i < selectedItems.length; i++){
newViews[i] = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.animated_layout_item, mContainerView, false);
// Set the text in each new row.
((TextView) newViews[i].findViewById(android.R.id.text1)).setText(selectedItems[i]);
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newViews[i], 0);
// Set a click listener for the "X" button in the row that will remove the row.
OnClickListener listener = new MyAddItemListener(newViews[i], mContainerView, this);
newViews[i].findViewById(R.id.delete_button).setOnClickListener(listener);
}
}