android - 需要更多了解 android 中的 AsyncTask

标签 android android-asynctask

我这行代码完全没有问题,唯一的问题是每次执行这段代码时,应用程序都会等待所有进程完成,使界面可能有半秒钟没有响应。我找到的最佳解决方案是使用 Thread 但它会导致一些问题,所以我求助于 AsyncTask 我无法理解。任何人都可以帮助我如何将它实现到这行代码吗?

            ima.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    search.clearFocus();
                    currentItem = ssil;
                    if (currentRoom.equals("None")) {
                        Toast.makeText(StartMoving.this, "Please select a room before adding an item", Toast.LENGTH_LONG).show();
                    }else{

                        try {
                            String currentitemholder = startmoving.getSingleItemName(currentMove, currentRoom, currentItem);
                            if (currentitemholder.equals(currentItem)) {
                                String ssi = startmoving.getSingleItemValue(currentMove, currentRoom, currentItem);
                                String ssiw = startmoving.getSingleItemWeight(currentMove, currentRoom, currentItem);
                                String stw = startmoving.getTotalWeight(currentMove);
                                String srw = startmoving.getRoomWeight(currentMove, currentRoom);
                                String diw = startmoving.getListItemWeight(currentItem);
                                int istw = Integer.parseInt(stw);
                                int isiw = Integer.parseInt(ssiw);
                                int isi = Integer.parseInt(ssi);
                                int isrw = Integer.parseInt(srw);
                                int idiw = Integer.parseInt(diw);
                                currentItemValue = "" + ++isi;
                                currentItemWeight = "" + (isiw + idiw);
                                currentTotalItemWeight = "" + (istw + idiw);
                                currentRoomWeight = "" + (isrw + idiw);
                                currentTotalItem = startmoving.getTotalItem(currentMove) + 1; 
                                startmoving.updateItems(currentMove, currentRoom, currentItem, currentItemValue, currentItemWeight);
                                startmoving.setMoveWeight(currentTotalItemWeight, currentMove);
                                startmoving.setRoomWeight(currentRoomWeight, currentMove, currentRoom); 
                                startmoving.setTotalItem(currentMove, currentTotalItem);
                                String displayname = startmoving.getItemName(currentMove, currentRoom);
                                String displayvalue = startmoving.getItemValue(currentMove, currentRoom);
                                String displayweight = startmoving.getItemWeightLBS(currentMove, currentRoom);
                                String displaytotalweight = startmoving.getTotalWeightLBS(currentMove);
                                String displayroomweight = startmoving.getRoomWeightLBS(currentMove, currentRoom);
                                roomContent.setText(displayname);
                                itemValue.setText(displayvalue);
                                itemWeight.setText(displayweight);
                                totalweight.setText(displaytotalweight);
                                roomweight.setText(displayroomweight); 
                                int displaytotalitem = startmoving.getTotalItem(currentMove); 
                                totalitem.setText("" + displaytotalitem);
                            }
                        }catch (Exception e) {
                            // TODO Auto-generated catch block
                            currentItemValue = "1";
                            currentItemWeight = ssiw;
                            String stw = startmoving.getTotalWeight(currentMove);
                            String srw = startmoving.getRoomWeight(currentMove, currentRoom);
                            String diw = startmoving.getListItemWeight(currentItem);
                            int istw = Integer.parseInt(stw);
                            int isrw = Integer.parseInt(srw);
                            int idiw = Integer.parseInt(diw);
                            currentTotalItemWeight = "" + (istw + idiw);
                            currentRoomWeight = "" + (isrw + idiw);
                            currentTotalItem = startmoving.getTotalItem(currentMove) + 1; 
                            startmoving.addNewItems(currentMove, currentRoom, currentItem, currentItemValue, currentItemWeight);
                            startmoving.setMoveWeight(currentTotalItemWeight, currentMove);
                            startmoving.setRoomWeight(currentRoomWeight, currentMove, currentRoom); 
                            startmoving.setTotalItem(currentMove, currentTotalItem);
                            startmoving.setTotalItem(currentMove, currentTotalItem);
                            String displayname = startmoving.getItemName(currentMove, currentRoom);
                            String displayvalue = startmoving.getItemValue(currentMove, currentRoom);
                            String displayweight = startmoving.getItemWeightLBS(currentMove, currentRoom);
                            String displaytotalweight = startmoving.getTotalWeightLBS(currentMove);
                            String displayroomweight = startmoving.getRoomWeightLBS(currentMove, currentRoom);
                            roomContent.setText(displayname);
                            itemValue.setText(displayvalue);
                            itemWeight.setText(displayweight);
                            totalweight.setText(displaytotalweight);
                            roomweight.setText(displayroomweight); 
                            int displaytotalitem = startmoving.getTotalItem(currentMove); 
                            totalitem.setText("" + displaytotalitem);
                        }
                    }
                }
            });

            imr.setOnClickListener(new OnClickListener() {

                @SuppressLint("ShowToast")
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    search.clearFocus();
                    currentItem = ssil;
                    if (currentRoom.equals("None")) {
                        Toast.makeText(StartMoving.this, "Please select a room before removing an item", Toast.LENGTH_LONG).show();
                    }else{

                        try {
                            String currentitemholder = startmoving.getSingleItemName(currentMove, currentRoom, currentItem);
                            if (currentitemholder.equals(currentItem)) {
                                String ssi = startmoving.getSingleItemValue(currentMove, currentRoom, currentItem);
                                String ssiw = startmoving.getSingleItemWeight(currentMove, currentRoom, currentItem);
                                String stw = startmoving.getTotalWeight(currentMove);
                                String srw = startmoving.getRoomWeight(currentMove, currentRoom);
                                String diw = startmoving.getListItemWeight(currentItem);
                                int istw = Integer.parseInt(stw);
                                int isiw = Integer.parseInt(ssiw);
                                int isi = Integer.parseInt(ssi);
                                int isrw = Integer.parseInt(srw);
                                int idiw = Integer.parseInt(diw);
                                if (isi > 1) {
                                    currentItemValue = "" + --isi;
                                    currentItemWeight = "" + (isiw - idiw);
                                    currentTotalItemWeight = "" + (istw - idiw);
                                    currentRoomWeight = "" + (isrw - idiw);
                                    currentTotalItem = startmoving.getTotalItem(currentMove) - 1; 
                                    startmoving.updateItems(currentMove, currentRoom, currentItem, currentItemValue, currentItemWeight);
                                    startmoving.setMoveWeight(currentTotalItemWeight, currentMove);
                                    startmoving.setRoomWeight(currentRoomWeight, currentMove, currentRoom); 
                                    startmoving.setTotalItem(currentMove, currentTotalItem);
                                    startmoving.setTotalItem(currentMove, currentTotalItem);
                                    String displayname = startmoving.getItemName(currentMove, currentRoom);
                                    String displayvalue = startmoving.getItemValue(currentMove, currentRoom);
                                    String displayweight = startmoving.getItemWeightLBS(currentMove, currentRoom);
                                    String displaytotalweight = startmoving.getTotalWeightLBS(currentMove);
                                    String displayroomweight = startmoving.getRoomWeightLBS(currentMove, currentRoom);
                                    roomContent.setText(displayname);
                                    itemValue.setText(displayvalue);
                                    itemWeight.setText(displayweight);
                                    totalweight.setText(displaytotalweight);
                                    roomweight.setText(displayroomweight); 
                                    int displaytotalitem = startmoving.getTotalItem(currentMove); totalitem.setText("" + displaytotalitem);
                                }else{
                                    String sstw = startmoving.getTotalWeight(currentMove);
                                    String ssrw = startmoving.getRoomWeight(currentMove, currentRoom);
                                    int isstw = Integer.parseInt(sstw);
                                    int issrw = Integer.parseInt(ssrw);
                                    currentTotalItemWeight = "" + (isstw - idiw);
                                    currentRoomWeight = "" + (issrw - idiw);
                                    currentTotalItem = startmoving.getTotalItem(currentMove) - 1; startmoving.setMoveWeight(currentTotalItemWeight, currentMove);
                                    startmoving.setRoomWeight(currentRoomWeight, currentMove, currentRoom);
                                    startmoving.deleteItem(currentMove, currentRoom, currentItem); startmoving.setTotalItem(currentMove, currentTotalItem);
                                    startmoving.setTotalItem(currentMove, currentTotalItem);
                                    String displayname = startmoving.getItemName(currentMove, currentRoom);
                                    String displayvalue = startmoving.getItemValue(currentMove, currentRoom);
                                    String displayweight = startmoving.getItemWeightLBS(currentMove, currentRoom);
                                    String displaytotalweight = startmoving.getTotalWeightLBS(currentMove);
                                    String displayroomweight = startmoving.getRoomWeightLBS(currentMove, currentRoom);
                                    roomContent.setText(displayname);
                                    itemValue.setText(displayvalue);
                                    itemWeight.setText(displayweight);
                                    totalweight.setText(displaytotalweight);
                                    roomweight.setText(displayroomweight); 
                                    int displaytotalitem = startmoving.getTotalItem(currentMove); 
                                    totalitem.setText("" + displaytotalitem);
                                }   
                            }
                        }catch (Exception e) {
                            // TODO Auto-generated catch block

                        }
                    }
                }
            });

