简体   繁体   中英

Passing a long value from one fragment through the main activity to another fragment

I have a main_activity containing two fragments. When a button is clicked on beatTimeFragment, I want beatTimeFragment to determine the msec past 1970 that the button was pressed and pass it to toneTimeFragment by way of the main activity. The toneTimeFragment will then compare the response to a standard beat and give an offSet.

Every example i have seen passes string data rather than long data. In my code, I tried passing String data then converting it to long data. The string data gets transfered (i can was it show string). But the long gives a response of 0 or it kick me out if i do not have the exception.

Is there a way to get the conversion to work or if not, how do i set up for a long data transfer rather than String by removing the

beatTimeDisplay.getText().toString()

at the end of the 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());
     }
}

While debugging, i realized that i was not passing my beatTime value into the readySendBeat method in the BeatTimeFragment. When i added the object to the method then I was able to pass the data and no longer get a value of 0.

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

Try this, you dont need call to findViewById inside button listener, neither create duplicate long field:

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);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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