1

I had asked question some days before but didnt reply by anyone.. Help me guys plz ... I have a button which on clicked , an alertdialog will be displayed.... In alertDialog i set prompview which have two editTexts and a save button... After clicking a save button the information should be saved in listview (for example name and email)... so i want to saved more than records by clicking save button... For example user enter name and email and when user enter save button , the record should be saved as one item in listview... so user can save more than one records after clicking save button in alertDialog.. I am very new to android... I have searched but dont understand... plz help me... I have no idea how to do this...

This is my code:

public class Personal_Info extends ListActivity {
        final Context context = this;
        Button btnAddEmail;
        EditText etEmailName,etEmailAddress;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.personal_info);

            btnAddEmail =(Button)findViewById(R.id.btnAddEmail);


            btnAddEmail.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub


                    LayoutInflater li = LayoutInflater.from(context);
                    View promptsView = li.inflate(R.layout.add_email, null);
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            context);

                    // set prompts.xml to alertdialog builder
                    alertDialogBuilder.setView(promptsView);
                    etEmailName =(EditText)promptsView.findViewById(R.id.etEmailName);
                    etEmailAddress =(EditText)promptsView.findViewById(R.id.etEmailAddress);


                    alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                         String emailname = etEmailName.getText().toString();
                         String emailadd = etEmailAddress.getText().toString();

                        if(emailname.equals("") || emailadd.equals("")){
                            Toast.makeText(Personal_Info.this, "enter name and email address", Toast.LENGTH_SHORT).show();
                        }else{
                            final String[] email =new String[]{emailname,emailadd};
                            setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.row_item,email));
                            ListView list = getListView();
                             list.setTextFilterEnabled(true);

                             list.setOnItemClickListener(new OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                        View arg1, int arg2, long arg3) {
                                    // TODO Auto-generated method stub

                                    Toast.makeText(getApplicationContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();

                                    }
                                });
                            }
                        }
                      })
                    .setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }
                      });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
                }
            });
        }
    }
Developer
  • 6,292
  • 19
  • 55
  • 115

1 Answers1

0

Seems like you never add any item to your list.

Adjust code like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.row_item,email);
adapter.add(email);
setListAdapter(adapter);

Did it work?

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • I see. You shouldn't create a new instance of your adapter inside the onclick method. create and set the adapter in the oncreate method and in your onclick just call .add(email) on your adapter – donfuxx Feb 14 '14 at 09:40
  • Unable to start activity ComponentInfo ... and.... Your content must have a ListView whose id attribute is 'android.R.id.list'... This the error – user3302944 Feb 14 '14 at 10:21
  • check this question http://stackoverflow.com/questions/17907122/java-lang-runtimeexception-your-content-must-have-a-listview-whose-id-attribute – donfuxx Feb 14 '14 at 11:09
  • is it still a problem? @user3302944 – donfuxx Mar 13 '14 at 19:55