Value of EditText in Custom Dialog

微笑、不失礼 提交于 2019-12-23 16:23:24

问题


I have ListActivity , onClick of each item a Custom Dialog appears.Custom Dialog contains spinner and EditText Now i am not able to get the value of EditText, while debug value of EditText is coming as blank or "".

 protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    super.onListItemClick(l, v, position, id);
    positionList=position;

    HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(position);

    LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.answer_screen,null);

    AlertDialog.Builder alertDialog=new AlertDialog.Builder(CommonScreen6.this);
    alertDialog.setTitle("Select Option");
    alertDialog.setView(layout);

    Spinner answerList=(Spinner)layout.findViewById(R.id.spinnerAnswerList);

    ArrayAdapter<String> adapterAnswerType = new ArrayAdapter<String> (CommonScreen6.this, android.R.layout.simple_spinner_item,(ArrayList<String>)temp.get(Constants.answerDesc));

    adapterAnswerType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);

    answerList.setAdapter(adapterAnswerType);            
    answerList.setOnItemSelectedListener(this);

    ArrayList<String> answerDescList=(ArrayList<String>)temp.get(Constants.answerDesc);

    answerList.setSelection(answerDescList.indexOf(temp.get(Constants.defaultAnswerDesc)));

    alertDialog.setPositiveButton("Submit",new DialogInterface.OnClickListener() 
      {

    @Override
    public void onClick(DialogInterface dialog, int which)
               {
      LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
      View layout = inflater.inflate(R.layout.answer_screen,null);
      EditText editText=(EditText)layout.findViewById(R.id.remark);
      String remark=editText.getText().toString();

      HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(positionList);
            temp.put(Constants.remark, remark);
            adapter.notifyDataSetChanged();
           }
  });
 }

回答1:


The reason that you are not getting any values is because you are reinflating the layout just before you read the values, thus resetting values to the defaults. I suppose that you would like to findViewById on the v argument that was given to the onListItemClick method, instead of reinflating the layout.

That is, instead of

LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answer_screen,null);
EditText editText=(EditText)layout.findViewById(R.id.remark);

try something like

EditText editText=(EditText)v.findViewById(R.id.remark);

You might need to make the v variable final, in order for this to work. I hope this helps.



来源:https://stackoverflow.com/questions/8398375/value-of-edittext-in-custom-dialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!