Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

listview - Android - Unable to increment or decrement value from list item

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;
    }

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...