如何以编程方式将项目添加到recyclerview?

时间:2018-02-06 11:29:24

标签: android android-recyclerview recycler-adapter

我正在尝试使用回收站和卡片视图实现添加到购物车功能。我的目录人口稠密,一切都很好。或者我认为,我现在需要的是允许用户在点击某个按钮时将项目添加到他们的购物车。

我尝试使用意图和共享偏好但总是传递一个项目。我不知道我做错了什么。这是我的适配器,detailsActivity,其中一个项目添加到购物车,购物车活动和模式我分别使用。

请参阅我的代码:

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.myHolder> {
private final List<CartItems> list;
private Context context;

CartAdapter(List<CartItems> list,Context context) {
    this.list = list;
    this.context = context;
}

@Override
public myHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_list, parent, false);
    return new myHolder(v);
}

@Override
public void onBindViewHolder(final myHolder holder,final int position) {
    final CartItems listItem = list.get(position);
    holder.setIsRecyclable(true);
    holder.item_name.setText(listItem.getItem());
    holder.quan.setText(listItem.getQuan());
    holder.cost.setText(listItem.getCost());
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}

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


static class myHolder extends RecyclerView.ViewHolder {

    final TextView item_name;
    final TextView quan;
    final TextView cost;
    final CardView cardView;
    final ImageView imageView;


    myHolder(View itemView) {
        super(itemView);
        item_name = itemView.findViewById(R.id.ph_no);
        quan = itemView.findViewById(R.id.tv_year);
        cost = itemView.findViewById(R.id.msg);
        cardView = itemView.findViewById(R.id.card_view);
        imageView = itemView.findViewById(R.id.cart_image);
    }
}
void addItems(List<CartItems> list,int position){
    CartItems cartItems = new CartItems();
    cartItems.setCost(cartItems.getCost());
    cartItems.setImageId(cartItems.getImageId());
    cartItems.setItem(cartItems.getItem());
    cartItems.setQuan(cartItems.getQuan());
    list.add(position, cartItems);
    notifyItemInserted(position);
    notifyDataSetChanged();
}}

DetailsActivity;

public class DetailsActivity extends BaseActivity {
RoundedImageView imageView;
AppCompatTextView textView, textView1, price, quantity, multiplier;
AppCompatImageButton btn_add, btn_sub;
FloatingActionButton fabAdd;
int c = 1;
private RecyclerView rvList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_details);
    init();

    String pathString = String.valueOf(getIntent().getStringExtra("text"));
    final String image = getIntent().getStringExtra("image");
    final String quan = getIntent().getStringExtra("quantity");
    final String pric = getIntent().getStringExtra("price");

    imageView.setBackgroundResource(Integer.parseInt(image));
    textView.setText(pathString);
    quantity.setText(MessageFormat.format("{0}{1}", getApplicationContext().getResources().getString(R.string.quan_tag), quan));
    price.setText(MessageFormat.format("{0}{1}", getApplicationContext().getResources().getString(R.string.price_tag), pric));
    btn_sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView1.setText("");
            c--;
            if (c < 1) {
                textView1.setText(String.valueOf(1));
                c = 1;
            } else if (c > 0) {
                textView1.setText(String.valueOf(c));
                String pri = price.getText().toString();
                String Price = pri.replace("ksh", "");
                String Pric = Price.replace("Price:", "");
                String sub = pric.replace("ksh", "");
                price.setText(MessageFormat.format("{0}ksh", String.valueOf(getApplicationContext().getResources().getString(R.string.price_tag) + (Integer.parseInt(Pric) - Integer.parseInt(sub)))));
                multiplier.setText(String.valueOf(c));

            }
        }
    });
    btn_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView1.setText("");
            c++;
            textView1.setText(String.valueOf(c));
            String Price = pric.replace("ksh", "");
            price.setText(MessageFormat.format("{0}ksh", String.valueOf(getApplicationContext().getResources().getString(R.string.price_tag) + Integer.parseInt(Price) * c)));
            multiplier.setText(String.valueOf(c));
        }
    });
    fabAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String cost = price.getText().toString();
            String quan = quantity.getText().toString();
            String item = textView.getText().toString();

            Intent intent = new Intent(getApplicationContext(), CartActivity.class);
            intent.putExtra("item",item);
            intent.putExtra("cost",cost);
            intent.putExtra("quan",quan);
            intent.putExtra("image",image);

            List<CartItems> list = new ArrayList<>();
            CartItems listItem;
            listItem = new CartItems();
            listItem.setItem(item);
            listItem.setQuan(quan);
            listItem.setCost(cost);
            listItem.setImageId(Integer.parseInt(image));
            list.add(listItem);

            CartAdapter listAdapter = new CartAdapter(list,getApplication());
            listAdapter.addItems(list,list.size());
            startActivity(intent);
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish(); //here add your back function code.
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void init() {
    imageView = findViewById(R.id.item_image);
    textView = findViewById(R.id.item_name);
    btn_add = findViewById(R.id.btn_add);
    btn_sub = findViewById(R.id.btn_sub);
    textView1 = findViewById(R.id.tv);
    price = findViewById(R.id.price);
    quantity = findViewById(R.id.quan);
    multiplier = findViewById(R.id.quan_true);
    fabAdd = findViewById(R.id.fab);
}}

CartActivity;

public class CartActivity extends BaseActivity {
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView rvList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_cart);

    rvList = (RecyclerView) findViewById(R.id.rvList);

    List<CartItems> list = new ArrayList<>();
    CartItems listItem;
    listItem = new CartItems();
    String cost = getIntent().getStringExtra("item");
    String quan = getIntent().getStringExtra("quan");
    String item = getIntent().getStringExtra("cost");
    String image = getIntent().getStringExtra("image");

    listItem.setItem(item);
    listItem.setQuan(quan);
    listItem.setCost(cost);
    listItem.setImageId(Integer.parseInt(image));
    list.add(listItem);

    CartAdapter listAdapter = new CartAdapter(list,getApplicationContext());
    listAdapter.notifyDataSetChanged();
    listAdapter.addItems(list,list.size());
    rvList.setAdapter(listAdapter);
    rvList.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}}

最后是模态类

class CartItems extends ArrayList<String> {

private String Item, Quan, Cost;
private int ImageId;

String getItem() {
    return Item;
}

void setItem(String item) {
    Item = item;
}

String getQuan() {
    return Quan;
}

void setQuan(String quan) {
    Quan = quan;
}

String getCost() {
    return Cost;
}

void setCost(String cost) {
    Cost = cost;
}

int getImageId() {
    return ImageId;
}

void setImageId(int imageId) {
    ImageId = imageId;
}}

0 个答案:

没有答案