public class MyCustomAdapter extends BaseAdapter {
Context context;
String[] result;
int[] imageId;
String[] Cost;
static int counter = 0;
private static LayoutInflater inflater = null;
public MyCustomAdapter(Context mainActivity, String[] result, int[] imageId, String[] Cost
) {
// TODO Auto-generated constructor stub
this.result = result;
this.imageId = imageId;
this.Cost = Cost;
this.context = mainActivity;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
TextView cost;
ImageView img;
ImageView plusitem;
ImageView minusitem;
TextView itemnumber;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.listitem, null);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
holder.cost = (TextView) rowView.findViewById(R.id.textView2);
holder.plusitem = (ImageView) rowView.findViewById(R.id.imageButton2);
holder.minusitem = (ImageView) rowView.findViewById(R.id.imageButton);
holder.itemnumber = (TextView) rowView.findViewById(R.id.textView4);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
holder.plusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.itemnumber.setText(String.valueOf( _counter));
//holder.itemnumber.setText(result[position]);
}
});
holder.minusitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.itemnumber.setText(String.valueOf( _counter));
// holder.itemnumber.setText(result[position]);
}
});
return rowView;
}}
这是我的适配器,我们有+按钮用于增加项目和 - 按钮 对于减少项目我必须得到第一项中的项目编号 如果我们有2项和第二项我们可以3,4 ..所以.. 现实我想在车上添加项目实际上我 尝试使用静态变量,它不能正常工作 请告诉我逻辑。看下面的屏幕
答案 0 :(得分:2)
创建设计中显示的Buttons
和TextView
。为onclick
编写Buttons
方法。初始化Integer
变量以保存计数。单击+按钮,将计数递增1并在Textview
中设置文本。同样,单击 - 按钮时,将计数减1并在TextView
中设置文本。
int count=0;
TextView txtCount =(TextView) findViewById(R.id.txt);
Button buttonInc= (Button) findViewById(R.id.button1);
Button buttonDec= (Button) findViewById(R.id.button2);
buttonInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count--;
txtCount.setText(String.valueOf(count));
}
});
答案 1 :(得分:0)
使用哈希映射。对于每个单击的项目,将项目ID存储为键,将点击的计数存储为值。
例如:<Item_id:item_count>
通过这种方式,您可以轻松管理用户的选择,并且比使用静态变量更有效,因为您只需要一个哈希映射。
答案 2 :(得分:0)
首先,您应该维护一个自定义类的arraylist,其中包含ItemId,ItemName,Price,Quantity,Image等字段。这样您就可以轻松地在购物车中维护数量和其他细节。
现在在适配器中你只需要传递这个arrayList,不需要传递不同的数组。
int quantity = arraylist.get(position).getItemQuantity();
btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quantity++;
tvQuantity.setText(quantity+"");
}
});
btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quantity--;
tvQuantity.setText(quantity+"");
}
});
这很有效......
答案 3 :(得分:0)
这是适配器添加到购物车中你可以在btnIncrease和btnDecrease上看到我改变购物车的价值而我使用notifyDataSetChanged();它会自动更改数据
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, cart, genre;
public Button btnIncrease,btnDecrease;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
cart = (TextView) view.findViewById(R.id.cart);
btnIncrease =view.findViewById(R.id.plus);
btnDecrease = view.findViewById(R.id.minus);
}
}
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Movie movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.cart.setText(String.valueOf(movie.getcart()));
holder.btnIncrease.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
movie.cart++;
notifyDataSetChanged();
}
});
holder.btnDecrease.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (movie.cart>0)
movie.cart--;
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
答案 4 :(得分:0)
holder.cart_minus_img.setOnClickListener(new View.OnClickListener() {
//1. List item
@Override
public void onClick(View v) {
int count= Integer.parseInt(String.valueOf(holder.cart_product_quantity_tv.getText()));
if(count>0){
//2. enter code here
count--;
holder.cart_product_quantity_tv.setText(String.valueOf(count));
}
}
});
holder.cart_plus_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count= Integer.parseInt(String.valueOf(holder.cart_product_quantity_tv.getText()));
count++;
holder.cart_product_quantity_tv.setText(String.valueOf(count));
}
});