为什么我不能在警告对话框中的答案中打开一个意图?

时间:2014-05-18 05:54:57

标签: android android-intent alertdialog

我想要做的是从警告对话框中回答“否”时从此活动中打开另一个活动,但它会一直显示此活动的空列表!我不知道为什么会发生这种情况..请有人帮助我吗?

public class CartList extends Activity {

    ListView list;

    static List<CartItems> detailsList = new ArrayList<CartItems>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cart_list);

        initializeComponents();

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CartList.this);


            alertDialogBuilder.setTitle("easy shopping");


            alertDialogBuilder
                .setMessage("Do you like to Continue shopping?")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        dialog.cancel();


                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                         launchIntent();

                    }


                });

                AlertDialog alertDialog = alertDialogBuilder.create();          
                alertDialog.show();

    }

    private void initializeComponents()
    {
        list=(ListView)findViewById(R.id.listView1);
    }


    private void SetAdapterList() 
    {
        // TODO Auto-generated method stub

        CartItems details;
         //List<CartItems> detailsList = new ArrayList<CartItems>();
         List<CartItems> testList=new ArrayList<CartItems>();
         try{
         Intent newintent = getIntent();
         final String itemname = newintent.getStringExtra("itemname");
        final String itemprice = newintent.getStringExtra("itemprice");
        final String itemquantity = newintent.getStringExtra("itemquantity");
        final double totalprice = newintent.getDoubleExtra("totalprice", 0.0);
        String totprice=String.valueOf(totalprice);

        final int imagehandler=newintent.getIntExtra("image", 0);
        Log.e("image handler",imagehandler+"");
        details = new CartItems(itemname,itemprice,itemquantity,totprice,imagehandler);
        detailsList.add(details);

        Log.e("detailslsit",detailsList+"");
         }
         catch(Exception e){
             e.printStackTrace();
         }

    }



  @Override
protected void onResume() {
      SetAdapterList();


    CustomAdapter adapter=new CustomAdapter(this,detailsList);

    list.setAdapter(adapter);

    super.onResume();
}  

  private void launchIntent() {
        // TODO Auto-generated method stub
      Intent intent=new Intent(getBaseContext(),PersonalInfo.class);
        startActivity(intent);
    }     
}

1 个答案:

答案 0 :(得分:1)

你把launchIntent()置于负面评论中,no它应该在positive button内,这是肯定的

<强>解决方案:

只需将其从正面切换为负面,反之亦然....

======================================

当你改变意图时不要使用baseContext只说this

改变这一点:

Intent intent=new Intent(getBaseContext(),PersonalInfo.class);

Intent intent=new Intent(this,PersonalInfo.class);