2017-04-25 58 views
-5

我需要在圖像上傳操作啓動到數據庫時在我的用戶界面中設置當前日期。我在Adapter上的上傳按鈕代碼和片段上的方法。我怎樣才能調用代碼?再一次,我只需要在我的應用程序中顯示當前日期。謝謝如何在TextView中設置當前日期

enter image description here

public MyViewHolder(View view) { 
     super(view); 

     input_tgl = (TextView) view.findViewById(R.id.input_tgl_terima); 
     String dateApp = new SimpleDateFormat("dd-MMM-yyyy").format(new Date()); 
     final String strDateApp = dateApp.replaceAll("-", " "); 

回答

0

查找

Calendar c = Calendar.getInstance(); 
String formattedDate = df.format(c.getTime()); 

yourTextview.setText(formattedDate); 
0

所以基本上你想從適配器類調用片段方法的解決方案,對,你可以做任何這...

解決方案1:使適配器成爲片段的內部類,以便您可以直接調用該方法。

解決方案2:更新您的適配器構造函數以接受Fragment作爲參數。

喜歡的東西:

customAdapter = new CustomAdapter(myContext, list, HomeFragment.this); 

和更新適配器的構造:

public CustomAdapter(Context context, int id, HomeFragment fragment) { 
    this.fragment = fragment; 
} 

然後調用使用片段變量的方法。

fragment.updateDate(); 
1

試試這個:

//in your fragment 
public static YourFragment fragment; 

onCreate{ 
fargment = YourFragment.this; 
} 
public void updaDate(){ 
YourTextView.setText(""); 
} 


in your adapter 
YourFragment.fragment.updateDate(); 
相關問題