2016-12-23 3 views
0
ändern

Ich habe einen alertDialog und wenn der Benutzer die Done-Taste auf der Tastatur gedrückt wird, möchte ich die setText() -Methode verwenden, um die TextView in der ListView zu ändern. Ich kann setText() -Methode scheinbar überall verwenden, aber in der Done-Taste bei Klick-Methode, obwohl ein Toast von Done-Taste auf Klick funktioniert. Der fehlerhafte Code befindet sich in der ArrayAdapter-Klasse. Dies ist alles mein Code und die Log-Cat-Nachrichten, wenn ich den Done-Button drücke.Willst du TextView auf Knopfdruck von der weichen Tastatur

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ViewSwitcher; 

import java.util.ArrayList; 

public class MainActivity extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ArrayList<String> Chores = new ArrayList<>(); 
     Chores.add(""); 
     //final String[] Chores = {"", "", ""}; 
     final ListAdapter MyAdapter = new CustomAdapter(this, Chores); 
     ListView listViewObject = (ListView)findViewById(R.id.customListView_ID); 
     listViewObject.setAdapter(MyAdapter); 

     listViewObject.setOnItemClickListener(
      new AdapterView.OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
        String ChoreString = String.valueOf(parent.getItemAtPosition(position)); 




       } 
      } 

     ); 
     final Button button = (Button) findViewById(R.id.button_ID); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Chores.add(""); 
       ((ArrayAdapter)MyAdapter).notifyDataSetChanged(); 

      } 

     }); 




    } 
} 

CustomAdapter.java

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(); 







     //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.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; 
        } 
       }); 



       OptionDialog.show(); 
      } 
     }); 


     imageButton.setImageResource(R.drawable.clock); 

     return customView; 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.emilythacker.chorelist.MainActivity"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/customListView_ID" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="50dp" /> 

    <Button 
     android:text="Add Chore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/button_ID" /> 
</RelativeLayout> 

custom_listview_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="60dp"> 

    <ImageButton 
     android:layout_width="60dp" 
     android:scaleType="fitCenter" 
     app:srcCompat="@drawable/clock" 
     android:id="@+id/imageButton_ID" 
     android:layout_height="60dp" 
     android:background="@null" 
     android:layout_alignParentRight="true" 
     android:padding="5dp" 
     android:layout_weight="1" /> 


    <TextView 
     android:text="Click here to add chore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:id="@+id/textView_ID" 
     android:textSize="25dp" 
     android:layout_centerVertical="true" 
     android:layout_toStartOf="@+id/imageButton_ID" 
     android:layout_marginEnd="11dp" 
     /> 
</RelativeLayout> 

Protokoll Katze Nachrichten

D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 1128) vi=Rect(0, 96 - 0, 1128) or=1 
D/ViewRootImpl: #3 mView = null 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
E/ViewRootImpl: sendUserActionEvent() mView == null 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection 
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection 
W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection 
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 
D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1 
V/ActivityThread: updateVisibility : ActivityRecord{e0eeb0a [email protected] 
+0

Haben Sie versucht, Runnable nicht zu verwenden? –

Antwort

0

postDelayed Versuchen() statt post() wie unten.

textView.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      // your stuff to update the UI 
      textView.setText("hello"); 
     } 
    },200); 

Ich hoffe, es wird funktionieren.

Verwandte Themen