Hi everyone i am using custom listview for getting data from server and show in listview.. I am able to get data and show it in listview but i dont know to implement the click event of button inside listitem. There are two buttons to increement and decrement qty. My clicklistener is working but its not working in right manner. Please help me correcting this issue. I did search too many postrs in stack overflow but was unable to understand it...
Here is my adapter class
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;
}
This is my FeedItem class:
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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…