2014-11-24 3 views
0

Мой сценарий:Spinner от editext андроида

Когда я нажимаю верхнюю (+) значок есть диалоговое окно отображается с editext и если я введите текст и нажмите кнопку ОК текст должен быть добавлен к моей блесны которые я не могу сделать.

Вот что я хочу сказать:

enter image description here

Это то, что я сделал:

protected void showInputDialog() { 

    // get prompts.xml view 
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); 
    View promptView = layoutInflater.inflate(R.layout.input_dialog, null); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      MainActivity.this); 
    alertDialogBuilder.setView(promptView); 


    // setup a dialog window 
    alertDialogBuilder 
      .setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Spinner element 
        listsp = (Spinner) findViewById(R.id.listspinner); 

        listtext = (EditText) findViewById(R.id.list_text); 
        list = new ArrayList<String>(); 
        list.add(listtext.getText().toString()); 
        listadapter = new ArrayAdapter<String>(getApplicationContext(), 
          android.R.layout.simple_spinner_item, list); 
        listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        listsp.setAdapter(adapter); 
       } 
      }) 
      .setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 

    // create an alert dialog 
    AlertDialog alert = alertDialogBuilder.create(); 
    alert.show(); 

} 

ответ

0

Попробуйте обновить адаптер за OnClick:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Whatever else 
    listsp = (Spinner) findViewById(R.id.listspinned); 
    list = new ArrayList<String>(); 
    listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list); 
    listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    listsp.setAdapter(adapter); 

}

protected void showInputDialog() { 

// get prompts.xml view 
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); 
View promptView = layoutInflater.inflate(R.layout.input_dialog, null); 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
     MainActivity.this); 
alertDialogBuilder.setView(promptView); 


// setup a dialog window 
alertDialogBuilder 
     .setCancelable(false) 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       listtext = (EditText) findViewById(R.id.list_text); 
       updateAdapter(listtext.getText().toString()); 
      } 
     }) 
     .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 

// create an alert dialog 
AlertDialog alert = alertDialogBuilder.create(); 
alert.show(); 

} 

protected void updateAdapter(String input) { 
    list.add(input); 
    listadapter.notifyDataSetChanged(); 
} 

EDIT: Вот как реализовать свой пользовательский адаптер (я сделал его приватным, чтобы он использовал тот же список данных. Таким образом, вам не нужно вызывать любую updateData() функции, только для уведомления адаптера, что данные изменились с notifyDataSetChanged()):

private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter { 

     @Override 
     public int getCount() { 
      return list.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return list.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View view, ViewGroup parent) { 
      TextView text = new TextView(lexs); 
      text.setText(list.get(position).getName()); 
      return text; 
     } 

    } 
+0

спасибо Саймон за reply..Can Я знаю, как я могу добавить мой собственный адаптер для этого ... скажите мне, как это сделать? – coder

+0

См. Мое редактирование для быстрого способа создания адаптера – NSimon

Смежные вопросы