从Fragment到Activity的Android Java Pass变量

时间:2015-09-08 19:02:10

标签: java android android-activity fragment

如何将片段的变量传递给活动?

例如片段:

int diffDays = 0;
int diffHours = 0;
int diffMinutes = 0

的活动:

private void testMethod(){
     System.out.print(diffDays);
}

1 个答案:

答案 0 :(得分:0)

你的片段:

friendList.setModel(new javax.swing.AbstractListModel() {
    URL url = getClass().getResource("friends");
    File folder = new File(url.getPath());
    File[] listOfFiles = folder.listFiles();
    String[] elements = new String[listOfFiles.length];
    for(int i=0; i<listOfFiles.length;i++) {
        elements[i] = (listOfFiles[i].getName());
}

然后在你的活动中:

public class frag extends Fragment{
   private int data;
   public int getData(){
      return data;
   }
}