• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Sequence of the program running

ShamusVW

Newbie
I am running a fragment that as you switch to it, it queries a MySQL database and populates a spinner.
On choosing an item in the spinner and then pressing a button, I want it to again query the database and extract info corresponding to the item selected.
However, it seems to complete all the steps in the setOnClickListener method first before it returns the results from the query, whose call is also included in the setOnClickListener method.

Below is my method, as well as the code that does the querying of the database.

This is in the onCreateView method and does the initial retrieving of records to populate the spinner.
Code:
if (getUserVisibleHint()) {
    GetTrainingVideosMySql getTrainingVideosMySql = new GetTrainingVideosMySql();
    getTrainingVideosMySql.execute("");
}

This following code also in the oncreateView method, and is when an item has been selected in the spinner, and now the button has been pressed. The objective is to again query the database, set the class string called fileUrl, and then use this fileUrl in vw.setVideoPath(fileUrl):
Code:
btnFetch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        fileUrl = "";
        GetVideoUrlMySql getVideoUrl = new GetVideoUrlMySql();
        getVideoUrl.execute("");
        vw.setVideoPath(fileUrl);
        vw.start();
    }
});

Below is the method that does the extracting of the information required of the database:
Code:
private class GetVideoUrlMySql extends AsyncTask<String, Void, String> {
    String fileLocation = "";
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, pass);

            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT url FROM training.training_videos WHERE id = 1");
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                fileLocation = rs.getString(1).toString() + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileLocation;
    }

    @Override
    protected void onPostExecute(String result) {
        fileUrl = fileLocation;
    }

The sequence that I would expect the program flow to take when the button is pressed, is first to get the file URL because of the statement getTrainingVideosMySql.execute(""), which sets fileUrl, then use this fileUrl string as the video required.
Instead, it responds to the button click and runs the method setOnClickListener.
It first sets fileUrl to an empty string, then goes to "protected void onPreExecute()" in "private class GetVideoUrlMySql", then sets the video url to this empty string, tries to start the video, then the doInBackground method, then the onPostExecute method.

I need the onPostExecute method to complete and then set this fileUrl to the video needed before trying to start the video.

Sorry if all confusing how I've explained it.
 
I'm not sure if the way I did it is correct, but it works.
So in the code below:
Code:
fileUrl = "";
        GetVideoUrlMySql getVideoUrl = new GetVideoUrlMySql();
        getVideoUrl.execute("");
        vw.setVideoPath(fileUrl);
        vw.start();
I added the line "while (fileUrl) {}
Code:
fileUrl = "";
        GetVideoUrlMySql getVideoUrl = new GetVideoUrlMySql();
        getVideoUrl.execute("");
        while (fileUrl =="") {}
        vw.setVideoPath(fileUrl);
        vw.start();

This force the "class GetVideoUrlMySql extends AsyncTask" to finish getting the result from the database before moving onto the next line of setting the video path to fileUrl.
If someone has a better (more correct way) of how to do this, I would appreciate feedback.
 
Back
Top Bottom