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 cli
Finally i solved my answer my self. Just need to implement interface and call in fragment class..
Updated Adapter class
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);
}
}
}
now in activity class implement View.OnClickListener for button events.
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;
}
Enjoy coding
make a class MyListItem like this :
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;
}
}
when you load the items initialize all of them to MyListItem.
your listener should change the object qty value.
now make an arrayList of MyListItem
NOT AN ARRAY due to you don't know how many items you have at the time
hope this help
Currently you have 1 variable per activity to store the quantity. If you want to store it per item, you should add: int qty;
to be a member in FeedItem
class.
Remove:
convertView.setTag(Integer.valueOf(id));
and similar to plus, you'll need to add:
minus.setTag(item.getId());
Then instead of qty++;
or basically in every place in the onClick instead of qty
you'll need to use item.qty
:
Integer id = view.getTag();
FeedItem item = filteredfeedItems.get(id);
item.qty++;
location.setText(String.valueOf(item.qty));
Update:
I think you're mixing FeedItem.id with position. If you're not changing the order / amount of items in the list then you could ALWAYS use position. Instead of:
minus.setTag(item.getId());
try:
minus.setTag(position);
and the same for plus