大家好我正在使用自定义列表视图从服务器获取数据并在listview中显示..我能够获取数据并在listview中显示它但我不知道在listitem中实现按钮的click事件。有两个按钮来递增和递减数量。我的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;
}
答案 0 :(得分:1)
目前,每个活动有1个变量来存储数量。如果您希望按项目存储,则应添加:int qty;
以成为FeedItem
类中的成员。
卸下:
convertView.setTag(Integer.valueOf(id));
和plus类似,您需要添加:
minus.setTag(item.getId());
然后代替qty++;
或基本上在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);
和plus
相同答案 1 :(得分:1)
最后我解决了我自己的回答。只需要在片段类中实现接口和调用..
更新了适配器类
public class FeedListAdapter extends RecyclerView.Adapter<FeedListAdapter.ViewHolder> {
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
private List<FeedItem> filteredfeedItems;
ImageView plus;
ImageView minus;
String result;
String formattedDate;
int id;
private View.OnClickListener onAddNum;
private View.OnClickListener onSubNum;
public FeedListAdapter(Activity activity, List<FeedItem> feedItems)
{ this.activity = activity;
this.feedItems = feedItems;
this.filteredfeedItems = feedItems;
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// create interface method
public void setOnAddNum(View.OnClickListener onAddNum){
this.onAddNum = onAddNum;
}
public void setOnSubNum(View.OnClickListener onSubNum){
this.onSubNum = onSubNum;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feed_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
FeedItem item =(FeedItem)filteredfeedItems.get(position);
holder.date.setText(item.getObservation());
holder.initiator
.setText(item.getObservation());
holder.location.setText(String.valueOf(item.getNum())+"");
holder.assignTo.setText(item.getSummary());
// set tag to know which button you clicked'
holder.plus.setTag(position);
holder.plus.setFocusable(true);
holder.plus.setClickable(true);
// set callback interface ,
holder.plus.setOnClickListener(onAddNum);
holder.minus.setTag(position);
holder.minus.setOnClickListener(onSubNum);
}
@Override
public int getItemCount() {
return filteredfeedItems.size();
}
public long getItemId(int position) {
return position;
}
class ViewHolder extends RecyclerView.ViewHolder{
ImageView plus;
ImageView minus;
TextView date;
TextView initiator;
TextView assignTo;
TextView location;
public ViewHolder(View vi) {
super(vi);
date = (TextView) vi.findViewById(R.id.name);
initiator = (TextView) vi
.findViewById(R.id.timestamp4);
assignTo = (TextView) vi.findViewById(R.id.timestamp);
location = (TextView) vi.findViewById(R.id.timestamp2);
plus = (ImageView) vi.findViewById(R.id.btnAddToCart1);
minus = (ImageView) vi.findViewById(R.id.btnAddToCart5);
}
}
}
现在在activity类中为按钮事件实现View.OnClickListener。
public class MyFragment extends Fragment implements View.OnClickListener
@Override
public void onClick(View view) {
Object tag = view.getTag();
switch (view.getId()){
case R.id.item_btn_add: // click add buttons number , performs corresponding processing
if (tag != null && tag instanceof Integer) {
int position = (Integer) tag;
Your pojo class
int num =feedItems.get(position).getNum();
num++;
feedItems.get(position).setNum(num);
adapter.notifyDataSetChanged();
}
break;
case R.id.btnAddToCart5:
if (tag != null && tag instanceof Integer) {
int position = (Integer) tag;
int num =feedItems.get(position).getNum();
if (num>0) {
num--;
feedItems.get(position).setNum(num);
listAdapter.notifyDataSetChanged();
}
else{
num= 0;
listAdapter.notifyDataSetChanged();
}
}
break;
}
享受编码
答案 2 :(得分:0)
像这样创建一个MyListItem类:
public class MyListItem{
private int qty;
private int itemId;
public void setQty(int qty)
{
this.qty=qty;
}
public void setQty(int qty)
{
this.qty=qty;
}
public int getItemId()
{
return this.itemId;
}
public int getQty()
{
return this.qty;
}
public MyListItem(int qty,int itemId)
{
this.qty=qty;
this.itemId=itemId;
}
}
加载项目时,将所有项目初始化为 MyListItem 。
您的监听器应该更改对象的数量值。
现在制作一个MyListItem
不是阵列的数组列表,因为您不知道当时有多少项目
希望这个帮助