如何在列表项单击android时解除alertdialog

时间:2014-10-02 06:07:37

标签: android android-alertdialog

这是我的代码:

当我点击textview时,会显示一个对话框显示列表。当我选择特定列表项对话框时不被解雇。如何在单击列表项时关闭对话框,

educationtxt=(TextView)findViewById(R.id.education_txt);
String[] educationarray = new String[]{"High School","Som College","Associates Degree","Bachelor Degree","Masters Degree","PHD"};
educationtxt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
  final ListView lv ;
  AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegistrationActivity.this);
  LayoutInflater inflater = getLayoutInflater();
  View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
  alertDialog.setView(convertView);
  alertDialog.setTitle("Education");
  View convertView1 = (View) inflater.inflate(R.layout.custom_dialog_row, null);
  TextView tv =(TextView)convertView1.findViewById(R.id.list_row_txt);
  lv = (ListView) convertView.findViewById(R.id.custom_listView1);
  lv.setBackgroundColor(Color.WHITE);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(RegistrationActivity.this,R.layout.custom_dialog_row,R.id.list_row_txt,educationarray);
  lv.setAdapter(adapter);
  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         education_selected = lv.getAdapter().getItem(position).toString();
         educationtxt.setText(education_selected);
         Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
         //here i want dismiss 
    }
  });
  alertDialog.show();   
 }
});

3 个答案:

答案 0 :(得分:6)

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                     @Override
                     public void onItemClick(AdapterView<?> parent, View view,
                                             int position, long id) {
                         education_selected = lv.getAdapter().getItem(position).toString();
                         educationtxt.setText(education_selected);
                         Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
                      //here i want dismiss 
                       alertDialog.dissmiss(); <----- add this lone to dismiss
                     }
                 });

并将alertdialoge作为最终

答案 1 :(得分:0)

这是最终代码:

eyecolortxt.setOnClickListener(new OnClickListener()
{
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final ListView lv ;
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegistrationActivity.this);
                    LayoutInflater inflater = getLayoutInflater();
                    View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
                    alertDialog.setView(convertView);
                    alertDialog.setTitle("Eye Color");
                    View convertView1 = (View) inflater.inflate(R.layout.custom_dialog_row, null);
                    TextView tv =(TextView)convertView1.findViewById(R.id.list_row_txt);
                     lv = (ListView) convertView.findViewById(R.id.custom_listView1);
                    lv.setBackgroundColor(Color.WHITE);
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(RegistrationActivity.this,R.layout.custom_dialog_row,R.id.list_row_txt,eyeArray);
                    lv.setAdapter(adapter);


                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                         @Override
                         public void onItemClick(AdapterView<?> parent, View view,
                                                 int position, long id) {
                             eye_color_selected = lv.getAdapter().getItem(position).toString();
                             eyecolortxt.setText(eye_color_selected);
                             Toast.makeText(RegistrationActivity.this, "You Clicked at "+eye_color_selected, Toast.LENGTH_SHORT).show();
                              alertDialog.dissmiss();  
                         }
                     });
                    alertDialog.show(); 
            }
        });

也没有用,我会得到方法对于AlertDialog.dissmiss()的AlertDialog.Builder类型,undemiss()是未定义的。线

答案 2 :(得分:0)

只需调用此方法并显示对话框。在单击ListItem时也关闭对话框

 public void showDialog(){
    final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
    dialog.setTitle("Choose App");
    dialog.setCancelable(true);

    View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_dialog_all_app, null);
    list = (ListView) view.findViewById(R.id.AllAppList);
    AllAppPckName = getPackages();
    AllAppListAdapter adapter= new AllAppListAdapter(getContext(), R.layout.app_item, AllAppPckName);

    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            appIcon.setVisibility(View.VISIBLE);
            appIcon.setImageDrawable(getPackageIcon(getContext(), AllAppPckName.get(i)));
            appNameBtn.setText(getAppNameFromPkgName(getContext(), AllAppPckName.get(i)));

            dialogg.dismiss();


        }
    });


    dialog.setView(view);
    dialogg = dialog.show();
}