Working with fragment class - java

Hello i want to working with fragment class.
in this code what to write???
in the myDbHelper = new DataBaseHelper(???????);
public class Tozihat extends Fragment{
DataBaseHelper myDbHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tozihat, container, false);
myDbHelper = new DataBaseHelper();
return view;
}
}

Use getActivity(); as Context if it is Fragment
myDbHelper = new DataBaseHelper(getActivity());

You need Activity's Context. So in Fragment you have to get is using getActivity().
So pass it as follows
myDbHelper = new DataBaseHelper(getActivity());

Related

How to pass data from Fragment to Tablayout fragment

This is will run fragment in fragment that use viewpageAdapter.I want to pass data from fragment to tablayout fragment. i have try any method and still crash. i just want to passs once data to fragment in the tablayout.
First fragment. this line i want pass to tablayout fragment:-
specItem = getArguments().getString("spec_item");
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageUrl = getArguments().getString("image_url");
priceItem = getArguments().getString("price_item");
specItem = getArguments().getString("spec_item");
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_details_item, container, false);
textPrice = (TextView) rootView.findViewById(R.id.textPrice);
image_Details = (ImageView) rootView.findViewById(R.id.image_Detail);
tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_id);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpage_id);
textPrice.setText(priceItem);
Picasso.get().load(imageUrl).into(image_Details);
ViewPageAdapter adapter = new ViewPageAdapter(getActivity().getSupportFragmentManager());
adapter.AddFragment(new FragmentSpecifications(), "Specification");
adapter.AddFragment(new FragmentReview(), "Review");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
Second fragment in tablayout, i dont know how to recieve:-
View rootView;
TextView textSpec;
String specItem;
public FragmentSpecifications() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_specification, container, false);
textSpec = (TextView) rootView.findViewById(R.id.textviewspec);
textSpec.setText();
return rootView;
}
Inside your FragmentSpecifications create a instance of the Fragment to send data to it, like this:
public static FragmentSpecifications newInstance(String priceItem) {
Bundle args = new Bundle();
args.putString("priceItem", priceItem);
FragmentSpecifications fragment = new FragmentSpecifications();
fragment.setArguments(args);
return fragment;
}
After that in same FragmentSpecifications override onCreate method and get value:
private String priceItem;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null)
priceItem = getArguments().getString("priceItem");
}
And finally when you add FragmentSpecifications do adapter pass the value:
adapter.AddFragment(FragmentSpecifications.newInstance(priceItem), "Specification");

problem with removing previous view in a fragment

i have created a fragment in android that contain 2 functions, the startview() is called at the beginning when the fragment is called, then when user click a button childfragment() is called. and at that point it should have remove the view from the startview() and then replacing it with the newview from childfragment(), and thats what i thought it would do. But instead it did not remove the old view and place the newview on top of the old view. even thought i already place mContainer.removeAllViews(); it did not remove the old view. what am i missing? please help.
here is the code
#SuppressLint("ValidFragment")
public class FragmentSideContent extends android.app.Fragment {
//private OnFragmentInteractionListener mListener;
Context context;
View view,trueview;
ListView listView;
ListAdapter listAdapter;
private LayoutInflater mInflater;
private ViewGroup mContainer;
int[] image = {1,2,3};
String[] titleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
String[] subTitleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
public FragmentSideContent(Context context) {
this.context = context;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
mInflater = inflater;
mContainer = container;
startview();
return trueview;
}
public void startview(){
view = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
listView = (ListView) view.findViewById(R.id.noteList);
listAdapter = new ListAdapter(context,image,titleText,subTitleText);
listView.setAdapter(listAdapter);
trueview = view;
}
public void childfragment(){
mContainer.removeAllViews();
View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
Fragment childFragment = new FragmentList(context);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
mContainer.addView(newview);
mContainer.addView(trueview);
trueview = newview;
}
}
I recommend you put in your fragment layout some GroupView for do this.
fragment_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
in yout Fragment.java
View view;
Framelayout fContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_layout, container, false);
return startView();
}
public View startview(){
fContainer = (Framelayout) view.findViewById(R.id.container);
listView = (ListView) view.findViewById(R.id.noteList);
listAdapter = new ListAdapter(context,image,titleText,subTitleText);
listView.setAdapter(listAdapter);
return view;
}
public void childfragment(){
fContainer.removeAllViews();
Fragment childFragment = new FragmentList(context);
FragmentTransaction transaction =
getChildFragmentManager().beginTransaction();
transaction.replace(R.id.container, childFragment).commit();
}
Now in your oncreateview of the other fragment (FragmentList) put the view that you want inflate.
FragmentList.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
return newview;
}

null reference on getArguments

