从alertDialog填充listView

时间:2014-02-14 07:33:45

标签: android listview android-listview

前几天我问过问题,但没有人回答。帮我们伙伴们... 我有一个单击按钮,将显示一个alertdialog ....在alertDialog中我设置了包含两个editTexts和一个保存按钮的提示视图...单击保存按钮后,信息应保存在列表视图中(例如名称和电子邮件)...所以我想通过单击保存按钮保存多个记录...例如用户输入名称和电子邮件,当用户输入保存按钮时,记录应保存为列表视图中的一个项目...因此用户可以在单击alertDialog中的保存按钮后保存多个记录。我对android很新...我已经搜索但不明白... PLZ帮助我...我不知道该怎么做...

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

好像你从未在列表中添加任何项目。

像这样调整代码:

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

有效吗?