得到检查时listItems从ListView和传递到另一个活动 [英] Get checked Listitems from ListView and pass that to another activity

查看:97
本文介绍了得到检查时listItems从ListView和传递到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用安卓应用程序的ListView

I'm developing a Androidapplication using ListView.

的ListView 在每一个文件列表项。在这里,我在的ListView 设置 onItemClick 。因此,如果用户点击了列表项电子邮件应用程序被打开并附加特定的文件中的电子邮件。它为单文件,这得到落实,工作正常。

ListView have a one file in each and every ListItem. Here, I have set onItemClickin ListView. So, that if user clicks the ListItememail application gets open and attach the particular file in email. Its for the single File, this gets implemented and working fine.

现在我要附加多个文件的电子邮件。即实施复选框中的每个列表项和检查的项目必须附加到邮件。

Now I want attach the multiple file in email. i.e. the implementing the CheckBoxin each ListItemand checked items have to attached into the Mail.

我知道它,因为它可能非常相似,检查多个文件,并通过单击单个按钮删除所有文件中的文件管理器应用程序。但不知道该怎么办。

I know its possible because its very similar to the file manager application that checking the multiple file and deleting the all file by clicking the single Button. But don't know how to do.

推荐答案

在你ListAdapter创建SparseBooleanArray

In you ListAdapter create a SparseBooleanArray

private SparseBooleanArray checkStatus;

这SparseBooleanArray存储检查的项目。现在,在getView做以下

This SparseBooleanArray stores the checked items. Now in getView do the following

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewCache viewCache;
    if (view == null){
        viewCache = new ViewCache();
        view = layoutInflater.inflate(R.layout.list_box, null, false);
        viewCache.checkBox = view.findViewById(R.id.check_box);            
        viewCache.checkBox.setOnCheckedChangeListener(onCheckedChangeListener);
        //other views in the list box
        ...........
    }
    vewCache = (ViewCache)view.getTag();
    viewCache.checkBox.setTag(position);
    viewCache.checkBox.setChecked(isChecked(position));
    //set other views
    ........
}

这是类视图缓冲

private static class ViewCache{        
    CheckBox checkBox;
    //other views in the list box
    .......
}

此方法检查位置是否检查

This method checks whether the position is checked

private boolean isChecked(int position){
    return checkStatus.get(position, false);
}

这是onCheckChangeListener

This is the onCheckChangeListener

CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        checkStatus.put(((Integer)compoundButton.getTag()), b);
    }
};

最后,你可以从SparseBooleanArray的checkStatus的检查项目。认为这将帮助你。

Finally you can get the checked items from the SparseBooleanArray checkStatus. Think it will help you.

这篇关于得到检查时listItems从ListView和传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