What I want to do is to call one fragment from another on click on item of listview. Program builds without errors however it crash on click on item. Btw I’m making here local Fragment_3 object, I have got already done it in my mainactivity, how to pass it to this function?
Fragment_1.java content:
public class Fragment_1 extends SherlockFragment implements OnItemClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Fragment Fragment3 = new Fragment_3();
Integer fragmentId = (Integer) v.getTag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentId, Fragment3);
fragmentTransaction.commit();
}
}
My suspicion is that v.getTag() is null; So Integer fragmentId = (Integer) v.getTag(); will end up in fragmentId being null. When unboxing that to an int you'll get a NullPointerException. Why don't you replace fragmentId in fragmentTransaction.replace(fragmentId, Fragment3); with the id of the container that holds current instance of Fragment_1?
Related
When i replace fragment Listview place fragment Listview
I had trouble this in photo but i can't fix it .
i need to fix it,plz.
UI_screenshot
Code
#Override
public void onActivityCreated(Bundle savedInstanceState) {
final String[] titel = {"asdasd", "asda", "bdfg", "asdqweqwe"};
int idimg = R.mipmap.icon_main_list;
MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), titel, idimg);
setListAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
Anmalz anmalz=new Anmalz();
Main_List_Fragment main_list_fragment=new Main_List_Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, anmalz);
fragmentTransaction.remove(main_list_fragment).commit();
break;
Change your onCreateView() to -
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment_list, container, false);
}
Also, in onListItemClick(), you should not remove/replace the same fragment again and again. You should just refresh the list adapter.
I am using android PagerStrip Slidding tab. I have two fragments. In the first fragment I have a listview.I want to navigate to the second fragment wen i click on the listview item in the first fragment . How do i do this? Thanks.
Here is the OnClick Listener in Fragment1
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragmentcontainer,Fragment2.newInstance());
transaction.addToBackStack(null);
}
});
//Here is my Fragment 2 which i want to navigate to
public class Fragment2 extends Fragment {
private static final String ARG_POSITION = "position";
public static final String TAG = Fragment2.class
.getSimpleName();
#InjectView(R.id.textView)
TextView textView;
private int position;
public static Fragment2 newInstance(int position) {
Fragment2 f = new Fragment2();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
public static Fragment2 newInstance(){
Fragment2 f = new Fragment2();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
//setContentView(R.layout.activity_maps);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_card,container,false);
ButterKnife.inject(this, rootView);
ViewCompat.setElevation(rootView, 50);
textView.setText("CARD "+position);
return rootView;
}
}
I have figured out how to do this.
1. In the MainActivity your might already have
ViewPager pager;
and in your Oncreate method you also have
......
pager.setAdapter(adapter);
Now create the method below in the MainActivity below.ie(The Activity which controls all your fragments etc)
public void setCurrentItem (int position, boolean smoothScroll) {
pager.setCurrentItem(item,true);
}
Now Lets assume your fragment1 has a listview or button.And you want to navigate to fragment2 by onClick.
This is what you need to do
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
((MainActivity)getActivity()).setCurrentItem (1, true);
}
});
//Remeber int position is the position of the fragment you want to navigate to in your Adapter
When I click on my gridview item, it should replace the current fragement by the new fragment. It seems to happen but I don't see the new fragment. The new fragment is behind the other one.
I can't figure out why it's not working.
Any idea ?
Thanks
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.TypesFragment, container, true);
return rootView;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
typesList = FullDAO.GetTypesOfResto ();
GridView mGridView = Activity.FindViewById<GridView> (Resource.Id.list);
mGridView.Adapter = new TypeOfRestoAdapter (Activity, typesList);
mGridView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) {
OnGridItemClick((GridView)sender,args.Parent,args.Position,0);
};
}
public void OnGridItemClick(GridView l, View v, int index, long id)
{
//Get the selected type
TypeResto currentTypeResto = typesList[index];
// Create a new fragment and a transaction.
FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
RestoByTypeFragment aDifferentDetailsFrag = new RestoByTypeFragment();
aDifferentDetailsFrag.typeId = currentTypeResto.id;
// Replace the fragment that is in the View fragment_container (if applicable).
fragmentTx.Replace(Resource.Id.types_fragment, aDifferentDetailsFrag);
// Add the transaction to the back stack.
fragmentTx.AddToBackStack(null);
// Commit the transaction.
fragmentTx.Commit();
}
I am facing a problem in regarding fragment.
In my scenario,
There are two fragment associated with FragmentActivity.
In FragmentActivity, there are a container layout (Frame Layout) in which all fragment will replace.
public void replaceFragment(Fragment fragmentClass) {
String selectedFragment = fragmentClass.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction
.replace(R.id.content_frame, fragmentClass);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
First time , I set a List type fragment (Fragment A) in which get the data from web service and papulate over listview. I execute the AsyncTask from onCreateView() method.
In MainActivity: onCreate
SherlockFragment fragment = new FragmentA();
replaceFragment(fragment);
On list item click of Fragment A, Fragment A will callback the activity method to replace it to details type fragment Fragment B.
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
callback = (ICallBack) activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*View rootView = inflater.inflate(R.layout.fragment_locate, container,
false);*/
View rootView = inflater.inflate(R.layout.fragment_a, container,
false);
ListView list = (ListView) rootView
.findViewById(R.id.listView);
adapter = new MyListAdapter();
list.setAdapter(adapter);
list
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View convertView, int position, long id) {
SherlockFragment fragment = new SalonDetailFragment();
callback.replaceFragment(fragment);
}
});
ListDataTask task = new ListDataTask();
task.execute();
return rootView;
}
class ListDataTask extends AsynTask<Void,Void,List<Data>>{
public Void doInBackground(Void parems){
List<Data> = getDataFromServer();
}
onPostExecute(List<Data> data){
adapter.addAllData(data);
adapter.notifyDataSetChanged();
}
}
When I press back button, from Fragment B then Application will show Fragment A but it execute Asynctask again and get the data to papulate the listview.
So I need to know, How to maintain the pervious state of Fragment like Activity.
Is there are any way to not to create Fragment after come back from Other activity
Have a look my pseudo code.
I got solution. Simple.... do the check null value of rootview
public class FragmentA extends Fragment {
View _rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (_rootView == null) {
// Inflate the layout for this fragment
_rootView = inflater.inflate(R.layout.fragment_a, container, false);
// Find and setup subviews
_listView = (ListView)_rootView.findViewById(R.id.listView);
...
} else {
// Do not inflate the layout again.
// The returned View of onCreateView will be added into the fragment.
// However it is not allowed to be added twice even if the parent is same.
// So we must remove _rootView from the existing parent view group
// (it will be added back).
((ViewGroup)_rootView.getParent()).removeView(_rootView);
}
return _rootView
I have the same problem and solved by replacing
fragmentTransaction
.replace(R.id.content_frame, fragmentClass);
to
fragmentTransaction
.add(R.id.content_frame, fragmentClass);
Replace will always create new instance on back press while Add is just add a new fragment in Stack. for more information check this link
I want to replace a fragment containing a list of items with a new fragment depending on the item clicked. This is my code, how do I need to approach this? Should I declare my ListView, Adapter and such in onCreateView or onActivityCreated? The container is a ViewPager if that makes a difference.
public class PreferenceListFragment extends SherlockFragment
{
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout)inflater.inflate(R.layout.settings, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv = (ListView)getView().findViewById(R.id.SettingsLV);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Preferences.TITLES));
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("FragmentList", "Item clicked: " + arg2);
// Switch case that starts fragment depending on 'position'.
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
);
}
}
Link to some very weird behaviour from my app.
The setting of adapter and listview should be done in onActivityCreated. OnCreateView is just to draw the user interface for the first time. The processing should be in onActivityCreated(). Refer http://developer.android.com/guide/topics/fundamentals/fragments.html