0

I want to get Textview info to fragment textview.

Mainactivity:

   public final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR, year);
                c.set(Calendar.MONTH, month);
                c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String format = new SimpleDateFormat("dd/MM/yyyy").format(c.getTime());
                datePicker1.setText(format);
Tab1_fragment.getInstance().setText(Integer.toString(difference(c.getTimeInMillis())));
             
            }
        };

Fragment:

public class Tab1_fragment extends Fragment {
        TextView betweendate;
        public void setText (String text){
            TextView betweendate = (TextView) this.getView().findViewById(R.id.betweenDate);
            betweendate.setText(text);
        }

Am I doing it correct? Result in log is:

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

T.Kopa
  • 13
  • 4
  • May I know why `datePickerListener` is not inside Fragment? – Ticherhaz FreePalestine Jul 04 '21 at 12:09
  • 2
    Your code is not clear enough to be helped. Though, by the title of the question is that your trying to pass data from an activity `MainActivity` to a fragment `Fragment`. Refer to this link https://stackoverflow.com/a/12739968/9942927. Hope it solves your problem. – 4xMafole Jul 04 '21 at 12:12
  • @TicherhazFreePalestine genarate of data must be in mainactivity and when i get this data in fragments (3), there are some additional actions. – T.Kopa Jul 04 '21 at 17:17
  • @4xMafole Thanks, I'll review it – T.Kopa Jul 04 '21 at 17:17

3 Answers3

0

you first define all view(textview and ....) in onCreateView (in fragment) and them initial view here is example :

 @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_compare_result, container, false);
    initView(view);
    return view;


}
private void initView(View view) {
    txtProductName = view.findViewById(R.id.txt_product_name);
    txtProductc = view.findViewById(R.id.txt_product_c);
    txtProductn = view.findViewById(R.id.txt_product_n);

    }

and to send data in activity to fragment you can use contracture fragment to get data or Intent

0

Simple implementation for beginners is like below:

You can access activity data from fragment:

Activity:

public class YourActivity extends Activity {

    private String myString = "hello";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ...
    }

    public String getDataLocal() {
        return myString;
    }
}

Fragment:

public class MyFragment extends Fragment {
    TextView tv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {=

        View view = inflater.inflate(R.layout.frgament_layout_view, container, false);
        tv = (TextView) view.findViewById(R.id.yout_text_view);
        
        return view;
    }

    @Override
    public View onViewCreated(..) {

        YourActivity activity = (YourActivity) getActivity();
        String myActivityData = activity.getDataLocal();
        tv.setText(myActivityData);
     
        return view;
    }
}

Hope this gives you basic idea of how to send data from Activity to fragment.


Second solution: From YourActivity

var fragment = supportFragmentManager.findFragmentById(YOUR_FRAG_ID) as MyFragment
fragment.updateMyData(YOUR_NEW_DATA);

Try this second solution it should work. :)

rasfarrf5
  • 219
  • 1
  • 5
0

It looks like you need to use Bundle, Assume that my Fragment called ResultFragment And for example i wanna pass a date.

    String date = "10/12/2000";

    Bundle bundle = new Bundle();
    bundle.putString("date", date);

    ResultFragment resultFragment = new ResultFragment();
    resultFragment.setArguments(bundle);
    resultFragment.show(this.getSupportFragmentManager(), "result fragment");

To get the data in your Fragment use this code below.

 Bundle bundle = this.getArguments();
   
    if (bundle != null) {

        String date = bundle.getString("date", null);
    }
Yechiel babani
  • 344
  • 3
  • 13