Android:将适配器类的getView()方法的按钮单击响应传递给mainActivity

时间:2012-12-29 21:22:29

标签: android android-arrayadapter android-button onclicklistener

问题很简单。我写了一个单独的类来扩展ArrayAdapter。该适配器处理几个TEXTVIEW和一个按钮。问题是我不确定我是否已将用getView()方法编写的Button的onClickListener的响应带到main_activity。 main_activity基于按钮响应有很多逻辑要遵循。你能否告诉我如何获得对main_activity的按钮响应?

以下是getView方法

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.menuItem = (TextView) convertView.findViewById(R.id.tvGroup);
            convertView.setBackgroundColor(Color.RED); 
            break;              
        case 1:
            convertView = vi.inflate(R.layout.confirmitem,  null); 
            holder.menuItem = (TextView) convertView.findViewById(R.id.tvConfirmItem); 
            holder.quantity = (TextView) convertView.findViewById(R.id.tvQuantity); 
            holder.cancel   = (Button)   convertView.findViewById(R.id.bCancel); 

            //   cancel button
               holder.cancel.setOnClickListener(new View.OnClickListener() {        
                    public void onClick(View v) {                   
                        Log.i("ConfirmAdapter ","Button postion  "+ position + "canceled item : " + menuItemList.get(position).getTicketItemObject().getName() ); 
                        }       
                });   
            break; 
         }  
        convertView.setTag(holder); 
    } else {
        holder = (ViewHolder) convertView.getTag();  
    }    

    switch (type) {
    case 0:         
        holder.menuItem.setText(menuItemList.get(position).getTicketItemObject().getCategoryName()) ; 
        convertView.setBackgroundColor(Color.RED);  
        break;              
    case 1:
        holder.menuItem.setText(menuItemList.get(position).getTicketItemObject().getName());
        holder.quantity.setText(Integer.toString(menuItemList.get(position).getTicketItemObject().getItemCount())); 
        break; 
     }

    return convertView; 
    }

1 个答案:

答案 0 :(得分:0)

如何在arrayadapter的构造函数中引用该活动? 像

这样的东西
public YourArrayAdapter(YourActivity a){
     mActivity = a;
}

从onClickMethod中提到mActivity?

在YourActivity中,您可以从那里调用onItemButtonClicked()方法。