Android: Calling a dialog from a button imbedded in a list
I have a custom List that contains two TextViews
and two Buttons
. I want one button to change the data shown in one TextView
and I want the other button
to inflate a Dialog
explaining the purpose of that row. So I need to dynamically update the Dialog
content. As far as I can tell the only way to imbued a clickable button in a list is to setOnClickListener
in an adapter when you inflate the row, but I can't create a dialog
outside of the activity
(I'm getting a Force Close). Here's my getView()
call. Any suggestions?
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView=mInflater.inflate(R.layout.text_list_item,null);
holder=new ViewHolder();
holder.clear=(Button)convertView.findViewById(R.id.btnClr);
holder.label=(TextView) convertView.findViewById(R.id.textListItemLabel);
holder.value=(TextView) convertView.findViewById(R.id.textListItemValue);
holder.info=(Button)convertView.findViewById(R.id.btnInfo);
holder.group= (RadioGroup)convertView.findViewById(R.id.radiogroup);
holder.r1=(RadioButton)convertView.findViewById(R.id.radio1);
holder.r2=(RadioButton)convertView.findViewById(R.id.radio2);
holder.t1=(ToggleButton)convertView.findViewById(R.id.toggle1);
holder.clear.setOnClickListener(new OnClickListener(){
private int pos= position;
@Override
public void onClick(View v){
holder.value.setText(String.valueOf(pos));
notifyDataSetChanged();//I know there's a problem on here, and I'm working on that... but at least it reacts to the button press.
}
});
holder.info.setOnClickListener(new OnClickListener(){
private int pos= position;
@Override
public void onClick(View v){
Button button = (Button) v;
AlertDialog.Bui开发者_运维知识库lder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to display?");
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create();//Ok to here...
builder.show();//Crash
String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
The Activity that containing the adapter can't handle it's own lifecycle - Activity.onPause()
when there is a request from a contained object such this adapter to cover the activity with Dialog.
So to handle it, move all the AlertDialog
code to the activity inside a new method which will be called by pressing the button.
you can do that by fill the argument of the attribute android:onClick
in the button xml's definition with the name of the method as mentioned before.
like here: http://androidforbeginners.blogspot.com/2010/03/clicking-buttons-in-listview-row.html
精彩评论