将一个片段中的长值从主要活动传递到另一个片段

时间:2017-07-05 22:12:40

标签: android android-fragments fragment

我有一个包含两个片段的main_activity。当在beatTimeFragment上单击一个按钮时,我希望beatTimeFragment确定按下该按钮的1970年后的msec,并通过主要活动将其传递给toneTimeFragment。然后,toneTimeFragment将响应与标准节拍进行比较并给出offSet。

我见过的每个例子都传递字符串数据而不是长数据。在我的代码中,我尝试传递String数据,然后将其转换为长数据。字符串数据被传输(我可以显示字符串)。但是,如果我没有例外,那么long会给出0的响应,或者它会把我踢掉。

有没有办法让转换工作,如果没有,我如何通过删除

来设置长数据传输而不是String
beatTimeDisplay.getText().toString()

在beatTimeFragment的末尾?

main_activity

public class MainActivity extends AppCompatActivity implements 
BeatTimeFragment.BeatTimeListener{

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

//Get called by BeatTimeFragment when button is pushed
@Override
public void sendBeatData(String beatTime) {
    ToneTimeFragment toneFragment = (ToneTimeFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    toneFragment.setBeatLong(beatTime);
}
}

toneTimeFragment

public class ToneTimeFragment extends Fragment {


public void setBeatLong(String beatTime) {
    try {
        beatTimeL = Long.parseLong(beatTime);
    } catch (NumberFormatException e) {
        System.out.println("NumberFormatException: " + e.getMessage());
    }
    // beatTimeL = Long.parseLong(beatTime);
    offSet = beatTimeL - toneTime;
    offSetView.setText(beatTime + " msec");

beatTimeFragment

public class BeatTimeFragment extends Fragment {

private static Button beatBtn;
private static TextView beatTimeDisplay;
int q = 0;
long beatTime = 0;

//set up sending beatTime
BeatTimeListener activityCommander;

public interface BeatTimeListener{
    public void sendBeatData(String beatTime);
}

//setup to send data to top
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCommander = (BeatTimeListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}

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

    beatTimeDisplay = (TextView) view.findViewById(R.id.beatTimeDisplay);
    final Button beatBtn = (Button) view.findViewById(R.id.beatBtn);


    beatBtn.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v){

                    long beatTime= System.currentTimeMillis();
                    TextView view1 = (TextView) view.findViewById(R.id.beatTimeDisplay);
                    view1.setText(beatTime + " msec");
                    readySendBeat(v);

                }
            }

    );

    return view;
}

//calls to send data when button clicked
public void readySendBeat(View v) {
    activityCommander.sendBeatData(beatTimeDisplay.getText().toString());
     }
}

2 个答案:

答案 0 :(得分:1)

调试时,我意识到我没有将beatTime值传递给BeatTimeFragment中的readySendBeat方法。当我将对象添加到方法时,我能够传递数据,不再获得值0。

public void readySendBeat(Long beatTime){
    activityCommander.sendBeatData(beatTime);
    }

答案 1 :(得分:0)

试试这个,你不需要在按钮监听器内调用findViewById,也不需要创建重复的长字段:

MainActivity:

public class MainActivity extends AppCompatActivity implements 
BeatTimeFragment.BeatTimeListener{

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

//Get called by BeatTimeFragment when button is pushed
@Override
public void sendBeatData(long beatTime) {
    ToneTimeFragment toneFragment = (ToneTimeFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    toneFragment.setBeatLong(beatTime);
}
}

ToneTimeFragment:

public class ToneTimeFragment extends Fragment {

    public void setBeatLong(long beatTime) {
        try {
            beatTimeL = beatTime;
        } catch (NumberFormatException e) {
            System.out.println("NumberFormatException: " + e.getMessage());
        }
        offSet = beatTimeL - toneTime;
        offSetView.setText(beatTime + " msec");

BeatTimeFragment:

public class BeatTimeFragment extends Fragment {

private static Button beatBtn;
private static TextView beatTimeDisplay;
int q = 0;
long beatTime = 0;

//set up sending beatTime
BeatTimeListener activityCommander;

public interface BeatTimeListener{
    public void sendBeatData(String beatTime);
}

//setup to send data to top
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCommander = (BeatTimeListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString());
    }
}

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

    beatTimeDisplay = (TextView) view.findViewById(R.id.beatTimeDisplay);
    final Button beatBtn = (Button) view.findViewById(R.id.beatBtn);


    beatBtn.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v){

                    beatTime= System.currentTimeMillis();
                    beatTimeDisplay.setText(beatTime + " msec");
                    readySendBeat(v);

                }
            }

    );

    return view;
}

//calls to send data when button clicked
public void readySendBeat() {
    activityCommander.sendBeatData(beatTime);
     }
}