Search code examples
androidandroid-arrayadapterandroid-alertdialog

alert dialogue with custom adapter


I have a alert dialogue with a spinner in it. The default value of spinner is "Add New User". When the particular one is selected i am showing another pop up without closing alert ,in which new user details can be entered and it will be stored to my DB.I want to update my spinner in first alert with the new user value entered from db.

My code spinner selection method is

spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
                    model_list = planList_list.get(position);
                    if (model_list.getDevice().trim().equalsIgnoreCase("Add New User")){

                        User();
                        adapter_usr.notifyDataSetChanged();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    model_list = planList_list.get(0);
                }

            });

Method to add new user

public void User(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    View dialogLayout = inflater.inflate(R.layout.mybank, null);
    final EditText add1 = (EditText)dialogLayout.findViewById(R.id.shop1);
    final EditText add2 = (EditText)dialogLayout.findViewById(R.id.shop2);
    final EditText add3 = (EditText)dialogLayout.findViewById(R.id.shop3);

    Button ok = (Button)dialogLayout.findViewById(R.id.later );
    Button close = (Button)dialogLayout.findViewById(R.id.close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            dialog.dismiss();
        }
    });
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

                String addr1 = add1.getText().toString().trim();
                String addr2 = add2.getText().toString().trim();
                String addr3 = add3.getText().toString().trim();

                db.insertUser(addr1,addr2,addr3);
                adapter_usr.notifyDataSetChanged();
                dialog.dismiss();
        }
    });

    dialog.setView(dialogLayout,0,0,0,0);
    dialog.show();

}

I want to update spinner data in first alert dialogue with the value from second alert dialogue. Your response are appreciated


Solution

  • After inserting user data to the table add the user data to planList_list before calling notifyDataSetChanged().

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
    
                String addr1 = add1.getText().toString().trim();
                String addr2 = add2.getText().toString().trim();
                String addr3 = add3.getText().toString().trim();
    
                db.insertUser(addr1,addr2,addr3);
                planList_list.add(/*Add user data*/);
                adapter_usr.notifyDataSetChanged();
                dialog.dismiss();
        }
    });
    

    Hope this solves your issue. Happy coding.