I have a MainActivity (extends Activity) and the a FooFragment (extends Fragment).
Activity uses newFooInstance method to instantiate FooFragment and set some arguments to it:
public static FooFragment newInstance(String label) {
FooFragment fooFragment = new FooFragment();
Bundle args = new Bundle();
args.putString("fooLabel", label);
fooFragment.setArguments(args);
return fooFragment;
}
Now, inside FooFragments onCreate and onCreateView I do:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fooLabel = getArguments().getString("fooLabel"); // null error
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_balanse_period, container, false
);
TextView fooView = (TextView) rootView.findViewById(R.id.fooLabel);
fooView.setText(fooLabel);
return rootView;
}
And it produces:
Attempt to invoke virtual method java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String) on a null object reference*
Any idea what am I doing wrong?
EDIT:
Defining the main activity as:
public class MainActivity extends AppCompatActivity {
Then instantiated fooFragment as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragments = new ArrayList<>();
fragments.add(newInstance("bar"));
// Instantiate a ViewPager and PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new FooPagerAdapter(getSupportFragmentManager(), fragments);
I have found the root of evil.
Inside FooPagerAdapter the getItem was written wrong.
Wrong:
#Override
public Fragment getItem(int position) {
return new FooFragment();
}
Correct:
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}

Button does not respond

Ive got a Button in one of my Fragments. But it seems to be "not-clickable". There is no Log.d. Message when I click the Button (In another Fragment (Same onclicklistenercode) everything is fine.
For greater overview I added the whole Class and the part of my layout file which defindes the button.
public class ListViewFragment extends Fragment {
DbHelper mydb;
Button buttondeletedb;
Button buttonexport;
private EditText roomnr;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
Context context = getContext();
mydb = new DbHelper(context);
buttondeletedb = (Button) view.findViewById(R.id.button_deletelist_list);
buttonexport = (Button) view.findViewById(R.id.button_export);
String dataList = mydb.getAllElements();
String [] dataListArray = dataList.split("\n");
List<String> dataListFinal = new ArrayList<>(Arrays.asList(dataListArray));
ArrayAdapter<String> dataListAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_datalist,
R.id.list_item_datalist_textview,
dataListFinal);
View rootView=inflater.inflate(R.layout.fragment_listview, container, false);
ListView dataListListView = (ListView) rootView.findViewById(R.id.datalist);
dataListListView.setAdapter(dataListAdapter);
buttondeletedb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("buttondeletelist", "clicked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
mydb.deleteAll();
}
});
return rootView;
}
}
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/deletelist"
android:id="#+id/button_deletelist_list"
android:layout_gravity="center_horizontal" />
You can not click the button because the root view of Fragment is not view, you returned rootView. The button is child view of view.
Change return rootView; to return view;.
It should work
//Remove rootView and return view instead of rootView
public class ListViewFragment extends Fragment {
DbHelper mydb;
Button buttondeletedb;
Button buttonexport;
private EditText roomnr;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
Context context = getContext();
mydb = new DbHelper(context);
buttondeletedb = (Button) view.findViewById(R.id.button_deletelist_list);
buttonexport = (Button) view.findViewById(R.id.button_export);
String dataList = mydb.getAllElements();
String [] dataListArray = dataList.split("\n");
List<String> dataListFinal = new ArrayList<>(Arrays.asList(dataListArray));
ArrayAdapter<String> dataListAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_datalist,
R.id.list_item_datalist_textview,
dataListFinal);
// Change made here. replace view instead of rootView and remove rootView
ListView dataListListView = (ListView) view.findViewById(R.id.datalist);
dataListListView.setAdapter(dataListAdapter);
buttondeletedb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("buttondeletelist", "clicked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
mydb.deleteAll();
}
});
// Replace rootView with view.
return view;
}
}
Link between XML file and Class file of the Element (Button) is not there.Add this line in your onCreateView()
Button buttondeletedb = (Button) view.findViewById(R.id.buttonid);

Parse.com queryAdapter data Fragment not showing up on actionBar tabs

I'm using the Parse.com query adapter to populate my listview fragment. The data seems to load when I run it, but it only shows lines, not the data.
Also, the data loads perfectly fine when I use the adapter within a listview Activity.
public class imboard extends FragmentActivity implements ActionBar.TabListener {
#Override
public android.app.Fragment getItem(int position) {
return MyFavs.FavInstance();
}
}
public class MyFavs extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFavView = inflater.inflate(R.layout.fragment_favorites, container, false);
ListView listView = (ListView) myFavView.findViewById(R.id.favList);
FavoriteAdapter favoriteAdapter = new FavoriteAdapter(getActivity());
ParseQueryAdapter<CladPost> mainAdapter =
new ParseQueryAdapter<>(getActivity(), CladPost.class);
mainAdapter.setTextKey("title");
listView.setAdapter(mainAdapter);
favoriteAdapter.loadObjects();
return myFavView;
}
}

Categories

Resources