gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:25:59 25 4
gpt4 key购买 nike

我这行代码完全没有问题,唯一的问题是每次执行这段代码时,应用程序都会等待所有进程完成,使界面可能有半秒钟没有响应。我找到的最佳解决方案是使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com