私はまったく問題のないこのコード行を持っていますが、唯一の問題は、このコードが実行されるたびに、アプリがすべてのプロセスが終了するのを待って、インターフェースがおそらく0.5秒間応答しないことです。私が見つけた最善の解決策は使用することでした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に表示する2つのボタンです。
編集
素晴らしい応答者に感謝します!正しい答えを決める前に、まずAsyncTaskを勉強します。あなたが私にくれたこのすべての情報をまだ処理する必要があります。
編集 は、startmovingが私のデータベースモジュールであることを言及するのを忘れました