【问题标题】:Android Get child items in listViewAndroid获取listView中的子项
【发布时间】:2016-12-13 14:08:20
【问题描述】:

我有一个用于 listView 的 ArrayAdapter,里面有复选框。 当复选框值更改时,我想获取带有选中复选框的项目列表。

onCreate 我的活动函数中

ArrayAdapter<DrawerDetail> adapter = new ArrayAdapter<DrawerDetail>(this, R.layout.settings_minidrawer_item, apps) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = getLayoutInflater().inflate(R.layout.settings_minidrawer_item, null);
        }
        ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
        appIcon.setImageDrawable(apps.get(position).icon);
        appIcon.setTag(apps.get(position).name);
        TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_name);
        appLabel.setText(apps.get(position).label);
        appLabel.setText(Integer.toString(position));
        CheckBox appCheck = (CheckBox)convertView.findViewById(R.id.item_app_check);
        appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                UpdatePreferences();
            }
        });

        return convertView;
    }
};

list.setAdapter(adapter);

updatePreferences

public void UpdatePreferences() {
    SharedPreferences.Editor prefEditor = prefs.edit();

    SparseBooleanArray checked = list.getCheckedItemPositions();

    List<String> drawerListArray = new ArrayList<String>();
    View v;
    CheckBox c;
    for(int i = 0; i < list.getAdapter().getCount(); i++) {
        v = list.getAdapter().getView(i, null, list);
        c = (CheckBox) v.findViewById(R.id.item_app_check);
        TextView aka = (TextView) v.findViewById(R.id.item_app_name);
        aka.setText("dd");
        Log.e("checked: ", Boolean.toString(c.isChecked()));
    }
}

如果您检查SparsBooleanArray checked 值,它始终为空。 list.getAdapter().getView() 似乎也不起作用。

此行找不到任何视图(不返回错误,但无法获取/设置值):

c = (CheckBox) v.findViewById(R.id.item_app_check);
TextView aka = (TextView) v.findViewById(R.id.item_app_name);

我尝试了不同的方法,但无法成功。现在我的猜测是我正在覆盖适配器的 getView 方法并且它们相互冲突,但我无法想出任何解决方案如何覆盖 getView 以使其适用于两者。

【问题讨论】:

    标签: android listview android-arrayadapter


    【解决方案1】:

    不要检索这些视图。在您的 onCheckedChangeListener 中传递已检查/未检查的信息 - 例如如果选中/未选中,则为字符串和信息。然后你可以在数组中收集选中的项目。

    尝试做这样的事情:

    public class TestClass {
    
        private List<String> checkedStrings = new ArrayList<>();
    
        ...
    
        void updateArray(boolean checked, String checkedText){
            if(checked){
                if(!checkedStrings.contains(checkedText)){
                    checkedStrings.add(checkedText);
                }
            } else {
                checkedStrings.remove(checkedText);
            }
        }
    }
    

    在你的 ArrayAdapter 中:

    appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    updateArray(isChecked, apps.get(position).label);
                }
            });
    

    【讨论】:

    • 是的,但在我的情况下,我需要获取所有检查的值,而不仅仅是我刚刚检查的那个。我的方法似乎不正确。我是安卓新手
    • 是的,我明白了 - 您可以在类中创建一个数组,并在调用 onCheckedChange 时在 UpdatePreferences 方法中将选中/未选中的项目放入/删除该数组。您是否还想更新相应的列表视图行,例如改标题?
    • 不只是选中的复选框列表,但我应该能够从此复选框中获取标签。在类中创建数组是什么意思?
    【解决方案2】:

    请尝试替换您的代码:

    c = (CheckBox) v.findViewById(R.id.item_app_check);
    

    使用此代码:

    View nextChild = null;
    
    // Find CheckBox inside the parent view
    for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) {
        nextChild = ((ViewGroup) v).getChildAt(i);
        if (nextChild instanceof CheckBox) {
            // Do your work;
    
            break;
        }
    }
    

    【讨论】:

    • 为什么直接找不到?
    • 因为您试图从另一个类(在您的情况下为 TestClass)访问属于适配器类的视图。因此,如果您尝试从适配器类中访问它,您只需像以前一样调用它(通过 id)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多