上下文:有一個自定義Listview,每個列表項都有一個按鈕。當您單擊按鈕時,會出現一個帶有編輯文本和提交按鈕的alertDialog。這隻會在第一次點擊時發生,隨後的點擊Toast將僅以其點擊次數顯示。Android-幫助修復帶有EditText的自定義警報對話框
當您點擊提交按鈕時,會出現一個烤麪包,顯示在editText中輸入的文本以及點擊它的次數,推測總是爲1,因爲這隻能在第一次點擊時發生。
問題:如果用戶在點擊提交之前點擊editText,那麼timesClicked計數器無法正常工作。我猜測它正在休息。但是,如果用戶沒有而不是,請點擊editText,然後程序正常工作。 0_o我不知所措。
嘗試解決:我將代碼簡化了很多,試圖找出問題並找出問題所在。最初我膨脹了一個只有編輯文本的視圖,然後我只是使用builder.setPositiveButtton。我認爲直接在視圖中實現按鈕可以修復它,但似乎並非如此。我一直堅持這一點。任何幫助將是巨大的
Here is a video of the bug happening
private class OnSubtractClickListener implements View.OnClickListener {
final int id; //id of list item that was clicked
int timesClicked;
Toast toast;
public OnSubtractClickListener(int id, View view) {
super();
this.id = id;
timesClicked = 0;
}
@Override
public void onClick(View view) {
if (timesClicked != 0) {
toast.setText(Integer.toString(timesClicked));
toast.show();
}
else{
toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT);
final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null);
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setView(dialogView);
builder.setTitle("Subtract cigar?");
builder.setIcon(R.mipmap.monkey_launcher);
final AlertDialog dialog = builder.create();
Button yesButton = (Button)dialogView.findViewById(R.id.dialog_notes_yes_button);
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText);
String userInput = editText.getText().toString();
String timesClickedString = Integer.toString(++timesClicked);
toast.setText(timesClickedString + ": " + userInput);
toast.show();
dialog.dismiss();
}
});
dialog.show(); //new
}
}
}
爲了讓次的點擊回到0,則必須在某處重新創建點擊監聽器。你可以顯示你在代碼中設置點擊監聽器的位置嗎? – AdamMc331
它只在用戶點擊對話框中的editText時才被重置。否則,它工作正常。我設置它在一個bindView的customlistAdapter – Dillon
沒有人知道這個問題?在我的應用程序中,數據庫正在被調用,並且正在更新列表視圖。但是,然後listview實際上恢復到以前的狀態。但數據庫已更新。沒有意義。有什麼可以取消對列表視圖項的更改 – Dillon