Customize dialog which has single-choice list items

后端 未结 3 832
一整个雨季
一整个雨季 2021-02-05 23:55

I created a Dialog with single-choice list items:

final CharSequence[] items = {\"Red\", \"Green\", \"Blue\"};

AlertDialog.Builder builder = ne         


        
3条回答
  •  时光取名叫无心
    2021-02-06 00:30

    You basically will have to create your own ListAdapter by subclassing one of the available Adapter classes and supply that to the dialog (using builder.setAdapter(...)). If you have an array or list of items/objects, subclassing ArrayAdapter is probably what you'll want to look into.

    In your Adapter subclass, you override the getView(...) method (amongst others) and fill the views of your custom layout with data for the supplied position in the list. More specifically, you'll want to set an image against an ImageView and text to a TextView.

     Create class `MySimpleArrayAdapter` extending from  `ArrayAdapter`. 
    

    Using ListView.setOnItemClickListener() method we get selected value of CheckTextView

    #dialog.xml
    
    
    
            
    
    
    

    To create a custom dialog box

            String [] view_location = {"Red", "Green", "Blue"};
                  TextView label = (TextView) findViewById(R.id.selected_dept);
                  AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle("Select Location");
                  ListView listview = new ListView(this);
                  listview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                          LayoutParams.WRAP_CONTENT));
                  listview.setCacheColorHint(0);
                  listview.setBackgroundColor(Color.WHITE);
    
    
                  if (view_location != null) {
    
                      MySimpleArrayAdapter choice_arrayAdapter = new MySimpleArrayAdapter(this, view_location,label.getText().toString());
                      listview.setAdapter(choice_arrayAdapter);
                      builder.setView(listview);
    
                      listview.setOnItemClickListener(new OnItemClickListener() {
                          @Override
                          public void onItemClick(AdapterView arg0, View arg1,
                                  int arg2, long arg3) {
                              // TODO Auto-generated method stub
                              TextView label = (TextView) findViewById(R.id.selected_dept);
                              label.setText(view_location[arg2]);
                              survedObjectId = view_location_id[arg2];
                              dInterface.dismiss();
    
                          }
                      });
    
    
                  }
                  Dialog dialog = builder.create();
                  dInterface = dialog;
                  dialog.getWindow().setLayout(200, 400);
                  dialog.show();'
    
        // `Extends From ArrayAdapter
    
         'public class MySimpleArrayAdapter extends ArrayAdapter {
                 private final Context context;
                 private final String[] values;
                 private final String  selectedText;
                 public MySimpleArrayAdapter(Context context, String[] values, String  selectedText) {
                   super(context, R.layout.dialog_text, values);
                   this.context = context;
                   this.values = values;
                   this.selectedText = selectedText;
                 }
    
                 @Override
                 public View getView(int position, View convertView, ViewGroup parent) {
                   LayoutInflater inflater = (LayoutInflater) context
                       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   View rowView = inflater.inflate(R.layout.dialog_text, parent, false);
                   CheckedTextView textView = (CheckedTextView) rowView.findViewById(R.id.textDialog);
    
                   textView.setText(values[position]);
                   if(textView.getText().toString().equals(selectedText))
                   {
                     textView.setChecked(true);
                   }
                   else
                   {
                     textView.setChecked(false);
                   }
    
                   return rowView;
                 }
               }'
    

提交回复
热议问题