2017-10-13 193 views
-2

我很難理解靜態變量。任何人都可以解釋爲什麼當我在FragmentJoy中調用變量'startingDate'時,它不會返回正確的值嗎?假設所有其他的代碼工作正常,而不是NULL值和databaseCategories工作正常。如何在'onDataChange()'方法內保存一個值並在FragmentJoy中使用它?從一個類訪問靜態變量到另一個類

我無法刪除:

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

這是儀表盤活動

public class Dashboard extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

static String startingDate; 
DatabaseReference databaseCategories; 
ArrayList<Category> currentJoyCategories; 


User currentUser; //holds the information of the current user 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dashboard); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    . . . . . . 

    databaseCategories = FirebaseDatabase.getInstance().getReference("Categories"); 

    currentJoyCategories = new ArrayList<>(); 

    databaseCategories.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 

       //want to do 
       startingDate = "some string"; 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
} //end of Dashboard class 

在另一類我想做的事:

public class FragmentJoy extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_joy,container,false); 

     //not storing the string "some string" 
     System.out.println("testing " + Dashboard.startingDate); 

     return view; 

} //end of onCreateView method 
} //end of class 
+0

更好的方法是爲此創建接口。 –

回答

2

您必須聲明你startingDatepublic static String startingDate

使用本

public static String startingDate; 

,而不是這個

static String startingDate; 

然後你可以使用它在你的FragmentJoy這樣

Log.e("testing ",Dashboard.startingDate); 
+0

好的,但你確定通常是一個好主意,像這樣公開類級非常量變量? –

+0

@TimBiegeleisen nop –

+0

其返回NULL仍然 – Lazaro

0

在這裏,我向您展示如何使用一個class的靜態方法給另一個cla SS。

例如我在HomeFragment類中定義一個靜態方法,

公共類HomeFragment擴展BaseFragment {

Unbinder unbinder; 
    int mode; 

    public static HomeFragment getInstance(int mode) { 
     HomeFragment homeFragment = new HomeFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putInt(KeyUtil.KEY_MODE, mode); 
     homeFragment.setArguments(bundle); 
     return homeFragment; 
    } 
    .................... 
} 

而且,我已經在我的MainActivity課堂上使用這種static方法,

public class MainActivity extends AppCompatActivity { 

    ........................... 

     /** 
     * Get the fragment while need to push. 
     * 
     * @return: fragment 
     */ 
     private Fragment openFragment() { 
      switch (navItemIndex) { 
       case 0: 
        // Here I have called static method 
        return HomeFragment.getInstance(0); 
       } 
     } 

     ........................ 
    }