I'm passing value from adapter to fragment class,
Here adapter class,
rbFolder.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(selected != null)
{
selected.setChecked(false);
String meee = data.get(position).getTypeId();
System.out.println("*******so*inside********"+meee);
}
rbFolder.setChecked(true);
String so = data.get(position).getTypeId();
System.out.println("*******so*********"+so);
selected = rbFolder;
System.out.println("********selected*******"+selected);
Fragment homepage = new Fragment();
FragmentTransaction fragmentManager =((FragmentActivity)context).getSupportFragmentManager()
.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("name", so); //key and value
System.out.println("*****venki***meee*******"+so);
homepage.setArguments(bundle);
// fragmentManager.replace(R.id.content_frame, homepage);
fragmentManager.addToBackStack(null);
fragmentManager.commit();
}
});
In this, putstring working well but in my fragemt class i didnt receive values. I'm trouble in this place.
Here Fragmnet class,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootview != null) {
ViewGroup parent = (ViewGroup) rootview.getParent();
if (parent != null)
parent.removeView(rootview);
}
try {
rootview = inflater.inflate(R.layout.homepage, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
Here I get values from adapter but this is not working,
Bundle bundle = ((Activity)context).getIntent().getExtras();
if (bundle != null)
{
String strtext=getArguments().getString("name");
System.out.println("*******strtext*********"+strtext);
}
return rootview;
}
First of All you have to call your HomeFragment() instead of fragment like that
Fragment homepage = new HomeFragment();
FragmentTransaction fragmentManager =((FragmentActivity)context).getSupportFragmentManager()
.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("name", so); //key and value
homepage.setArguments(bundle);
fragmentManager.replace(R.id.content_frame, homepage);
fragmentManager.addToBackStack(null);
fragmentManager.commit();
When you want to get value inside home fragment then
if( getArguments() != null)
String strtext = getArguments().getString("name");
follow steps one by one, copy Context context;
List rowItems;
onItemClickListner onItemClickListner; and pest into your adapter
then copy two constructor methods and then pest on item click method to pass data to fragment.
in your fragment class set adapter item on click listener code in this way...
madapter.setOnClickListener(new MyListAdapterTemp.onItemClickListner() {
#Override
public void onClick(String str) {
}
});
Context context;
List<String> rowItems;
onItemClickListner onItemClickListner;
public MyListAdapterTemp(Context context, List<String> items) {
this.context = context;
this.rowItems = items;
}
public void setOnClickListener(onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
holder.txtDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListner.onClick(rowItems.get(position).toString().trim().toUpperCase());
}
});
this method works fine on my app and for activity to fragment , fragment to activity we can pass data using interface but not this
way this is best and proper way to perform this type of operations.
Fragment homepage = new Fragment();
this is supposed to be your Fragment class name -
Fragment homepage = new MyFragment();
And Your Fragment be like -
public class MyFragment extends Fragment {
#Override
public void onCreate(...) {
Bundle b = getArguments(); //To get extras from Fragment
}
}
Other than that,
Bundle bundle = ((Activity)context).getIntent().getExtras();
This is used for an Activity. For Fragment you have to use getArguments()
Related
I pass my data as a parcelableArrayList from Activity-->Adapter-->Fragment. In my static newInstance() method, I'm instantiating a Fragment and setting its arguments. I check if the arguments are set or not in the newInstance() method itself. When I try to access the data in my onCreate(), I get a null Pointer Exception.
Adapter where I'm passing the data to fragments:
#Override
public Fragment getItem(int position){
switch(position){
case 0:
return StatusFragment.newInstance("Hahahahah, Dumbfuck");
case 1:
return GraphListFragment.newInstance(position, tempBundle);
case 2:
return GraphListFragment.newInstance(position, humBundle);
case 3:
return GraphListFragment.newInstance(position, pressureBundle);
default:
return GraphFragment.newInstance(1);
}
}
My static newInstance() method:
public static Fragment newInstance(int position, Bundle savedInstanceState){
GraphListFragment graphListFragmentInstance = new GraphListFragment();
if(savedInstanceState != null){
Bundle bundle =new Bundle();
bundle.putParcelableArrayList("list", savedInstanceState.getParcelableArrayList("list"));
graphListFragmentInstance.setArguments(bundle);
Log.d("==============>>>>", graphListFragmentInstance.getArguments().toString());
}
return new GraphListFragment();
}
Here's the Log result:
D/==============>>>>: Bundle[{list=[com.ishanvadwala.cmpe295b.Model.TemperatureData#8da04a0, com.ishanvadwala.cmpe295b.Model.TemperatureData#5fce859, com.ishanvadwala.cmpe295b.Model.TemperatureData#9503c1e, com.ishanvadwala.cmpe295b.Model.TemperatureData#6aeecff, ....
Here's my onCreate() where I'm trying to access the data:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate====>>>>", getArguments().toString());
/*
List<?> list = (ArrayList<? extends Parcelable>) getArguments().getParcelableArrayList("list");
if(list.get(0) instanceof TemperatureData){
for(TemperatureData tempData: (List<TemperatureData>)list){
System.out.println("I got this: "+tempData.getTemperature());
}
}
*/
}
I've looked at several StackOverflow posts and I still have no idea what I'm doing wrong.
Replace
return new GraphListFragment();
With
return graphListFragmentInstance;
That's the actual Fragment with the Bundle!
Also, creating a new Bundle is useless. Just set the one you are passing
if(savedInstanceState != null){
// Bundle bundle =new Bundle();
// bundle.putParcelableArrayList("list", savedInstanceState.getParcelableArrayList("list"));
graphListFragmentInstance.setArguments(savedInstanceState);
Log.d("==============>>>>", savedInstance.toString());
}
return graphListFragmentInstance;
How to pass args to fragment
SET
Bundle bundle = new Bundle();
bundle.putString("Video1", "Sosis");
bundle.putString("Video2", "Dumb");
Fragment fragment = new YourFragment();
fragment.setArguments(bundle);
// Attach the fragment
GET
public static class YourFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (getArguments() != null)
{
Log.e(getArguments().getString("Video1", ""));
Log.e(getArguments().getString("Video2", ""));
}
}
}
I am trying to send data from Fragment A to Fragment B of NAVIGATION Drawer on Button click.I tried with bundle and intent but both of them are not working.
In Fragment A I have editText and button when I click the data is passed to another fragment.
In Fragment B there is textView where editText data is going to show but I am not getting a way to communicate between fragment in Navigation Drawer
When lauching Fragment from first fragment
Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);
Fragment fragment = new SECONDFragment();
if (arguments != null) {
fragment.setArguments(arguments);
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
And in SecondFragment
private String mData;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString("key");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.YourLayout, container, false);
TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
text.setText(mData);
return rootView;
}
Let your Activity to do the communication.
Take a public static variable in your MainActivity from where you're controlling the Fragment replaces of the navigation drawer. When you click the button in FragmentA store the value in the EditText to the public static variable of your MainActivity. Then when FragmentB is loaded check if the public static value is null or not. If not null place the value of that variable to your desired position.
This is not an elegant way for passing values between fragments, but in your case it'll work just fine.
If you're looking for how to pass values from one fragment to another, try something like this.
// To pass some value from FragmentA
FragmentB mFragmentB = new FragmentB();
Bundle args = new Bundle();
args.putString("VALUE", value);
mFragmentB.setArguments(args);
And from your FragmentB use the code to get the values passed.
Bundle args = getArguments();
int value = args.getString("VALUE");
1- create Application Class
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
private static MyApplication mInstance;
public static synchronized MyApplication getInstance() {
return mInstance;
}
String mytext;
public String getMytext() {
return mytext;
}
public void setMytext(String mytext) {
this.mytext = mytext;
}
}
2- app name tag in manifast
<application
android:name=".MyApplication"
.......
3- from first Fragment
MyApplication.getInstance().setMytext("your text here");
4- from other Fragment
String text=MyApplication.getInstance().getMytext();
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("KEY", "VALUE");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView of the new Fragment:
//Retrieve the value
String value = getArguments().getString("KEY");
I am Using one String [] to display in ListView of fragmentone and pass String[] to fragmentTwo which has listView. my tried Codes below,
MainActivity:
public class MainActivity extends FragmentActivity implements ListInterface {
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
fragmentOne = new FragmentOne ();
fragmentTwo = new FragmentTwo ();
FragmentManager fragmentManager = getSupportFragmentManager ();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
fragmentTransaction.add (R.id.frame_one,fragmentOne);
fragmentTransaction.add (R.id.frame_two , fragmentTwo);
fragmentTransaction.commit ();
}
#Override
public void getValue (String[] s) {
fragmentTwo.setValue (s);
}}
FragmentOne:
public class FragmentOne extends Fragment {
private ListView listView;
private ListInterface listInterface;
String [] listData = {"Dhana","Rahul","Strobs","Uday","Selvi"};
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.fragment_one,container,false);
listView = (ListView)view.findViewById (R.id.lst_view);
ArrayAdapter adapter = new ArrayAdapter (getActivity (),R.layout.fragment_one,listData);
listView.setAdapter (adapter);
return view;
}
#Override
public void onAttach (Context context) {
super.onAttach (context);
if(context instanceof ListInterface){
listInterface =(ListInterface)context;
listInterface.getValue (listData);
}else{
throw new ClassCastException (context.toString ()+"mess ");
}
}}
FragmentTwo:
public class FragmentTwo extends Fragment {
private ListView listView;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.fragment_two,container,false);
listView = (ListView)view.findViewById (R.id.lst_view_two);
return view;
}
public void setValue (String [] value){
ArrayAdapter adapter = new ArrayAdapter (getActivity (),R.layout.fragment_two,value);
listView.setAdapter (adapter);
}}
ListInterface:
public interface ListInterface {
public void getValue (String [] s);}
i tried my codes it show
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
FragmentTwo.setValue(FragmentTwo.java:27)
MainActivity.getValue(MainActivity.java:31)
FragmentOne.onAttach(FragmentOne.java:37)
You can set the data in this way.
public static BuyerToyFragment newInstance(String yourText) {
FragmentOne fragment = new FragmentOne ();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, yourText);
fragment.setArguments(args);
return fragment;
}
NOw in fragmentOne you need to get data like this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
String myStringData = getArguments().getString(ARG_PARAM1);
}
}
and in your MainActivity you can set data like this.
// you can add string array as well. i dont remember the tag exactely. i its
//like putStingArray() but you need to check it yourself
fragmentOne = new FragmentOne ().newInstance("Your data goes here");
you need to do the same for fragment 2
I don't really understand the role of your list interface.
I don't really understand neither why you make your setValue and getValue call statically...
1/The easiest way is to store your list in your activity and then access it from the fragment, for exemple in onViewCreated.
Activity:
public String[] getMyList() {
return mMyList;
}
You will also need to do the setter to set the list from another fragment.
Access your list from the fragment:
String[] listFromActivity = ((MainActivity) getActivity()).getMyList();
2/A cleaner way could be to pass your list as an argument to your fragment.
In activity :
FragmentOne frO = new FragmentOne();
Bundle bundle = new Bundle();
bundle.putStringArray(mMyList, "my_list_key");
frO.setArguments(bundle);
In fragment :
String[] listFromActivity = getArguments().getStringArray("my_list_key");
The solution 1 is more easy if you want to update your list, if your list won't change the solution 2 is cleaner.
Basically I have my fragment
public class FragmentDashboard extends Fragment {
public static FragmentDashboard newInstance() {
FragmentDashboard frag = new FragmentDashboard();
return frag;
}
public void updateData(Object object){
myTextView.setText(object.getField);
//update views with object values
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
myTextView = (TextView) view.findViewById(R.id.myTextView );
}
}
Then in my activity I need to update data I do:
FragmentDashboard fragmentDashboard = (FragmentDashboard) getSupportFragmentManager().findFragmentById(R.id.layFragment);
fragmentDashboard.updateData(myObject)
This works good if I am making the call on my activity after the Fragment has been displayed (like from an asynctask on completed).
The problem I am running into is having to run the updateData right after I add the fragment on my activity's onCreate().
final FragmentTransaction ft = fragmentManager.beginTransaction();
FragmentDashboard fragmentDashboard = FragmentDashboard.newInstance();
ft.replace(R.id.layFragment, fragmentDashboard);
ft.commit();
fragmentDashboard.updateData(myObject)
Running this I get a NPE because the fragment's onCreateView hasn't been run yet and the myTextView is not initialized
I don't want to add parameters on newInstance as I'd like to avoid making the object as parcelable. Any ideas ?
You can use a local field to contains your data and use it in your onCreateView method :
public class FragmentDashboard extends Fragment {
private Object myData=null;
private TextView myTextView = null;
public static FragmentDashboard newInstance() {
FragmentDashboard frag = new FragmentDashboard();
return frag;
}
public void updateData(Object object){
myData = object;
if(myTextView != null)
myTextView.setText(myData);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
myTextView = (TextView) view.findViewById(R.id.myTextView );
if(myData != null && myTextView != null)
myTextView.setText(myData);
}
}
It isn't a good practice to set Data like updateData(Object ) . Make your model class parcelable or serializable and pass it in putExtra and get it in onViewCreated.
public static FragmentDashboard newInstance(Object object) {
Bundle args = new Bundle();
args.putParcelable("yourModelClass",object);
FragmentDashboard frag = new FragmentDashboard();
frag.setArguments(args);
return frag;
}
And in onViewCreated
if(getArguments != null)
yourModelClassObject = getArguments().getParcelable("yourModelClass");
if(yourModelClassObject != null)
textView.setText(yourModelClassObject.getField());
I have written code orally . May contain mistakes.
It's small Android 2.2 test application using the Compatibility Package. I am trying to update the textview on another fragment on another activity on list item selection. But problem is that everytime the first click returns nullpointer exception and only on the second try its text is changing. I would like to know why is the happening and what is a good solution.
ListActivity:-
public class ListActivity extends FragmentActivity implements
ListFragment.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
}
#Override
public void onItemSelected(int index) {
// TODO Auto-generated method stub
// Check to see if there is a frame in which to embed the
// detail fragment directly in the containing UI.
View detailsFrame = findViewById(R.id.detailcontainer);
if (detailsFrame != null
&& detailsFrame.getVisibility() == View.VISIBLE) {
DetailFragment detailFragment = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailcontainer);
if (detailFragment == null) {
detailFragment = new DetailFragment();
}
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
getSupportFragmentManager().beginTransaction()
.replace(R.id.detailcontainer, detailFragment).commit();
detailFragment.setTextView(index);
} else {
// Otherwise we need to launch a new activity to display
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
ListFragment :-
public class ListFragment extends Fragment {
private OnItemSelectedListener listener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] countries = new String[] { "India", "Pakistan", "Sri Lanka",
"China", "Bangladesh", "Nepal", "Afghanistan", "North Korea",
"South Korea", "Japan" };
View view = inflater.inflate(R.layout.list_fragment, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
// Populate list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_list_item_1,
countries);
listView.setAdapter(adapter);
// operation to do when an item is clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "ListItem Number " + position,
Toast.LENGTH_SHORT).show();
listener.onItemSelected(position);
}
});
return view;
}
// Container Activity must implement this interface
public interface OnItemSelectedListener {
public void onItemSelected(int index);
}
// To ensure that the host activity implements this interface
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet ListFragment.OnItemSelectedListener");
}
}
public void operation(int index) {
listener.onItemSelected(index);
}
}
Detail Activity :-
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DetailFragment details;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
details = new DetailFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, details).commit();
}
Bundle extras = getIntent().getExtras();
int index = extras.getInt("index");
try {
details = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailfragment);
details.setTextView(index);
} catch (NullPointerException ex) {
ex.getStackTrace();
}
}
}
DetailFragment :-
public class DetailFragment extends Fragment {
String[] capitals;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null)
return null;
capitals = new String[] { "delhi", "karachi", "colombo", "beijing",
"dhaka", "katmandu", "Afghanistan", "pyongyang", "seoul",
"tokyo" };
View v = inflater.inflate(R.layout.detail_fragment, container, false);
return v;
}
public void setTextView(int index) {
try {
TextView view = (TextView) getView().findViewById(R.id.detailView);
view.setText(capitals[index]);
} catch (NullPointerException ex) {
ex.getStackTrace();
}
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
}
Update:- I am adding the detailFragment xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/lightblue"
android:orientation="vertical" >
<TextView
android:id="#+id/detailView"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/detail_frag" />
</RelativeLayout>
Instead of doing details.setTextView(index) in DetailActivity , set the value of TextView in the DetailFragment's onActivityCreated while passing the value to be set in fragments setArgument method in the Detailactivity...
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras()); // pass the value of text view here
And In fragment onActivityCreated get that value via getArguments() and set it in textview..
EDIT for sending value Selected to loaded Fragment
In List Activity
detailFragment = getFragmentbyTag
if(detailFragment == null)
Create Fragment and Add it and Set Arguments here as well
else
detailFragment.setTextView(value); // fragment already loaded no need to set arguments
if you want to replace each time and not add once and use the added/loaded fragment , use setarguments each time.... and remove previous fragment and add new fragment( with Arguments) But added once and reused is preferred instead of removing and adding/ replacing on each click
Updated2
Just initialize your TextView in onCreateView() no need to create DetailFragment with parametrize just put in comment
Check the details object whether its null or not if null initialize as you do and then called setTextView()
if(details==null)
details = new DetailFragment(index);
else
setTextView(index);
TextView inside onCreateView() and used their
public class DetailFragment extends Fragment {
String[] capitals;
int index;
private TextView textView;
public DetailFragment(int index){
this.index = index;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null)
return null;
capitals = new String[] { "delhi", "karachi", "colombo", "beijing",
"dhaka", "katmandu", "Afghanistan", "pyongyang", "seoul",
"tokyo" };
View v = inflater.inflate(R.layout.detail_fragment, container, false);
// Initialize textView
textView = (TextView) v.findViewById(R.id.detailView);
setTextView(index); // set value value here for first time
return v;
}
public void setTextView(int index) {
if(textView!=null)
textView.setText(capitals[index]);
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
}
did you tried using this
TextView view = (TextView) findViewById(R.id.detailView);
instead of this
TextView view = (TextView) getView().findViewById(R.id.detailView);