Android – 无法从列表项增加或减少值
作者:互联网
大家好我正在使用自定义列表视图从服务器获取数据并在列表视图中显示..我能够获取数据并在列表视图中显示它但我不知道在listitem中实现按钮的单击事件.有两个按钮来递增和递减数量.我的clicklistener正在工作,但它没有以正确的方式工作.请帮我纠正这个问题.我在堆栈溢出中搜索了太多的邮件但是无法理解它…
这是我的适配器类
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
private List<FeedItem> filteredfeedItems;
ImageView plus;
ImageView minus;
int qty = 0;
String result;
String formattedDate;
int id;
public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
this.filteredfeedItems = feedItems;
}
@Override
public int getCount() {
return filteredfeedItems.size();
}
@Override
public Object getItem(int position) {
return filteredfeedItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.feed_item, null);
FeedItem item = filteredfeedItems.get(position);
id = item.getId();
convertView.setTag(Integer.valueOf(id));
TextView date = (TextView) convertView.findViewById(R.id.name);
TextView initiator = (TextView) convertView
.findViewById(R.id.timestamp4);
TextView assignTo = (TextView) convertView.findViewById(R.id.timestamp);
final TextView location = (TextView) convertView.findViewById(R.id.timestamp2);
//TextView category = (TextView) convertView.findViewById(R.id.category);
data.setText(item.getData();
price
.setText(item.getPrice());
description.setText(item.getDescription());
plus = (ImageView) convertView.findViewById(R.id.btnAddToCart1);
plus.setTag(item.getId());
plus.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int position = (Integer) view.getTag();
//convertView.setTag(Integer.valueOf(id));
FeedItem item = filteredfeedItems.get(position);
item.quantity++;
location.setText(String.valueOf(item.quantity));
}
});
minus = (ImageView) convertView.findViewById(R.id.btnAddToCart5);
minus.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Integer taggedPosition = (Integer) view.getTag();
if (qty>0){
//minus.setImageResource(R.drawable.minus_pressed);
qty--;
location.setText(String.valueOf(qty));
}
else{
qty = 0;
}
}
});
return convertView;
}
这是我的FeedItem类:
public class FeedItem {
int quantity = 0;
int id;
public FeedItem(int id,int quantity){
super();
this.id = id;
this.quantity = quantity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
解决方法:
目前,每个活动有1个变量来存储数量.如果你想为每个项目存储它,你应该添加:int qty;成为FeedItem类的成员.
去掉:
convertView.setTag(Integer.valueOf(id));
和plus类似,你需要添加:
minus.setTag(item.getId());
而不是数量;或者基本上在onClick而不是qty的每个地方你都需要使用item.qty:
Integer id = view.getTag();
FeedItem item = filteredfeedItems.get(id);
item.qty++;
location.setText(String.valueOf(item.qty));
更新:
我认为你将FeedItem.id与位置混合在一起.如果您没有更改列表中的项目顺序/数量,那么您可以始终使用位置.代替:
minus.setTag(item.getId());
尝试:
minus.setTag(position);
和加号相同
标签:android,listview,baseadapter 来源: https://codeday.me/bug/20190927/1824293.html