我有 ListActivity ,每个项目的 onClick 都会出现一个 Custom Dialog。Custom Dialog 包含微调器和 EditText现在我无法获得 EditText 的值,而 EditText 的调试值显示为空白或“”。
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();
}
});
}
最佳答案
您没有获得任何值的原因是因为您在读取值之前重新填充布局,从而将值重置为默认值。
我想您希望在提供给 onListItemClick 方法的 v
参数上查找 ViewById,而不是重新展开布局。
也就是代替
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);
尝试类似的东西
EditText editText=(EditText)v.findViewById(R.id.remark);
您可能需要将 v
变量设置为最终变量,以使其正常工作。
我希望这会有所帮助。
关于android - 自定义对话框中EditText的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398375/