我不能用自定义适配器数据清除我的列表视图?

时间:2014-05-22 10:47:46

标签: android android-listview custom-adapter

我所拥有的是一个带有自定义适配器的列表视图,它从一个正常工作的数组中获取数据。我添加的是一个按钮,当我点击它时我想清除我的列表。但是它没有清楚清单......为什么会发生这种情况?

这里是我的列表代码:

public class CartList extends Activity {


     ArrayList <String> listmix=new ArrayList<String>();
    ListView list;
    CartItems newtest;
    static List<CartItems> detailsList = new ArrayList<CartItems>();
    double totalpricetext=0.0;
    TextView totalpricevalue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cart_list);
        totalpricevalue=(TextView)findViewById(R.id.totalpricevalue);
        Button clear=(Button)findViewById(R.id.button1);
        clear.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                detailsList.clear();
                  SetAdapterList();


                    CustomAdapter adapter=new CustomAdapter(CartList.this,detailsList);

                    list.setAdapter(adapter);
            }
        });
        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) {
//                       Intent intent=new Intent(CartList.this,EasyShopping.class);
//                      
//                      startActivity(intent);
                        dialog.cancel();


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

                         launchIntent();
                          // getlist();
                    }


                });

                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>();

         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);


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



  private void getdata() {
        // TODO Auto-generated method stub
      CartItems item;

      for(int i = 0; i < detailsList.size();i++){
      item = detailsList.get(i);
      String itemname=item.getItemName();
      listmix.add(itemname);
      String itemprice=item.getItemPrice();
      listmix.add(itemprice);
      String itemQuantity=item.getQuantity();
      listmix.add(itemQuantity);
      String itemtotalPrice=item.getTotalPrice();
      listmix.add(itemtotalPrice);
      Log.i("myLog","then name of item is "+item.getItemName()+"");
      Log.i("listmix",listmix+"");
      double doubleprice = Double.parseDouble(itemtotalPrice);
        totalpricetext=totalpricetext+doubleprice;
        totalpricevalue.setText("the total amount of your cart is "+totalpricetext+" JD");
      }
    }

@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(this,PersonalInfo.class);
      intent.putStringArrayListExtra("listmix", listmix);
        startActivity(intent);
    }     
}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

评论SetAdapterList()中的onClick,因为它正在重新填充您传递给ArrayList的{​​{1}}