2012-01-29 36 views
3

我有一個自定義ListViewTextViewCheckBox。我還有一個自定義SimpleAdapter,其中我重寫了getView()方法,並能夠檢索TextViewCheckBox更改的點擊次數。我的問題是我不知道如何在OnCheckedChangedOnClick內獲得正確的點擊ListItemCheckBox獲取ListItem在自定義SimpleLayout中的位置

UPDATE增加整個CustomAdapter類:

public class CustomAdapter extends SimpleAdapter { 
    private LayoutInflater mInflater; 
    private List<HashMap<String, String>> mItems = null; 
    private Context mContext; 
    private int mPosicion; 

    public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
     super(context, items, resource, from, to); 
     mInflater = LayoutInflater.from(context); 
     mItems = items; 
     mContext = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     mPosicion = position; 

     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.custom_row_view, null); 

      holder = new ViewHolder(); 
      holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado); 
      holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion")); 

     convertView.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position 
      } 
     }); 

     holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       Log.i("Posicion",""+mPosicion);//Or checked 
      } 
     }); 

     return convertView; 

    } 


    private static class ViewHolder { 
     TextView txtTextoAgenda; 
     CheckBox chkbxEstado; 
    } 
} 

任何幫助表示讚賞。

+0

您有參數位置,您還需要什麼? – gwa 2012-01-30 01:55:18

+0

對不起,如果我沒有正確解釋。位置參數總是返回某個位置,但如果點擊不同的行或複選框則不更新。 – 2012-01-30 19:51:04

+0

這很奇怪,你可以發佈整個適配器嗎? – gwa 2012-01-30 20:47:58

回答

1

我找到了一個解決方案。如果有人知道更好的,請告訴我。工作CustomAdapter類:

public class CustomAdapter extends SimpleAdapter { 
     private LayoutInflater mInflater; 
     private List<HashMap<String, String>> mItems = null; 
     private Context mContext; 

     private OnClickListener mClick; 
     private OnCheckedChangeListener mChecked; 

     public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { 
      super(context, items, resource, from, to); 
      mInflater = LayoutInflater.from(context); 
      mItems = items; 
      mContext = context; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.custom_row_view, null); 

       holder = new ViewHolder(); 
       holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado); 
       holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista); 
       holder.posicion = position; //Add the new position into the holder for each row. 

       convertView.setTag(holder); 

       mClick = new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row. 
         Log.i("Posicion",""+v.posicion); 
        } 
       }; 

       mChecked = new OnCheckedChangeListener() { 
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox 
         Log.i("Posicion",""+viewHolder.posicion); 
        } 
       }; 

      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion")); 

      convertView.setOnClickListener(mClick); 

      holder.chkbxEstado.setOnCheckedChangeListener(mChecked); 

      return convertView; 

     } 

     public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag. 
      if(v.getTag() == null){ 
       return getViewHolder((View)v.getParent()); 
      } 
      return (ViewHolder)v.getTag(); 
     } 

     private static class ViewHolder { 
      TextView txtTextoAgenda; 
      CheckBox chkbxEstado; 
      int posicion; //Added position attribute to ViewHolder class. 
     } 
    } 

編輯重新使用onClickListener()和onCheckedChangedListener()實例。

+1

您應該考慮重新使用OnClickListener和OnCheckedChangeListener的實例。每當您滾動並且您的視圖被重新繪製時,您將分配新的視圖。 – bgs 2012-02-01 19:48:01

+0

感謝您的提示。更新。 – 2012-02-01 20:03:36

+0

不錯的解決方案!我希望有更優雅的東西,但這是有效的。 – 2012-03-27 03:05:53

0

OP的解決方案有效。但是,如果要擴展CursorAdapter,則position參數在newView()和bindView()中不存在。該怎麼辦?

他們提供一個光標。使用cursor.getPosition()。它做同樣的事情。