2011-08-18 40 views
0

一个CheckedTextView我已经下面的自定义适配器获得该项目对应于从Android的ListActivity自定义适配器

public class GroupAdapter extends ArrayAdapter<Group> { 
    private ArrayList<Group> items; 
    public HashMap<String, String> checkedItems = new HashMap<String, String>(); 
    ... 
    public void setCheckedItem(int item) { 
    } 
    public HashMap<String, String> getCheckedItems() { 
     return checkedItems; 
    } 
    ... 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    View v = convertView; 
    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v = vi.inflate(R.layout.list_groups, null); 
    Group bs = items.get(position); 
    CheckedTextView ctView = (CheckedTextView) v.findViewById(R.id.listCheckboxview); 
    ctView.setText(bs.getGroupName()); 
    // ctView.setId(bs.getId()); 
     ctView.setId(position); 
     ctView.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       ((CheckedTextView) v).toggle(); 
       if(((CheckedTextView) v).isChecked()) { 
        Log.e(MY_DEBUG_TAG, "Checked"); 
        // I need to get the item currespond to this view to call Adapters's setCheckedItem(item) method 
       } 
      } 
     }); 

和布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" 
    android:paddingLeft="5dip" android:paddingRight="5dip" 
    android:paddingTop="10dip" android:paddingBottom="10dip" 
    android:fastScrollEnabled="true" 
    > 
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/listCheckboxview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" android:gravity="center|left" 
     android:textColor="#0075AB" android:textStyle="bold" android:textSize="14dip" 
     android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
     android:paddingLeft="6dip" 
     android:paddingRight="6dip" 
     android:clickable="true" 
     android:focusable="true" 
     android:text="" 
     /> 
</LinearLayout> 

林“能捕捉到setOnClickListenerCheckedTextView,在听众的内部,我需要获取与点击CheckedTextView相对应的项目,以调用适配器的setCheckedItem(item)方法。 (请参阅适配器类中的注释)

+0

你是说什么项目... ...是什么意思?你在这里只有一个checkedTextView .. – ngesh

+0

@ntc它是[List View](http://developer.android.com/resources/tutorials/views/hello-listview.html) –

+0

你有没有考虑过使用单一/多重选择列表视图的模式? – Ronnie

回答

1

您应该设置checkedTextView的标签。
ctView.setTag(items.get(position));然后在onClick()v.getTag()会给你的项目组。

相关问题