List of usage examples for android.widget ArrayAdapter getItem
@Override public @Nullable T getItem(int position)
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 = {/* w w w.j a v a2s .co m*/ 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; }
From source file:org.tvbrowser.tvbrowser.TvBrowser.java
private void editDontWantToSee() { if (!SettingConstants.UPDATING_FILTER) { Set<String> currentExclusions = PrefUtils.getStringSetValue(R.string.I_DONT_WANT_TO_SEE_ENTRIES, null); final ArrayList<ExclusionEdit> mCurrentExclusionList = new ArrayList<TvBrowser.ExclusionEdit>(); if (currentExclusions != null && !currentExclusions.isEmpty()) { for (String exclusion : currentExclusions) { mCurrentExclusionList.add(new ExclusionEdit(exclusion)); }// ww w . ja va 2 s . c om } Collections.sort(mCurrentExclusionList); final ArrayAdapter<ExclusionEdit> exclusionAdapter = new ArrayAdapter<TvBrowser.ExclusionEdit>( TvBrowser.this, android.R.layout.simple_list_item_1, mCurrentExclusionList); View view = getLayoutInflater().inflate(R.layout.dont_want_to_see_exclusion_edit_list, getParentViewGroup(), false); ListView list = (ListView) view.findViewById(R.id.dont_want_to_see_exclusion_list); list.setAdapter(exclusionAdapter); final Runnable cancel = new Runnable() { @Override public void run() { } }; AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final ExclusionEdit edit = exclusionAdapter.getItem(position); View editView = getLayoutInflater().inflate(R.layout.dont_want_to_see_edit, getParentViewGroup(), false); final TextView exclusion = (TextView) editView.findViewById(R.id.dont_want_to_see_value); final CheckBox caseSensitive = (CheckBox) editView .findViewById(R.id.dont_want_to_see_case_sensitve); exclusion.setText(edit.mExclusion); caseSensitive.setSelected(edit.mIsCaseSensitive); Runnable editPositive = new Runnable() { @Override public void run() { if (exclusion.getText().toString().trim().length() > 0) { edit.mExclusion = exclusion.getText().toString(); edit.mIsCaseSensitive = caseSensitive.isSelected(); exclusionAdapter.notifyDataSetChanged(); } } }; showAlertDialog(getString(R.string.action_dont_want_to_see), null, editView, null, editPositive, null, cancel, false, false); } }; list.setOnItemClickListener(onClickListener); list.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.don_want_to_see_context, menu); MenuItem item = menu.findItem(R.id.dont_want_to_see_delete); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { ExclusionEdit edit = exclusionAdapter .getItem(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position); exclusionAdapter.remove(edit); exclusionAdapter.notifyDataSetChanged(); return true; } }); } }); Thread positive = new Thread() { @Override public void run() { SettingConstants.UPDATING_FILTER = true; final NotificationCompat.Builder builder = new NotificationCompat.Builder(TvBrowser.this); builder.setSmallIcon(R.drawable.ic_stat_notify); builder.setOngoing(true); builder.setContentTitle(getResources().getText(R.string.action_dont_want_to_see)); builder.setContentText( getResources().getText(R.string.dont_want_to_see_refresh_notification_text)); final int notifyID = 2; final NotificationManager notification = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notification.notify(notifyID, builder.build()); updateProgressIcon(true); HashSet<String> newExclusions = new HashSet<String>(); final ArrayList<DontWantToSeeExclusion> exclusionList = new ArrayList<DontWantToSeeExclusion>(); for (ExclusionEdit edit : mCurrentExclusionList) { String exclusion = edit.getExclusion(); newExclusions.add(exclusion); exclusionList.add(new DontWantToSeeExclusion(exclusion)); } new Thread() { public void run() { Cursor programs = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_DATA, new String[] { TvBrowserContentProvider.KEY_ID, TvBrowserContentProvider.DATA_KEY_TITLE }, null, null, TvBrowserContentProvider.KEY_ID); programs.moveToPosition(-1); builder.setProgress(programs.getCount(), 0, true); notification.notify(notifyID, builder.build()); ArrayList<ContentProviderOperation> updateValuesList = new ArrayList<ContentProviderOperation>(); int keyColumn = programs.getColumnIndex(TvBrowserContentProvider.KEY_ID); int titleColumn = programs.getColumnIndex(TvBrowserContentProvider.DATA_KEY_TITLE); DontWantToSeeExclusion[] exclusionArr = exclusionList .toArray(new DontWantToSeeExclusion[exclusionList.size()]); while (programs.moveToNext()) { builder.setProgress(programs.getCount(), programs.getPosition(), false); notification.notify(notifyID, builder.build()); String title = programs.getString(titleColumn); boolean filter = UiUtils.filter(getApplicationContext(), title, exclusionArr); long progID = programs.getLong(keyColumn); ContentValues values = new ContentValues(); values.put(TvBrowserContentProvider.DATA_KEY_DONT_WANT_TO_SEE, filter ? 1 : 0); ContentProviderOperation.Builder opBuilder = ContentProviderOperation.newUpdate( ContentUris.withAppendedId(TvBrowserContentProvider.CONTENT_URI_DATA_UPDATE, progID)); opBuilder.withValues(values); updateValuesList.add(opBuilder.build()); } notification.cancel(notifyID); programs.close(); if (!updateValuesList.isEmpty()) { try { getContentResolver().applyBatch(TvBrowserContentProvider.AUTHORITY, updateValuesList); UiUtils.sendDontWantToSeeChangedBroadcast(getApplicationContext(), true); handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), R.string.dont_want_to_see_sync_success, Toast.LENGTH_LONG) .show(); } }); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } } updateProgressIcon(false); SettingConstants.UPDATING_FILTER = false; } }.start(); Editor edit = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit(); edit.putStringSet(getString(R.string.I_DONT_WANT_TO_SEE_ENTRIES), newExclusions); edit.commit(); } }; showAlertDialog(getString(R.string.action_dont_want_to_see_edit), null, view, null, positive, null, cancel, false, true); } }