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