在自定义对话框的EditText价值 [英] Value of EditText in Custom Dialog

查看:198
本文介绍了在自定义对话框的EditText价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ListActivity 的onClick 的每一项的自定义对话框出现。自定义对话框包含微调和的EditText现在我不能够得到的EditText的价值,同时EditText上的调试值来为空白或。

 保护无效onListItemClick(ListView中升,视图V,INT位置,长的ID)
    {
    super.onListItemClick(L,V,位置ID);
    positionList =位置;    HashMap的<弦乐,对象> TEMP =(HashMap的<弦乐,对象>)list.get(位置);    LayoutInflater吹气=(LayoutInflater)CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    查看布局= inflater.inflate(R.layout.answer_screen,NULL);    AlertDialog.Builder alertDialog =新AlertDialog.Builder(CommonScreen6.this);
    alertDialog.setTitle(选择选项);
    alertDialog.setView(布局);    微调answerList =(微调)layout.findViewById(R.id.spinnerAnswerList);    ArrayAdapter<串GT; adapterAnswerType =新ArrayAdapter<串GT; (CommonScreen6.this,android.R.layout.simple_spinner_item,(ArrayList的<串GT;)temp.get(Constants.answerDesc));    adapterAnswerType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);    answerList.setAdapter(adapterAnswerType);
    answerList.setOnItemSelectedListener(本);    ArrayList的<串GT; answerDescList =(ArrayList的<串GT;)temp.get(Constants.answerDesc);    answerList.setSelection(answerDescList.indexOf(temp.get(Constants.defaultAnswerDesc)));    alertDialog.setPositiveButton(提交,新DialogInterface.OnClickListener()
      {    @覆盖
    公共无效的onClick(DialogInterface对话,诠释其)
               {
      LayoutInflater吹气=(LayoutInflater)CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
      查看布局= inflater.inflate(R.layout.answer_screen,NULL);
      EDITTEXT的EditText =(EditText上)layout.findViewById(R.id.remark);
      字符串的话= editText.getText()的toString()。      HashMap的<弦乐,对象> TEMP =(HashMap的<弦乐,对象>)list.get(positionList);
            temp.put(Constants.remark,备注);
            adapter.notifyDataSetChanged();
           }
  });
 }


解决方案

这是你没有得到任何值的原因是因为你reinflating读取的值之前的布局,从而重置价值为默认值。
我想你想findViewById而不是reinflating布局上的 v 说法,是考虑到onListItemClick方法。

也就是说,而非

  LayoutInflater吹气=(LayoutInflater)CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.answer_screen,NULL);
EDITTEXT的EditText =(EditText上)layout.findViewById(R.id.remark);

尝试像

 的EditText EDITTEXT =(EditText上)v.findViewById(R.id.remark);

您可能需要做出的 v 变量最后,为了这个工作。
我希望这有助于。

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

解决方案

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.

这篇关于在自定义对话框的EditText价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