如何在循环Android中专门为循环放置警告对话框

时间:2013-01-03 06:02:31

标签: java android

我想为不同的联系人添加不同的对话框.. !!我想在以下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();
}

1 个答案:

答案 0 :(得分:5)

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

只需将联系人的联系传递给alertforcall()方法,如果要在条件基础上显示对话框,请在 AlertDialog ..

例如,

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