Converting Thread to an Async task in Android

244 views Asked by At

I have a list of products in a row in my UI with its respective values.

Product1 --- 10
Product2 --- 12
. . . . . . . . and so on

. Each row can be clicked and a new custom dialog interface opens where the user can change the value and set new values. The problem is when I change the value and close the dialog box, the value doesn't get updated in the row. I have to open the dialog box and and close it again and then it gets updated. It always updates the "previous" value. For example, If I change Product1 from 10-12 and then open dialog again and change it to 13, it gets updated with "12". I'm using a thread in a separate utility method. I want to convert my Thread implementation to async task and use it in background of onPostExecute. How can I achieve this?

 static void saveMutation(final String productId,
                             final int mutationAmount,
                             final boolean isOpeningStock,
                             final String flightKey,
                             final FlightLegIdentifier fli,
                             final String crew, final String tabletId,
                             final AirFiApplication airFiApplication) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StockMutation mutation = new StockMutation();
                mutation.setOpeningStock(isOpeningStock);
                mutation.setFlightKey(flightKey);
                mutation.setFli(fli);
                mutation.setProductId(productId);
                mutation.setCrew(crew);
                mutation.setMutationId(String.valueOf(UUID.randomUUID()));
                mutation.setTabletId(tabletId);
                mutation.setMutationAmount(mutationAmount);
                StockMutationDao dao = new StockMutationDao(SHARD_DB);
                dao.createOrUpdate(mutation);
                StockUtils.adjustStock(flightKey, fli, airFiApplication);
                StockUtils.setIsStockChanged(true);
            }
        }).start();
    }
1

There are 1 answers

0
ʌɐɥqıɐʌ On BEST ANSWER

problem is that UI can be updated only on UI(main) thread. You can use Handler or runOnUIThread() for updating on UI thread from Non-UI thread. You can find lot of resources on google.