Reputation: 3
Hello everyone, I just started with android and I am on my first project. I am not a developer base, so please excuse me if my language is not fit. That one week already that I'm stuck on this alert dialog function. I ask you to help me. Thank you!
public class Myclass extends Activity implements DialogInterface.OnClickListener{
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt);
tv=(TextView)findViewById(R.id.textView1);
tv.setOnClickListener(new TextView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog ad = new AlertDialog.Builder(tv.getContext())
.setMessage("Blah blah blah.\n Fine pring.\n Do you accept all our terms and conditions?")
.setIcon(R.drawable.ic_launcher)
.setTitle("Terms of Service")
.setPositiveButton("Yes", this)
.setNegativeButton("No", this)
.setCancelable(false)
.create();
ad.show();
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case DialogInterface.BUTTON_POSITIVE: // yes
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:" + tv.getText().toString()));
startActivity(call);
break;
case DialogInterface.BUTTON_NEGATIVE: // no
tv.setText("Oh lalala");
break;
default:
// nothing
break;
}
}
}
Upvotes: 0
Views: 1378
Reputation: 18068
Replace:
.setPositiveButton("Yes", this)
.setNegativeButton("No", this)
with
.setPositiveButton("Yes", Myclass.this)
.setNegativeButton("No", Myclass.this)
Because, that statement is inside onClick
of anonymous class of type TextView.OnClickListener
. So, you will get compile error, as you are trying to pass View.OnCLickListener
instead of DialogInterface.OnClickListener
By the way, you should at least have mentioned that you are getting compile error.
Upvotes: 2
Reputation: 1
You can not pass this which is ref of tv.setOnClickListener(new TextView.OnClickListener() to Dialog interface use this try this .this will work for sure.
.
setPositiveButton("Yes", Myclass.this)
.setNegativeButton("No", Myclass.this)
Upvotes: 0