Copyright (c) 2014, Ford Motor Company
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are m...
If you think the Android project sdl_tester_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.livio.sdl.dialogs;
//www.java2s.comimport java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.livio.sdl.R;
import com.livio.sdl.utils.AndroidUtils;
/**
* A generic, abstract class representing a ListView dialog that allows the user to select
* multiple listview items. Selected items are stored in a protected List<E> object
* called selectedItems, which can be used by subclasses of this class.
*
* @author Mike Burke
*
* @param <E>
*/publicabstractclass BaseMultipleListViewDialog<E> extends BaseOkCancelDialog {
protected ListView listView;
protected List<E> selectedItems = new ArrayList<E>();
public BaseMultipleListViewDialog(Context context, String title, List<E> items) {
super(context, title, R.layout.listview);
listView.setAdapter(AndroidUtils.createMultipleListViewAdapter(context, items));
}
@Override
protectedvoid findViews(View parent) {
listView = (ListView) parent.findViewById(R.id.listView);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
publicvoid onItemClick(AdapterView<?> parent, View view, int position, long id) {
toggleItem(((ArrayAdapter<E>) parent.getAdapter()).getItem(position));
}
});
}
/**
* Toggles the input item in the list of selected items. So, if the current item
* was not selected, it is added to the list; if the current item was selected,
* it is removed from the list.
*
* @param item
*/protectedvoid toggleItem(E item){
finalboolean alreadyInList = selectedItems.contains(item);
if(alreadyInList){
selectedItems.remove(item);
}
else{
selectedItems.add(item);
}
}
}