【问题标题】:Get the item correspond to a CheckedTextView from Android ListActivity Custom Adapter从 Android ListActivity 自定义适配器获取与 CheckedTextView 对应的项目
【发布时间】:2011-08-18 13:21:25
【问题描述】:

我有以下自定义适配器

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>

我能够为CheckedTextView 捕获setOnClickListener,在侦听器中,我需要获取与单击的CheckedTextView 对应的项目以调用适配器的setCheckedItem(item) 方法。 (请查看适配器类中的 cmets)

【问题讨论】:

  • 你说的项目...是什么意思?你这里只有一个checkedTextView..
  • @ntc 是List View
  • 你考虑过使用列表视图的单选/多选模式吗?

标签: android android-listview adapter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2015-11-15
    • 2012-06-18
    • 2019-02-06
    相关资源
    最近更新 更多