我无法获取我的代码来解析findViewById - 我有一个从自定义对话框中获取两个数据的方法,但如果我使用findViewById来尝试获取数据,我只是得到它可以解决它。
见摘录:
public class ItemsActivity extends ActionBarActivity {
ArrayList<Item> itemlistv = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
populateListView();
final Button addbutton = (Button) findViewById(R.id.button_add_item);
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(ItemsActivity.this);
LayoutInflater inflater = ItemsActivity.this.getLayoutInflater();
alert.setView(inflater.inflate(R.layout.dialog_additems, null))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
Editable itemname = (Editable)dialog.findViewById(R.id.itemname);
Editable itemprice = (Editable)dialog.findViewById(R.id.itemprice);
//String itemnamestring = itemname.getText().toString();
// itemlistv.add(new Item(itemname, itemprice));
populateListView();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// End of dialog
}
});
alert.show();
}
});
// etc
我只是不能让findViewById在这里工作,但我已经在其他课程中完成了我的程序。我犯了一个愚蠢的错误吗?
忽略两条注释行,我知道它们不正确 - 我正在关注为什么在这种情况下我无法获得findByViewId。
非常感谢
亚历
答案 0 :(得分:0)
请勿在“对话框”中搜索“查看”字段。做下面的事情 -
LayoutInflater inflater = ItemsActivity.this.getLayoutInflater();
final View modifyView = inflater.inflate(R.layout.dialog_additems, null);
final Editable itemname = (Editable)modifyView.findViewById(R.id.itemname);
final Editable itemprice = (Editable)modifyView.findViewById(R.id.itemprice);
alert.setView(modifyView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
//String itemnamestring = itemname.getText().toString();
// itemlistv.add(new Item(itemname, itemprice));
populateListView();
}
})
//your code