简体   繁体   中英

Get the item correspond to a CheckedTextView from Android ListActivity Custom Adapter

I 've the following custom adapter

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
                }
            }
        });

and the layout

<?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>

Im' able to capture the setOnClickListener for the CheckedTextView , Inside the listener, I need to get the item correspond to clicked CheckedTextView to call Adapters's setCheckedItem(item) method. ( Please see the comments in the adapter class )

You should set the tag of the checkedTextView.
Like ctView.setTag(items.get(position)); then in the onClick() doing v.getTag() will give you the item group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM