List of usage examples for android.widget ArrayAdapter notifyDataSetChanged
@Override public void notifyDataSetChanged()
From source file:org.tvbrowser.tvbrowser.TvBrowser.java
private void showChannelSelectionInternal(final String selection, final String title, final String help, final boolean delete) { String[] projection = {/*from ww w . j a v a 2 s. c om*/ TvBrowserContentProvider.CHANNEL_TABLE + "." + TvBrowserContentProvider.KEY_ID + " AS " + TvBrowserContentProvider.KEY_ID, TvBrowserContentProvider.GROUP_KEY_DATA_SERVICE_ID, TvBrowserContentProvider.CHANNEL_KEY_NAME, TvBrowserContentProvider.CHANNEL_KEY_SELECTION, TvBrowserContentProvider.CHANNEL_KEY_CATEGORY, TvBrowserContentProvider.CHANNEL_KEY_LOGO, TvBrowserContentProvider.CHANNEL_KEY_ALL_COUNTRIES }; ContentResolver cr = getContentResolver(); Cursor channels = cr.query(TvBrowserContentProvider.CONTENT_URI_CHANNELS_WITH_GROUP, projection, selection, null, TvBrowserContentProvider.CHANNEL_KEY_NAME); channels.moveToPosition(-1); // populate array list with all available channels final ArrayListWrapper channelSelectionList = new ArrayListWrapper(); ArrayList<Country> countryList = new ArrayList<Country>(); int channelIdColumn = channels.getColumnIndex(TvBrowserContentProvider.KEY_ID); int categoryColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_CATEGORY); int logoColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_LOGO); int dataServiceColumn = channels.getColumnIndex(TvBrowserContentProvider.GROUP_KEY_DATA_SERVICE_ID); int nameColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_NAME); int countyColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_ALL_COUNTRIES); int selectionColumn = channels.getColumnIndex(TvBrowserContentProvider.CHANNEL_KEY_SELECTION); ; while (channels.moveToNext()) { int channelID = channels.getInt(channelIdColumn); int category = channels.getInt(categoryColumn); byte[] logo = channels.getBlob(logoColumn); String dataService = channels.getString(dataServiceColumn); String name = channels.getString(nameColumn); String countries = channels.getString(countyColumn); boolean isSelected = channels.getInt(selectionColumn) == 1 && !delete; if (countries.contains("$")) { String[] values = countries.split("\\$"); for (String country : values) { Country test = new Country(new Locale(country, country)); if (!countryList.contains(test) && test.mLocale.getDisplayCountry().trim().length() > 0) { countryList.add(test); } } } else { Country test = new Country(new Locale(countries, countries)); if (!countryList.contains(test) && test.mLocale.getDisplayCountry().trim().length() > 0) { countryList.add(test); } } Bitmap channelLogo = UiUtils.createBitmapFromByteArray(logo); if (channelLogo != null) { BitmapDrawable l = new BitmapDrawable(getResources(), channelLogo); ColorDrawable background = new ColorDrawable(SettingConstants.LOGO_BACKGROUND_COLOR); background.setBounds(0, 0, channelLogo.getWidth() + 2, channelLogo.getHeight() + 2); LayerDrawable logoDrawable = new LayerDrawable(new Drawable[] { background, l }); logoDrawable.setBounds(background.getBounds()); l.setBounds(2, 2, channelLogo.getWidth(), channelLogo.getHeight()); channelLogo = UiUtils.drawableToBitmap(logoDrawable); } channelSelectionList.add(new ChannelSelection(channelID, name, category, countries, channelLogo, isSelected, SettingConstants.EPG_DONATE_KEY.equals(dataService))); } // sort countries for filtering Collections.sort(countryList, new Comparator<Country>() { @Override public int compare(Country lhs, Country rhs) { return lhs.toString().compareToIgnoreCase(rhs.toString()); } }); countryList.add(0, new Country(null)); channels.close(); // create filter for filtering of category and country final ChannelFilter filter = new ChannelFilter(SettingConstants.TV_CATEGORY, null); // create default logo for channels without logo final Bitmap defaultLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); final Set<String> firstDeletedChannels = PrefUtils.getStringSetValue(R.string.PREF_FIRST_DELETED_CHANNELS, new HashSet<String>()); final Set<String> keptDeletedChannels = PrefUtils.getStringSetValue(R.string.PREF_KEPT_DELETED_CHANNELS, new HashSet<String>()); final int firstDeletedColor = getResources().getColor(R.color.pref_first_deleted_channels); final int keptDeletedColor = getResources().getColor(R.color.pref_kept_deleted_channels); // Custom array adapter for channel selection final ArrayAdapter<ChannelSelection> channelSelectionAdapter = new ArrayAdapter<ChannelSelection>( TvBrowser.this, R.layout.channel_row, channelSelectionList) { public View getView(int position, View convertView, ViewGroup parent) { ChannelSelection value = getItem(position); ViewHolder holder = null; if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) getContext() .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.channel_row, getParentViewGroup(), false); holder.mTextView = (TextView) convertView.findViewById(R.id.row_of_channel_text); holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.row_of_channel_selection); holder.mLogo = (ImageView) convertView.findViewById(R.id.row_of_channel_icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } SpannableStringBuilder nameBuilder = new SpannableStringBuilder(value.toString()); String channelID = String.valueOf(value.getChannelID()); if (keptDeletedChannels.contains(channelID)) { nameBuilder.setSpan(new ForegroundColorSpan(keptDeletedColor), 0, value.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else if (firstDeletedChannels.contains(channelID)) { nameBuilder.setSpan(new ForegroundColorSpan(firstDeletedColor), 0, value.toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (value.isEpgDonateChannel()) { nameBuilder.append("\n(EPGdonate)"); nameBuilder.setSpan(new RelativeSizeSpan(0.65f), value.toString().length(), nameBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } holder.mTextView.setText(nameBuilder); holder.mCheckBox.setChecked(value.isSelected()); Bitmap logo = value.getLogo(); if (logo != null) { holder.mLogo.setImageBitmap(logo); } else { holder.mLogo.setImageBitmap(defaultLogo); } return convertView; } }; // inflate channel selection view View channelSelectionView = getLayoutInflater().inflate(R.layout.dialog_channel_selection_list, getParentViewGroup(), false); channelSelectionView.findViewById(R.id.channel_selection_selection_buttons).setVisibility(View.GONE); channelSelectionView.findViewById(R.id.channel_selection_input_id_name).setVisibility(View.GONE); TextView infoView = (TextView) channelSelectionView.findViewById(R.id.channel_selection_label_id_name); if (help != null) { infoView.setText(help); infoView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.epg_donate_info_font_size)); } else { infoView.setVisibility(View.GONE); } // get spinner for country filtering and create array adapter with all available countries Spinner country = (Spinner) channelSelectionView.findViewById(R.id.channel_country_value); final ArrayAdapter<Country> countryListAdapter = new ArrayAdapter<Country>(this, android.R.layout.simple_spinner_item, countryList); countryListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); country.setAdapter(countryListAdapter); // add item selection listener to react of user setting filter for country country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Country country = countryListAdapter.getItem(position); filter.mCountry = country.getCountry(); channelSelectionList.setFilter(filter); channelSelectionAdapter.notifyDataSetChanged(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // get spinner for category selection and add listener to react to user category selection Spinner category = (Spinner) channelSelectionView.findViewById(R.id.channel_category_value); category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 1: filter.mCategory = SettingConstants.TV_CATEGORY; break; case 2: filter.mCategory = SettingConstants.RADIO_CATEGORY; break; case 3: filter.mCategory = SettingConstants.CINEMA_CATEGORY; break; default: filter.mCategory = SettingConstants.NO_CATEGORY; break; } channelSelectionList.setFilter(filter); channelSelectionAdapter.notifyDataSetChanged(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); if (delete) { channelSelectionView.findViewById(R.id.channel_country_label).setVisibility(View.GONE); channelSelectionView.findViewById(R.id.channel_category_label).setVisibility(View.GONE); country.setVisibility(View.GONE); category.setVisibility(View.GONE); } // get the list view of the layout and add adapter with available channels ListView list = (ListView) channelSelectionView.findViewById(R.id.channel_selection_list); list.setAdapter(channelSelectionAdapter); // add listener to react to user selection of channels list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckBox check = (CheckBox) view.findViewById(R.id.row_of_channel_selection); if (check != null) { check.setChecked(!check.isChecked()); channelSelectionAdapter.getItem(position).setSelected(check.isChecked()); } } }); // show dialog only if channels are available if (!channelSelectionList.isEmpty()) { AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this); if (title == null) { builder.setTitle(R.string.select_channels); } else { builder.setTitle(title); } builder.setView(channelSelectionView); builder.setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { boolean somethingSelected = false; boolean somethingChanged = false; Iterator<ChannelSelection> it = channelSelectionList.superIterator(); StringBuilder deleteWhere = new StringBuilder(); HashSet<String> keep = new HashSet<String>(); while (it.hasNext()) { ChannelSelection sel = it.next(); if (sel.isSelected() && !sel.wasSelected()) { somethingChanged = somethingSelected = true; if (delete) { if (deleteWhere.length() > 0) { deleteWhere.append(", "); } deleteWhere.append(sel.getChannelID()); } else { ContentValues values = new ContentValues(); values.put(TvBrowserContentProvider.CHANNEL_KEY_SELECTION, 1); getContentResolver().update( ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS, sel.getChannelID()), values, null, null); } } else if (!sel.isSelected() && sel.wasSelected()) { somethingChanged = true; ContentValues values = new ContentValues(); values.put(TvBrowserContentProvider.CHANNEL_KEY_SELECTION, 0); getContentResolver().update( ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_CHANNELS, sel.getChannelID()), values, null, null); getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_DATA_VERSION, TvBrowserContentProvider.CHANNEL_KEY_CHANNEL_ID + "=" + sel.getChannelID(), null); getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_DATA, TvBrowserContentProvider.CHANNEL_KEY_CHANNEL_ID + "=" + sel.getChannelID(), null); } else if (delete && !sel.isSelected()) { keep.add(String.valueOf(sel.getChannelID())); } } if (delete) { if (deleteWhere.length() > 0) { deleteWhere.insert(0, TvBrowserContentProvider.KEY_ID + " IN ( "); deleteWhere.append(" ) "); Log.d("info2", "DELETE WHERE FOR REMOVED CHANNELS " + deleteWhere.toString()); int count = getContentResolver().delete(TvBrowserContentProvider.CONTENT_URI_CHANNELS, deleteWhere.toString(), null); Log.d("info2", "REMOVED CHANNELS COUNT " + count); } Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit(); edit.putStringSet(getString(R.string.PREF_KEPT_DELETED_CHANNELS), keep); edit.commit(); } // if something was changed we need to update channel list bar in program list and the complete program table if (somethingChanged) { SettingConstants.initializeLogoMap(TvBrowser.this, true); updateProgramListChannelBar(); } // if something was selected we need to download new data if (somethingSelected && !delete) { checkTermsAccepted(); } } }); builder.setNegativeButton(android.R.string.cancel, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (delete) { HashSet<String> keep = new HashSet<String>(); Iterator<ChannelSelection> it = channelSelectionList.superIterator(); while (it.hasNext()) { ChannelSelection sel = it.next(); keep.add(String.valueOf(sel.getChannelID())); } Editor edit = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit(); edit.putStringSet(getString(R.string.PREF_KEPT_DELETED_CHANNELS), keep); edit.commit(); } } }); builder.show(); } selectingChannels = false; }