回收视图不会收到捆绑数据

时间:2017-05-08 18:35:46

标签: android android-recyclerview bundle

我试图将产品详细信息活动中的捆绑包传递到带有回收视图的购物车但是当我点击添加到购物车时,购物车已打开但仍然是空的我不知道&#39 ; s代码错误

这是第一个活动

public class product_details extends AppCompatActivity {
ImageView product_img, share;
TextView name, price;
public int img;
public int price1;
public String name1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_details);
    product_img = (ImageView) findViewById(R.id.Product_Img);
    name = (TextView) findViewById(R.id.product_name);
    price = (TextView) findViewById(R.id.product_price);
    share = (ImageView) findViewById(R.id.share);
    img = getIntent().getIntExtra("img", 00);
    Glide.with(this).load(img).into(product_img);
    name1 = getIntent().getStringExtra("name");
    name.setText(name1);
    price1 = getIntent().getIntExtra("price", 0);
    price.setText(String.valueOf(price1));
    ImageView cart = (ImageView) findViewById(R.id.add_cart);
    cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(v.getContext(), CartActivity.class);
            Bundle extras = new Bundle();
            extras.putString("price1", String.valueOf((price1)));
            extras.putString("name1", name1);
            extras.putString("img1",String.valueOf(img));
            intent.putExtras(extras);
            startActivity(intent);

        }

我的第二个活动课

public class CartRecycler {
int images_id;
String productname,productprice,productquantity;

public String getProductquantity() {
    return productquantity;
}

public CartRecycler(int images_id , String productname , String productprice , String productquantity){
    this.setImages_id(images_id);
    this.setProductname(productname);
    this.setProductprice(productprice);
    this.setProductquantity(productquantity);

}

public CartRecycler(String productname, int images_id, String productprice) {
    this.productname = productname;
    this.images_id = images_id;
    this.productprice = productprice;
}

public int getImages_id() {
    return images_id;
}

public void setImages_id(int images_id) {
    this.images_id = images_id;
}

public String getProductname() {
    return productname;
}

public void setProductname(String productname) {
    this.productname = productname;
}

public String getProductprice() {
    return productprice;
}
public void setProductprice(String productprice) {
    this.productprice = productprice;
}

public void setProductquantity(String productquantity) {
    this.productquantity = productquantity;
}

}

购物车适配器

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
ArrayList<CartRecycler> adapterlist  = new ArrayList<>();
CartActivity cartactivity;
Context cnx;
public CartAdapter(ArrayList<CartRecycler> adapterlist,Context cnx){
    this.adapterlist =adapterlist;
    this.cnx =cnx;
    cartactivity = (CartActivity) cnx;
}
@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_cards,parent,false);
    CartViewHolder cartviewholder = new CartViewHolder(v,cartactivity);

    return cartviewholder;
}

@Override
public void onBindViewHolder(CartViewHolder holder, int position) {

    holder.image.setImageResource(adapterlist.get(position).getImages_id());
    holder.name.setText(adapterlist.get(position).getProductname());
    holder.price.setText(adapterlist.get(position).getProductprice());
    holder.quantity.setText(adapterlist.get(position).getProductquantity());
    if(!cartactivity.edit_mode){
        holder.checkornot.setVisibility(View.GONE);
    }
    else {
        holder.checkornot.setVisibility(View.VISIBLE);
        holder.checkornot.setChecked(false);

    }
}


@Override
public int getItemCount() {
    return adapterlist.size();
}



public static class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView image;
    TextView price,quantity,name;
    CheckBox checkornot;
    CartActivity cartactivity;
    CardView cardv;
    public CartViewHolder(View itemView, CartActivity cartactivity) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.cartimage);
        name = (TextView) itemView.findViewById(R.id.pn);
        price = (TextView) itemView.findViewById(R.id.p);
        quantity = (TextView) itemView.findViewById(R.id.q);
        checkornot = (CheckBox) itemView.findViewById(R.id.ckbox);
        this.cartactivity= cartactivity;
        cardv = (CardView) itemView.findViewById(R.id.cardd);
        cardv.setOnLongClickListener((View.OnLongClickListener) cartactivity);
        checkornot.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        cartactivity.prepareselection(view,getAdapterPosition());
    }
}
public void updateadapter(ArrayList<CartRecycler> list){
    for(CartRecycler cartRecycler:list){
        adapterlist.remove(cartRecycler);
    }
    notifyDataSetChanged();
}

}

第二项活动

public class CartActivity extends AppCompatActivity implements View.OnLongClickListener{
Button l;
ImageView imv;
Toolbar t;
RecyclerView rv;
RecyclerView.LayoutManager layoutmanager;
RecyclerView.Adapter adapter;
ArrayList<CartRecycler> cartitems = new ArrayList<>();
ArrayList<CartRecycler> selected_items_list = new ArrayList<>();
int countt =0;
boolean edit_mode =false;
TextView counterr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    t = (Toolbar) findViewById(R.id.toolb);
    setSupportActionBar(t);
    getSupportActionBar().setDisplayShowTitleEnabled(false);





    l = (Button) findViewById(R.id.checkoutsummary);
    l.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent n = new Intent(CartActivity.this,SummaryActivity.class);
            startActivity(n);
        }
    });
    rv = (RecyclerView) findViewById(R.id.mycartrecycler);
    layoutmanager = new LinearLayoutManager(this);
    rv.setLayoutManager(layoutmanager);
    rv.setHasFixedSize(true);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String price = extras.getString("price1");
        String img1 = extras.getString("img1");
        int imag1 = Integer.parseInt(img1);
        String name = extras.getString("name1");


        for (int i = 0; i < cartitems.size(); i++) {
            CartRecycler cart = new CartRecycler(name, imag1, price);
            cartitems.add(cart);
            adapter.notifyDataSetChanged();
            i++;

        }

    } else {
        Intent intent = new Intent(this, test.class);
        startActivity(intent);
    }



    adapter = new CartAdapter(cartitems,CartActivity.this);
    rv.setAdapter(adapter);

    counterr = (TextView) findViewById(R.id.selecteditemcounter);
    counterr.setVisibility(View.GONE);
}

@Override
public boolean onLongClick(View view) {
    t.getMenu().clear();
    t.inflateMenu(R.menu.cart_edit_mode);
    counterr.setVisibility(View.VISIBLE);
    edit_mode = true;
    adapter.notifyDataSetChanged();

    return true;
}
public void prepareselection(View view,int position){

    if(((CheckBox)view).isChecked()){
        selected_items_list.add(cartitems.get(position));
        countt = countt+1;
        updatecounter(countt);
    }
    else {
        selected_items_list.remove(cartitems.get(position));
        countt = countt -1;
        updatecounter(countt);
    }

}
public void updatecounter(int counter){
    if(counter==0){
        counterr.setText("0 items selected");
    }
    else {
        counterr.setText(counter+" item selected");
    }
}
public boolean onOptionsItemSelected(MenuItem item){
    if(item.getItemId()==R.id.item_delete){
        CartAdapter cartAdapter = (CartAdapter) adapter;
        cartAdapter.updateadapter(selected_items_list);

    }

    else if(item.getItemId()== android.R.id.home)
    {
        adapter.notifyDataSetChanged();
    }
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

for(int i = 0; i&lt; cartitems.size(); i ++)此循环不会运行,直到且除非cartitems的大小大于0,因此其范围内的所有代码都将永远不会被执行