2017-06-01 12 views
0

ボタンを押したときにボタンを押すと、それがテキストフィールドと別のボタンがあるダイアログに移動します(私はListViewに同じアクティビティからアイテムを追加する方法を知っている)しかし、私は別のアクティビティやAlterDialogeからアイテムを追加する方法はありません。Android AlertDialogからListViewにアイテムを追加する

public class Main3Activity extends AppCompatActivity { 



    EditText et; 
    ListView lv; 
    Button bt; 
    ArrayList<String> arrayList; 
    ArrayAdapter<String> adapter; 

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


     et = (EditText) findViewById(R.id.TextAdd); 
     bt = (Button) findViewById(R.id.btnadd); 
     lv = (ListView) findViewById(R.id.listView); 

     arrayList = new ArrayList<String>(); 
     adapter= new ArrayAdapter<String>(Main3Activity.this,android.R.layout.simple_list_item_1,arrayList); 
     lv.setAdapter(adapter); 

     Button mShowDialog = (Button) findViewById(R.id.btnSave); 

     mShowDialog.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this); 
       View mView = getLayoutInflater().inflate(R.layout.dialog_add,null); 
       final EditText mUser = (EditText) mView.findViewById(R.id.TextAdd); 


       Button mAdd = (Button) mView.findViewById(R.id.btnadd); 

       mAdd.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if(!mUser.getText().toString().isEmpty()) { 

         String result = et.getText().toString(); 
          arrayList.add(result); 
          adapter.notifyDataSetChanged(); 


          Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show(); 
         } 
         else { 
          Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show(); 


         } 

        } 

       }); 
       mBuilder.setView(mView); 
       AlertDialog dialog = mBuilder.create(); 
       dialog.show(); 


      } 
     }); 
    } 





<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="5dp"> 

    <TextView 
     android:id="@+id/Add" 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:layout_margin="5dp" 
     android:textSize="25sp" 
     android:text="POP UP Window" /> 

    <EditText 
     android:id="@+id/TextAdd" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_marginTop="10dp" 
     android:inputType="textPersonName" 
     android:hint="write"/> 

    <Button 
     android:id="@+id/btnadd" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorAccent" 
     android:textColor="@android:color/white" 
     android:text="Add" /> 
</LinearLayout> 




<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.jim_a.testapp.Main3Activity" 
    tools:layout_editor_absoluteY="81dp" 
    tools:layout_editor_absoluteX="0dp"> 

    <Button 
     android:id="@+id/btnSave" 
     android:layout_height="73dp" 
     android:text="Open Window" 
     android:layout_width="0dp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="16dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintLeft_creator="1" /> 


    <ListView 
     android:id="@+id/listView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginStart="13dp" 
     tools:layout_constraintTop_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintBottom_creator="1" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginEnd="13dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginTop="33dp" 
     app:layout_constraintTop_toBottomOf="@+id/btnSave" 
     tools:layout_constraintLeft_creator="1" 
     android:layout_marginBottom="31dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginLeft="13dp" 
     android:layout_marginRight="13dp"></ListView> 
</android.support.constraint.ConstraintLayout> 

答えて

0

わかりましたので、私はあなたのコードの作業を取得するために2つの軽微な変更をした -

  1. のgetTextを(呼び出し時にNULLポインタ例外を避けるために、「MUSER」と「ら」を交換してください)
  2. 時にダイアログを終了リストの更新に成功しました。

         AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this); 
             View mView = getLayoutInflater().inflate(R.layout.dialog_add, null); 
             final EditText editText_mUser = (EditText) mView.findViewById(R.id.TextAdd); 
             Button mAdd = (Button) mView.findViewById(R.id.btnadd); 
             mBuilder.setView(mView); 
             //create dialog instance here, so that it can be dismissed from within the OnClickListener callback 
             final AlertDialog dialog = mBuilder.create(); 
    
             mAdd.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View v) { 
               if (!editText_mUser.getText().toString().isEmpty()) { 
                // Instead of et.getText(), call mUser.getText() 
                String result = editText_mUser.getText().toString(); 
                arrayList.add(result); 
                adapter.notifyDataSetChanged(); 
                Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show(); 
                //dismiss dialog once item is added successfully 
                dialog.dismiss(); 
               } else { 
                Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show(); 
               } 
              } 
             }); 
             dialog.show(); 
    

    は、詳細はthisを参照してください:

は、以下のonClick()メソッドの本体です。

イベントベースのリスト更新の2つのアクティビティ/フラグメント間の通信が必要な場合は、EventBusをチェックアウトすることができます。

関連する問題