2016-12-23 24 views
1

我一直在試圖從軟鍵盤完成按鈕點擊更改使用setText()textView文本改變我的TextView。但它不會工作。除了點擊按鈕之外,我可以在其他任何地方設置文本。我也可以從完成按鈕單擊設置工作Toast,只是不能從內部操作textView。我被暗示我應該嘗試runOnUiThread()方法,但我仍然無法使其工作,我花了數小時的努力。如果它有所不同,這全部來自自定義適配器。如何使用runOnUiThread()在alertDialog使用setText()方法

import android.content.Context; 
import android.os.Handler; 
import android.os.Looper; 
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(); 

     final MainActivity mn = new MainActivity(); 





     //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 
       OptionDialog.setTitle("Enter new chore"); 
       OptionDialog.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 wont change to string hello 
          //textView.setText(input.getText().toString()); 
          mn.runOnUiThread(new Runnable() { 

           @Override 
           public void run() { 
            // your stuff to update the UI 
            textView.setText("hello"); 

           } 
          }); 



          //this toast works 
          // Toast.makeText(getContext(), "this is my Toast message!!! =)", 
          //  Toast.LENGTH_LONG).show(); 
          OptionDialog.dismiss(); 
         } 
         return false; 
        } 
       }); 



       OptionDialog.show(); 
      } 
     }); 


     imageButton.setImageResource(R.drawable.clock); 

     return customView; 
    } 
} 
+0

你'onEditorAction'已經在UI胎面。你爲什麼要使用runOnUiThread? –

+0

我不確定自己是否只是來自其他堆棧溢出帖子的建議。 –

回答

0

final MainActivity mn = new MainActivity();是錯誤的,因爲主要活動已經存在。通過調用活動的引用一個ArrayAdapter,並呼籲myactivity.runOnUiThread()

+0

你的意思是「將調用活動的引用傳遞給arrayadapter」,我只是在學習。 –

+0

當你調用'new CustomAdapter()'時,你傳遞'Context'和'ArrayList'。只需將「Activity」添加到構造函數並傳遞即可。 – beeb

+0

我究竟如何將Activity添加到構造函數中? –

0

你的代碼是不是在Activity的子類,所以你可以不使用runOnUiThread()方法,因爲它是Activity的方法。

final MainActivity mn = new MainActivity();是不正確的,你不應該自己新的活動。

相反,你可以使用View.post(Runnable action),就像這樣:

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.post(new Runnable() { 
       @Override 
       public void run() { 
        // your stuff to update the UI 
        textView.setText("hello"); 
       } 
      }); 

      //this toast works 
      // Toast.makeText(getContext(), "this is my Toast message!!! =)", 
      //  Toast.LENGTH_LONG).show(); 
      OptionDialog.dismiss(); 
     } 
     return false; 
    } 
}); 
+0

沒有解決我的問題。但我正在逐漸在記錄這些錯誤CAT- –

+0

E/ViewRootImpl:sendUserActionEvent()MView的== NULL W/IInputConnectionWrapper:getSelectedText非活動InputConnection W/IInputConnectionWrapper:getTextAfterCursor非活動InputConnection W/IInputConnectionWrapper:getExtractedText上不活躍InputConnection W/IInputConnectionWrapper:beginBatchEdit on inactive InputConnection W/IInputConnectionWrapper:endBatchEdit on inactive InputConnection –