2016-12-27 11 views
0

ユーザはalertDialogの編集テキストからテキストを入力し、リストビューのテキストビューをその入力に更新します。 textViewを空白に変更する編集テキストに何も入力しない場合にのみ動作するように見えますが、入力がなければ何も起こりません。alertTialogのeditTextからのカスタム入力listViewのTextViewを変更できません

これは私がここに

import android.content.Context; 
import android.content.DialogInterface; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AlertDialog; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.ArrayList; 

import static com.example.emilythacker.chorelist.R.id.textView_ID; 



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

     textView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //what happens when textView is clicked 
       AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create(); 
       final EditText input = new EditText(getContext()); 
       input.setHint("hint"); 
       alertDialog.setView(input); 
       alertDialog.setTitle("Set Chore"); 
       alertDialog.setMessage("Alert message to be shown"); 
       alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 

           //this will set text to "hello" only if user does not enter anything into input 
           textView.setText("hello"); 

           //this also will only work if there is no input entered into input...wich will change textView into empty space 
           //textView.setText(input.getText().toString()); 

           //works the same with or without dialog.dismiss(); 
           dialog.dismiss(); 
          } 
         }); 
       alertDialog.show(); 

      } 
     }); 



     imageButton.setImageResource(R.drawable.clock); 

     return customView; 
    } 
} 

IN-働いていますアダプタクラスは、私のリストビューの行ごとに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> 

答えて

1

キーボードのポップアップが原因で、リストビューが更新されています。あなたのマニフェストでは、あなたのアダプターを保持している活動で、コードの行を追加する必要があります。

<activity android:name=".Your_Activity" android:windowSoftInputMode="adjustNothing"> //add this line

はそれが仕事をdidntのあなたの問題は

+0

それは動作します!これに長時間執着した。ありがとうございました –

0

をhelps-場合、私はあなたが取得し表示されていないですあなたのeditTextからの値をあなたのtextViewに割り当てます。このようにあなたの公開getViewメソッド()関数を更新し :それは場合に役立ちます

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

    String text; 

    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //what happens when textView is clicked 
      AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create(); 
      final EditText input = new EditText(getContext()); 
      input.setHint("hint"); 
      alertDialog.setView(input); 
      alertDialog.setTitle("Set Chore"); 
      alertDialog.setMessage("Alert message to be shown"); 
      alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

          text = input.getText().toString(); 

          //this will set text to "hello" only if user does not enter anything into input 
          textView.setText(text); 

          //this also will only work if there is no input entered into input...wich will change textView into empty space 
          //textView.setText(input.getText().toString()); 

          //works the same with or without  dialog.dismiss(); 
          dialog.dismiss(); 
         } 
        }); 
      alertDialog.show(); 

     } 
    }); 



    imageButton.setImageResource(R.drawable.clock); 

    return customView; 
} 

を教えてください。

+0

:)なくなっていると思います。それとtextView.setText(input.getText()。toString())の間には違いがありますか? ? –

関連する問題