Jim Abodheer
Jim Abodheer

Reputation: 1

Adding Items to ListView from Android AlertDialog

I have a ListView and a button i want when I press the button it takes me to a Dialogue it has a Text Field and another Button when I fill the text and press the button it will be saved in the list view ( I know how to add items from the same activity to the ListView) but I don't how to add items from another activity or AlterDialoge .

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>

Upvotes: 0

Views: 1307

Answers (1)

Kunal Chawla
Kunal Chawla

Reputation: 1314

Okay so I made 2 minor modifications to get your code working -

  1. Replace "et" with "mUser" to avoid Null Pointer Exception when calling getText()
  2. Dismiss Dialog upon successful updation of list.

Below is the onClick() method body :

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

See this for more info.

For other cases, where you want communication between 2 Activities/Fragments for Event-Based List Updation, you can check out EventBus.

Upvotes: 1

Related Questions