1

我想为不同的联系人添加不同的对话框..!! 我想在下面的for循环中创建,这是我的代码..!!

public class CallActivity extends Activity{

    sqlAdapter adapter;
    Context mContext; 
    String Birthdate, CurrentDate, ctName = "Unknown", phNumber = "Unknown";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder);

        Calendar cal = Calendar.getInstance();
        int date = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH)+1;

/* 获取当前日期 */

    if(date<10 && month<10){
        CurrentDate = "0"+month+"-0"+date;
        Toast.makeText(getBaseContext(), "CurrentDate : " + CurrentDate, Toast.LENGTH_LONG).show(); 
        }else if(month<10 && date>10){
            CurrentDate = "0"+month+"-"+date;
            Toast.makeText(getBaseContext(), "CurrentDate : " + CurrentDate, Toast.LENGTH_LONG).show(); 
        }else if(month>9 && date<10){
            CurrentDate = month+"-0"+date;
            Toast.makeText(getBaseContext(), "CurrentDate : " + CurrentDate, Toast.LENGTH_LONG).show(); 
        }else {
            CurrentDate = month+"-"+date;
            Toast.makeText(getBaseContext(), "CurrentDate : " + CurrentDate, Toast.LENGTH_LONG).show(); 
        }

    adapter = new sqlAdapter(getBaseContext());
    adapter.openToRead();
    ArrayList<ContactDetails> cDetails = adapter.getContactDetails(CurrentDate);
    adapter.close();

/* 这是 for 循环,我想为不同的联系人生成警报对话框 */

    for (int i = 0; i < cDetails.size(); i++) {
        ctName = cDetails.get(i).Name;
        adapter.openToRead();
        phNumber = adapter.getcNumber(ctName);
        adapter.close();
        alertforcall(getBaseContext(),ctName,phNumber);
    }   
}

public void alertforcall(Context context, String Name, final String ctNumber){
    mContext = context;

    AlertDialog alert = new AlertDialog.Builder(CallActivity.this).create();
    alert.setIcon(R.drawable.ic_launcher);
    alert.setTitle("");
    alert.setMessage("Are you sure want to call " + Name +"? " );

    alert.setButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            finish();

        }
    });

    alert.setButton2("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String phoneNumber = "tel:" + ctNumber;
            Intent intent = new Intent(Intent.ACTION_CALL, Uri
                    .parse(phoneNumber));
            startActivity(intent);
            finish();           
        }
    });
    alert.show();
}
4

1 回答 1

5

不好的做法,在for 循环中创建对话框不是一个好的编程习惯。

只需将联系人的 ArrayList传递给alertforcall()方法,当您想在条件基础上显示对话框时,请在该AlertDialog中设置名称和联系人。

例如,

现在,当您想显示特定联系人的警报对话框时,我假设您正在单击联系人列表,因此通过获取特定列表项的名称和编号并将其传递给alertforcall()方法,您可以显示特定的警报对话框。

于 2013-01-03T06:07:53.000 回答