伙计们我有一个自定义警报对话框,里面我正在给listView充气 我想访问该列表视图的子项,但它产生错误,任何人可以帮助这里是我的相关代码片段
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Context);
View alertView = AddExpenses.this.getLayoutInflater().inflate(R.layout.layout_alert_expenses, null);
int amount = Integer.parseInt(etExpensesAmount.getText().toString());
final ListView lvAlertDialogue = (ListView) alertView.findViewById(R.id.lv_alert_expenses);
final TextView tvTotal = (TextView) alertView.findViewById(R.id.tv_amount);
tvTotal.setText(etExpensesAmount.getText().toString());
CustomAdapterAlertList customAdapterAlertList = new CustomAdapterAlertList(AddExpenses.this, listSelectedContact, amount);
lvAlertDialogue.setAdapter(customAdapterAlertList);
alertDialogBuilder.setView(alertView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
final EditText etAmount;
View singleView = lvAlertDialogue.getChildAt(listSelectedContact.size()-1); //singleView is null here
答案 0 :(得分:0)
getChildAt()适用于列表视图中的可见项,否则将返回null。 看到你的代码,我想你想要最后一项。您可以尝试以下代码。
View singleView = lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition()):
答案 1 :(得分:0)
在您的代码中,您正在呼叫# | name | age | email |
--------------------------------
1 | Peter | 42 | something |
2 | Peter | 42 | null |
就在alertDialog.show();
lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition())
正在执行
当且仅当对话框可见时,getChildAt才会起作用。
所以它是空的。
试试这段代码。希望它会奏效。
getChildAt