Domanda

I'm developing a Androidapplication using ListView.

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top