OnClickListener无法正常使用Custom ListView中的按钮

时间:2014-05-29 11:01:08

标签: android onclicklistener custom-adapter

我有一个用于列表视图的自定义适配器。在列表视图中,我有2个文本视图和每行1个按钮。 我正在为这样的按钮设置点击监听器

public class CustomAdapter extends BaseAdapter   implements OnClickListener {

    /*********** Declare Used Variables *********/
    private PaymentsFragment frag;
    private ArrayList data;
    private static LayoutInflater inflater=null;
    public Resources res;
    ListModel tempValues=null;
    int i=0;

    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(PaymentsFragment customListView, ArrayList d,Resources resLocal) {

        /********** Take passed values **********/
        frag = customListView;
        data=d;
        res = resLocal;
        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)frag.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if(data.size()<=0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder to contain inflated xml file elements ***********/
    public static class ViewHolder{
        public TextView transfer_id;
        public TextView amount_payable;
        public Button btn_confirm;
        //public TextView textWide;
       // public ImageView image;

    }

    /*********** Depends upon data size called for each row , Create each ListView row ***********/
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        final ViewHolder holder;

        if(convertView==null){ 

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vi = inflater.inflate(R.layout.tabitem, null); 

            /******** View Holder Object to contain tabitem.xml file elements ************/
            holder=new ViewHolder();
            holder.transfer_id=(TextView)vi.findViewById(R.id.transfer_id);
            holder.amount_payable=(TextView)vi.findViewById(R.id.amount_payable);
            holder.btn_confirm=(Button)vi.findViewById(R.id.btn_confirm);
            //holder.image=(ImageView)vi.findViewById(R.id.image);

           /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        }
        else  
            holder=(ViewHolder)vi.getTag();

        if(data.size()<=0)
        {
            holder.amount_payable.setText("No Data");


        }
        else
        {
            /***** Get each Model object from Arraylist ********/
            tempValues=null;
            tempValues = (ListModel) data.get(position);

            /************  Set Model values in Holder elements ***********/
             holder.transfer_id.setText(tempValues.getTransferId());
             holder.amount_payable.setText(tempValues.getAmountPayable());
             //holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+tempValues.getImage(),null,null));
             holder.btn_confirm.setOnClickListener(new OnClickListener()
             {
                 public void onClick(View v)
                 {String serverURL = "http://ott.edgeaccounts.com/Webservices/UpdatePayment.php";
                     ConfirmPayment task = new ConfirmPayment(v.getContext(),(String) holder.transfer_id.getText()); // how you get the context to pass to the constructor may vary from where you're calling it, but most adapters to have one
                     task.execute(serverURL); 
                 }
             }
                     );
             /******** Set Item Click Listner for LayoutInflater for each row ***********/
            // vi.setOnClickListener(new OnItemClickListener(Integer.parseInt(tempValues.getTransferId())));
        }
        return vi;
    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener  implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int tId){
             mPosition = tId;
        }

        @Override
        public void onClick(View arg0) {
           // CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
            //sct.onItemClick(mPosition);
        }               
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }   
}

但无论哪个按钮我点击它只更新最后一行。 就像我有3行并按下第1行中的按钮一样,它会更新第3行。  为什么不为那一行工作呢? 按下确认按钮后,它也不会更新列表视图。 我在 AsyncTask onPostExecute 方法中使用了以下

Adapter.notifyDataSetChanged();

先谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这将解决问题

/*********** Declare Used Variables *********/
private PaymentsFragment frag;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
// remove this: ListModel tempValues=null
int i=0;

...
...

    holder.btn_confirm.setOnClickListener(new OnClickListener() {
        final ListModel tempValues = (ListModel) data.get(position);
        public void onClick(View v) {
            String serverURL = "http://ott.edgeaccounts.com/Webservices/UpdatePayment.php";
            ConfirmPayment task = new ConfirmPayment(v.getContext(), tempValues.getTransferId()); // how you get the context to pass to the constructor may vary from where you're calling it, but most adapters to have one
            task.execute(serverURL);
        }
    });

解决方案是soso,而且最好在ClickListener中引用底层数据/ id。