从 Android ListActivity 自定义适配器获取与 CheckedTextView 对应的项目

发布于 2024-11-30 09:12:03 字数 2611 浏览 3 评论 0原文

我有以下自定义适配器

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>

我能够捕获 CheckedTextViewsetOnClickListener,在侦听器内,我需要获取与单击的 相对应的项目>CheckedTextView 调用适配器的 setCheckedItem(item) 方法。 (请参阅适配器类中的注释

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2024-12-07 09:12:03

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文