JoeyL
JoeyL

Reputation: 1305

Multiple thread error

I have an app where I need to use AsyncTask but I am having the issue that the main thread is going too far and ends up crashing my app because my Connect1 thread has not retrieved the information yet needed to continue. I was wondering how could I have the thread wait till the AsyncTask thread dies out and then the main thread can continue.

Code:

private void gNameOriginTag() {

    TextView tV;

    Connect1 connect1 = new Connect1();
    connect1.execute();

    // Set the long name for the chosen.
    tV = (TextView) view.findViewById(R.id.gLName);
    tV.setText(columns.get(4));      //<<<< Error is here.

    ....
}

Connect1 AsyncTask:

private class Connect1 extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {

        Connection conn = null;
        Statement stmt = null;

        try {
            // STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            // STEP 3: Open a connection
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            stmt = conn.createStatement();

            // STEP 5a: Extract data from result set
            ResultSet rs = stmt.executeQuery("SELECT * FROM gs WHERE name ='"
                    + gSelected + "'");
            ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
            int x = 1;
            while (rs.next()) {
                while (x < rsmd.getColumnCount()) {

                    // Retrieve Strings & Add it to the ArrayList.
                    columns.add(rs.getString(x));
                    x++;
                }

            }
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }// end finally try
        }// end try

        return "";
    }
}

The reason why I am doing this is because android 2.3.3 doesn't run mysql on the main thread from what I learned so I am trying to learn AsyncTask, so what I am trying to accomplish in "Code:" is somehow make the main thread wait/join (I also read up that pausing the main thread is bad so I am even more lost on what to do) up for the Connect1 thread to finish so that:

tV.setText(columns.get(4)); 

can be retrieved so my app does not crash.

Upvotes: 1

Views: 113

Answers (2)

Sanober Malik
Sanober Malik

Reputation: 2805

You can write the code on onPostExecute method of Async task.

Upvotes: 1

koljaTM
koljaTM

Reputation: 10260

Define a method that is called by the AsyncTask when it is finished and that is then run on the UIThread again.

MainThread: calls AsyncTask

AsyncTask: does the work and calls doWhenDoneWithBackgroundWork

doWhenDoneWithBackgroundWork: does some work on MainThread again

Upvotes: 1

Related Questions