如您所见,这是两个调用数据库(发送和接收数据)并将其显示到 GUI 的按钮。

编辑

感谢你们的精彩回复!在我决定正确答案之前,我会先研究 AsyncTask。仍然需要处理您提供给我的所有这些信息。

编辑 忘了说 startmoving 是我的数据库模块

最佳答案

如果您执行一项持续时间很长的操作,例如加载文件或访问数据,您的 Android 应用程序的用户界面将阻塞,直到相应的代码完成。

AsyncTask

为了使用 AsyncTask 类,您必须扩展它并至少覆盖 doInBackground() 方法。

您需要实现的最常见方法如下:

   1. onPreExecute() – called on the UI thread before the thread starts running. This method is usually used to setup the task, for example by displaying a progress bar.

   2. doInBackground(Params…) – this is the method that runs on the background thread. In this method you should put all the code you want the application to perform in background. Referring to our Simple RSS Aplication, you would put here the code that downloads the XML feed and does the parsing. The doInBackground() is called immediately after onPreExecute(). When it finishes, it sends the result to the onPostExecute().

   3. onProgressUpdate() - called when you invoke publishProgress() in the doInBackground().

   4. onPostExecute(Result) – called on the UI thread after the background thread finishes. It takes as parameter the result received from doInBackground().

现在在您的代码选择中,您可以做什么。

所有数据库操作和数据操作代码都写在doInBackground(Params…)方法中,并在onPostExecute(Result)方法中显示数据。

谢谢。

关于android - 需要更多了解 android 中的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043001/

相关文章:

java - 从 AsyncTask 调用另一个 Activity

multithreading - Android ProgressBar 停止更新

java - 将另一个 Activity 中的项目添加到 ExpandableListView

Android Paging 3 如何过滤、排序和搜索我的数据

java - 如何让异步任务在 Android 中完成

java - Android 验证远程 MySQL 中的登录

android - 崩溃的JSON解析Android应用

android - Google 搜索更新后继续语音识别蜂鸣声

java - 为什么未显示检索到的数据

android - 在 iOS 中显示与 Android 中的 Toast 具有相同功能的消息