不显示太多代码:
我有一个Activity“A”。这通过 aActivity填充 a ,为每一行中的项目设置a 。ListViewBaseAdapterBaseAdaptergetView()onClickListener()
单击行的项目时,我想AlertDialog在“A”中显示一个。Activity
不显示太多代码:
我有一个Activity“A”。这通过 aActivity填充 a ,为每一行中的项目设置a 。ListViewBaseAdapterBaseAdaptergetView()onClickListener()
单击行的项目时,我想AlertDialog在“A”中显示一个。Activity
我看不出你在哪里遇到问题。如果您BaseAdapter在 A 中有您的版本,Activity那么您可以简单地调用(在您在 的方法中OnCLickListener设置的该行的项目中)您在 A 中创建的私有方法:getViewBaseAdapterActivity
private void showADialog(int position) {
new AlertDialog.Builder(this)
.setMessage("The clicked row is " + position)
.setPositiveButton("Ok?",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
}).show();
}
如果您的自定义与 ABaseAdapter不在同一个文件中,Activity那么您可以使用Context您传递给的BaseAdapter(如果您的适配器的代码是您上一个问题中的代码,如果不是,您应该在构造函数中传递它)。然后,您可以将其Context转换为您的活动并调用上一个方法showADialog()。此外,当您设置侦听器时,您应该将其position作为标签传递,以便您可以在其中检索它OnCLickListener(否则您可能会在视图 + 侦听器被回收时得到错误的位置):
//...
item.setTag(new Integer(position)); // or if you use a ViewHolder: holder.item bla.....
item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Integer realPosition = (Integer) v.getTag();
// Here show the dialog using one of the methods above.You either:
// showADialog(realPosition); // when the code for your custom BaseAdapter in in the same class as Activity A
//((CallingAlertDialogBaseAdapter) context).showADialog(realPosition); // when the code for the custom BaseAdapter is in a separate file then Activity A
//create directly an AlertDialog. For this you'll require a valid Context(from an Activity(getApplicationCOntext() will NOT work).
//You have this context from the parameter that you pass in the constructor of your custom BaseAdapter(as you ussualy pass this in the Activity where you instantiate the adapter)
}
});
//...