Get checked Listitems from ListView and pass that to another activity

徘徊边缘 提交于 2019-12-03 23:05:46

In you ListAdapter create a SparseBooleanArray

private SparseBooleanArray checkStatus;

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
    ........
}

This is the class ViewCache

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

This is the onCheckChangeListener

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

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

You can try implementing your own ArrayAdapter. Initialize it with an array of your file objects and use it in the list view.

Next make a list of indexes that is visible by the adapter and can be manipulated from the outside. In your onItemClick method you have the position of the clicked item. If it's in that list remove it, otherwise - insert it. Let's call that list selection.

Next in your adapter's getView method construct a view with a checkbox inside. Again you have the current position, because it's passed as an argument. Set the checkbox state depending on the presence of the position in selection.

Finally implement your button's onClick so that it does whatever you do with your file objects only for those objects of your file_array whose positions are in your selection.

Hope that helps

In the above answers Sreejith has given a good explanation of how to store the states of the checked items in the list view using a SparseBooleanArray. This solves the first part of your problem.
The second part regarding the passing of the states of these items to the other activities can be achieved using the Application class.
Application class:
Base class for those who need to maintain global application state. Sometimes you want to store data, like global variables which need to be accessed from multiple Activities - sometimes everywhere within the application. In this case, the Application object will help you.

Here is a sample code for this:

public class TopClass extends Application {
private static TopClass topClass;

public TopClass getInstance()
{
    return topClass;
}

@Override
public void onCreate ( )
{
    super.onCreate();
    topClass = this;
}

public ArrayList<String> arrList = new ArrayList<String>();   

}

You need to set tag android:name="TopClass" in the application manifest file under the application tag. Something like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name="TopClass" >
    ....
    ....

Here is how you can access it from the activity:

TopClass top = (TopClass)getApplicationContext();
top.arrList.add("StackOverflow");

Now you can access the same variable from other activities similarly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!