2016-12-19 9 views
0

ユーザーがinputを入力した後、ソフトキーから「完了」ボタンをクリックしてalertDialogを閉じようとしています。dismiss()メソッドを使用していますが動作しません。私はそれをエスケープするためにalertDialogボックスをクリックしなければならない。これは、アクティビティではなくカスタム・アダプタ・クラスから実行されます。カスタムアダプタからalertDialogを閉じるには

import android.content.Context; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AlertDialog; 
import android.text.InputType; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.inputmethod.EditorInfo; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 


class CustomAdapter extends ArrayAdapter{ 

    public CustomAdapter(Context context, ArrayList choreText) { 
     super(context, R.layout.custon_listview_row, choreText); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater myInflater = LayoutInflater.from(getContext()); 
     View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false); 
     ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID); 
     final TextView textView = (TextView) customView.findViewById(R.id.textView_ID); 
     final EditText input = new EditText(getContext()); 
     final AlertDialog OptionDialog = new AlertDialog.Builder(getContext()).create(); 


     //makes textView clickable 
     textView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //what happens when textView is clicked 

       final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
       // put aler dialog box logic here 
       builder.setTitle("Enter new chore"); 
       builder.setView(input); 
       input.setInputType(InputType.TYPE_CLASS_TEXT); 
       input.setImeOptions(EditorInfo.IME_ACTION_DONE); 
       //checks if "Done" button on soft keyboard is clicked 
       input.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
        @Override 
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
         if(actionId==EditorInfo.IME_ACTION_DONE){ 
          //what happens when "Done" is clicked 
          textView.setText(input.getText().toString()); 
          OptionDialog.dismiss(); 
         } 
         return false; 
        } 
       }); 



       builder.show(); 
      } 
     }); 


     imageButton.setImageResource(R.drawable.clock); 

     return customView; 
    } 
} 
+0

をエディタのリスナーにあなたはありがとうござい別の1 –

+0

@Rachit Solankiを却下しているのに対し、あなたが新しいダイアログを作成しているのリスナーをクリックしてください!それはまさにそれだった。 –

答えて

0

のonClickでこれを試してみてください。TextViewには

//comment this line 
//final AlertDialog OptionDialog = new AlertDialog.Builder(getContext()).create(); 

    //makes textView clickable 
    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //what happens when textView is clicked 

      final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
      AlertDialog OptionDialog = builder.create(); 
      // put aler dialog box logic here 
      builder.setTitle("Enter new chore"); 
      builder.setView(input); 
      input.setInputType(InputType.TYPE_CLASS_TEXT); 
      input.setImeOptions(EditorInfo.IME_ACTION_DONE); 
      //checks if "Done" button on soft keyboard is clicked 
      input.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
       @Override 
       public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if(actionId==EditorInfo.IME_ACTION_DONE){ 
         //what happens when "Done" is clicked 
         textView.setText(input.getText().toString()); 
         OptionDialog.dismiss(); 
         //send true as the event has occured 
         return true; 
        } 
        return false; 
       } 
      }); 
      builder.show(); 
     } 
    }); 
+0

ありがとうございましたが動作しませんでした。 –

関連する問題