简体   繁体   English

从非静态Java获取数据到静态

[英]Get Data to Static From NonStatic Java

public class RetrieveActivity extends Activity {
    static String dateData;
    static int choice ;



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

    public void showDatePickerDialogFrom(View v) 
    {   
        choice=1;
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }


    public void showDatePickerDialogTo(View v) 
        {   
        choice=2;
        String dateSelected;
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
        }



    public static void populateSetDate(int year,int month,int day)
    {
        dateData = ""+day+"/"+month+"/"+year;
        if(choice==1)
        {
        EditText dateFrom=(EditText)findViewById(R.id.dateTo); <------ error over here on the static to non static stuff.
        dateFrom.setText(dateData);
        }
        else
        {
        EditText dateTo=(EditText)findViewById(R.id.dateFrom1);<--- here also the same.
        dateTo.setText(dateData);
        }
    }

    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener 
    {


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) 
        {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) 
        {   
            populateSetDate(year, month+1,day);
        }

I tried to change the variable to static so that it doesn't give me error. 我试图将变量更改为static,以便它不会给我错误。 and then i did search on the web how fix this problem . 然后我在网上搜索了如何解决此问题。 Some did tell me that adding getActivity will fix the problem. 有些人确实告诉我,添加getActivity将解决此问题。 but it still giving me the same error. 但它仍然给我同样的错误。 anyone can help. 任何人都可以提供帮助。 Thanks 谢谢

First make your method populateSetDate and variables non-static. 首先,将您的方法populateSetDate和变量设为非静态。

And in fragment: 并在片段中:

 public void onDateSet(DatePicker view, int year, int month, int day) 
    {   
        ((RetrieveActivity)getActivity()).populateSetDate(year, month+1,day);
    }

A better solution would be to create an interface : 更好的解决方案是创建一个interface

interface Callbacack{
    public void populateSetDate(int year,int month,int day);
}

Make you activity implement it: 让您的活动实现它:

public class RetrieveActivity extends Activity implements Callback

Inside fragment: 内部片段:

public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener 
    {
    Callback callback;
    public void setCallback(Callback callback){
        this.callback = callback;
    }

And onDateSet onDateSet

 public void onDateSet(DatePicker view, int year, int month, int day) 
        {   
            callback.populateSetDate(year, month+1,day);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM