0

I have a ArrayAdapter which is populated using a POJO class. The listview comprises of 2 layout. 1 is for a menuitem and one is for the category. The Listview with seperator is fine.

Later I tried to add a button in each menuitem row to edit the details in it. Here I have a problem when I tried to get the position of the row where the button is clicked.

I tried to display the position using the log. 1. If there is less number of rows and there is no need to scroll. the the log shows the correct position. 2. If i have more rows running into pages then the position in my log is not correct.

Could you please guid me to the line where my code needs correction ? Thanks in Advance

public class ConfirmAdapter  extends ArrayAdapter<POJO_ConfirmMenu> {
    private ArrayList<POJO_ConfirmMenu> ticketItem;  
    Context context;
    LayoutInflater vi; 
    public ConfirmAdapter(Context context ,ArrayList<POJO_ConfirmMenu> menu) {    
        super(context, 0, menu ); 
        this.ticketItem = new ArrayList<POJO_ConfirmMenu>();    
        this.ticketItem.addAll(menu); 
        this.context =context;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        }     
    private class ViewHolder {
        TextView ticketItem;   
        TextView type;  
        TextView quantity;  
        Button cancel,edit; 
        } 
    public boolean isEnabled(int position) { 
        if (ticketItem.get(position).getItemType().equals("menucategory")) 
            return false;
            return true;
    } 
    public int getItemViewType(int position) {
        if (ticketItem.get(position).getItemType().equals("menucategory")) 
            return 0;
            return 1;
    } 
    public int getViewTypeCount() {
        return 2;
    } 
    public View getView(final int position, View convertView, ViewGroup parent) {      
        ViewHolder holder;   
        int type = getItemViewType(position);  
        if (convertView == null) { 
            holder = new ViewHolder();  
            switch (type) {
            case 0:
                convertView = vi.inflate(R.layout.group,  null); 
                holder.ticketItem = (TextView) convertView.findViewById(R.id.tvGroup);
                convertView.setBackgroundColor(Color.RED); 
                break;              
            case 1:
                convertView = vi.inflate(R.layout.confirmitem,  null); 
                holder.ticketItem = (TextView) convertView.findViewById(R.id.tvConfirmItem); 
                holder.quantity  = (TextView) convertView.findViewById(R.id.tvQuantity); 
                holder.cancel    = (Button)   convertView.findViewById(R.id.bCancel); 
                holder.edit      = (Button)   convertView.findViewById(R.id.bEdit);   
                                 holder.edit.setTag(position);  
                //   Edit button
                holder.edit.setOnClickListener(new View.OnClickListener() {  
                    public void onClick(View v) {   
                        int pos = (Integer) v.getTag();

                        Log.i("ConfirmAdapter ","Order       Edit @ position : " + pos); 
                        }       
                });   
                break; 
             }              convertView.setTag(holder); 
        } else {
            holder = (ViewHolder) convertView.getTag();  
        }     
        switch (type) {
        case 0:         
            holder.ticketItem.setText(ticketItem.get(position).getTicketItemObject().getCategoryName()) ; 
            convertView.setBackgroundColor(Color.RED);  
            break;              
        case 1:
            holder.ticketItem.setText(ticketItem.get(position).getTicketItemObject().getName());
            holder.quantity.setText(Integer.toString(ticketItem.get(position).getTicketItemObject().getItemCount())); 
            break; 
         } 
        return convertView; 
        } 
}  

}

1 Answer 1

4

Within getView() method you have to set tag to your button and when you click on button get the tag within integer, it will return you correct position of your button click, something like below.

else {
        holder = (ViewHolder) convertView.getTag();  
}   
holder.edit.setTag(position);  //to get the orignal position later in onClick() of button
holder.edit.setOnClickListener(new View.OnClickListener() {  
          public void onClick(View v) {   
                 int pos = (Integer) v.getTag();  //the real and updated position
             Log.i("ConfirmAdapter ","Order Edit @ position : " + pos); 
          }       
});  

Updated

Note: after getting convert-view tag, set the tag to your button and handle click even too.

What is the main purpose of setTag() getTag() methods of View?

Sign up to request clarification or add additional context in comments.

4 Comments

Robin. I used the Set and getTag but still I get the wrong position. I have updated the code in the question
If i put the setTag after convertView.getTag(). I am getting the error (NullPointerException) in the line holder.edit.setTag(position);
Robin.. I have pasted the code from the link. I got the error when the control reaches the line holder.edit.setTag(position); (line number 366 ).. I have added the error log in the link pastie.org/5620823
As you Said.. It works :) Now I understand why it was not working before.. Thanks for your knowledge / time / dedication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.