Example usage for android.widget FilterQueryProvider FilterQueryProvider

List of usage examples for android.widget FilterQueryProvider FilterQueryProvider

Introduction

In this page you can find the example usage for android.widget FilterQueryProvider FilterQueryProvider.

Prototype

FilterQueryProvider

Source Link

Usage

From source file:de.baumann.browser.popups.Popup_files.java

private void setFilesList() {

    deleteDatabase("files_DB_v01.db");

    File f = new File(sharedPref.getString("files_startFolder",
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()));
    final File[] files = f.listFiles();

    Arrays.sort(files, new Comparator<File>() {
        @Override/*from  www . j a  v a 2s.c  o  m*/
        public int compare(File file1, File file2) {
            if (file1.isDirectory()) {
                if (file2.isDirectory()) {
                    return String.valueOf(file1.getName().toLowerCase())
                            .compareTo(file2.getName().toLowerCase());
                } else {
                    return -1;
                }
            } else {
                if (file2.isDirectory()) {
                    return 1;
                } else {
                    return String.valueOf(file1.getName().toLowerCase())
                            .compareTo(file2.getName().toLowerCase());
                }
            }
        }
    });

    // looping through all items <item>
    if (files.length == 0) {
        Snackbar.make(listView, R.string.toast_files, Snackbar.LENGTH_LONG).show();
    }

    for (File file : files) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

        String file_Name = file.getName().substring(0, 1).toUpperCase() + file.getName().substring(1);
        String file_Size = getReadableFileSize(file.length());
        String file_date = formatter.format(new Date(file.lastModified()));
        String file_path = file.getAbsolutePath();

        String file_ext;
        if (file.isDirectory()) {
            file_ext = ".";
        } else {
            file_ext = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
        }

        db.open();
        if (db.isExist(file_Name)) {
            Log.i(TAG, "Entry exists" + file_Name);
        } else {
            db.insert(file_Name, file_Size, file_ext, file_path, file_date);
        }
    }

    try {
        db.insert("...", "", "", "", "");
    } catch (Exception e) {
        Snackbar.make(listView, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
    }

    //display data
    final int layoutstyle = R.layout.list_item;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "files_title", "files_content", "files_creation" };
    final Cursor row = db.fetchAllData(this);
    adapter = new SimpleCursorAdapter(this, layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) listView.getItemAtPosition(position);
            final String files_icon = row2.getString(row2.getColumnIndexOrThrow("files_icon"));
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));
            final File pathFile = new File(files_attachment);

            View v = super.getView(position, convertView, parent);
            final ImageView iv = (ImageView) v.findViewById(R.id.icon_notes);

            iv.setVisibility(View.VISIBLE);

            if (pathFile.isDirectory()) {
                iv.setImageResource(R.drawable.folder);
            } else {
                switch (files_icon) {
                case "":
                    iv.setImageResource(R.drawable.arrow_up_dark);
                    break;
                case ".m3u8":
                case ".mp3":
                case ".wma":
                case ".midi":
                case ".wav":
                case ".aac":
                case ".aif":
                case ".amp3":
                case ".weba":
                case ".ogg":
                    iv.setImageResource(R.drawable.file_music);
                    break;
                case ".mpeg":
                case ".mp4":
                case ".webm":
                case ".qt":
                case ".3gp":
                case ".3g2":
                case ".avi":
                case ".f4v":
                case ".flv":
                case ".h261":
                case ".h263":
                case ".h264":
                case ".asf":
                case ".wmv":
                    try {
                        Glide.with(Popup_files.this).load(files_attachment) // or URI/path
                                .override(76, 76).centerCrop().into(iv); //imageView to set thumbnail to
                    } catch (Exception e) {
                        Log.w("HHS_Moodle", "Error load thumbnail", e);
                        iv.setImageResource(R.drawable.file_video);
                    }
                    break;
                case ".vcs":
                case ".vcf":
                case ".css":
                case ".ics":
                case ".conf":
                case ".config":
                case ".java":
                case ".html":
                    iv.setImageResource(R.drawable.file_xml);
                    break;
                case ".apk":
                    iv.setImageResource(R.drawable.android);
                    break;
                case ".pdf":
                    iv.setImageResource(R.drawable.file_pdf);
                    break;
                case ".rtf":
                case ".csv":
                case ".txt":
                case ".doc":
                case ".xls":
                case ".ppt":
                case ".docx":
                case ".pptx":
                case ".xlsx":
                case ".odt":
                case ".ods":
                case ".odp":
                    iv.setImageResource(R.drawable.file_document);
                    break;
                case ".zip":
                case ".rar":
                    iv.setImageResource(R.drawable.zip_box);
                    break;
                case ".gif":
                case ".bmp":
                case ".tiff":
                case ".svg":
                case ".png":
                case ".jpg":
                case ".JPG":
                case ".jpeg":
                    try {
                        Glide.with(Popup_files.this).load(files_attachment) // or URI/path
                                .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
                                .override(76, 76).centerCrop().into(iv); //imageView to set thumbnail to
                    } catch (Exception e) {
                        Log.w("HHS_Moodle", "Error load thumbnail", e);
                        iv.setImageResource(R.drawable.file_image);
                    }
                    break;
                default:
                    iv.setImageResource(R.drawable.file);
                    break;
                }
            }

            if (files_attachment.isEmpty()) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        iv.setImageResource(R.drawable.arrow_up_dark);
                    }
                }, 350);
            }

            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_filesBY", "files_title");
    sharedPref.edit().putString("filter_filesBY", "files_title").apply();
    editText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    listView.setAdapter(adapter);
    //onClick function
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            Cursor row2 = (Cursor) listView.getItemAtPosition(position);
            final String files_icon = row2.getString(row2.getColumnIndexOrThrow("files_icon"));
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));

            final File pathFile = new File(files_attachment);

            if (pathFile.isDirectory()) {
                try {
                    sharedPref.edit().putString("files_startFolder", files_attachment).apply();
                    setFilesList();
                } catch (Exception e) {
                    Snackbar.make(listView, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
                }
            } else if (files_attachment.equals("")) {
                try {
                    final File pathActual = new File(sharedPref.getString("files_startFolder",
                            Environment.getExternalStorageDirectory().getPath()));
                    sharedPref.edit().putString("files_startFolder", pathActual.getParent()).apply();
                    setFilesList();
                } catch (Exception e) {
                    Snackbar.make(listView, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
                }
            } else {
                helper_main.open(files_icon, Popup_files.this, pathFile, listView);
            }
        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor row2 = (Cursor) listView.getItemAtPosition(position);
            final String files_title = row2.getString(row2.getColumnIndexOrThrow("files_title"));
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));

            final File pathFile = new File(files_attachment);

            if (pathFile.isDirectory()) {
                Snackbar snackbar = Snackbar
                        .make(listView, R.string.bookmark_remove_confirmation, Snackbar.LENGTH_LONG)
                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                sharedPref.edit().putString("files_startFolder", pathFile.getParent()).apply();
                                deleteRecursive(pathFile);
                                setFilesList();
                            }
                        });
                snackbar.show();

            } else {
                final CharSequence[] options = { getString(R.string.choose_menu_2),
                        getString(R.string.choose_menu_3), getString(R.string.choose_menu_4) };

                final AlertDialog.Builder dialog = new AlertDialog.Builder(Popup_files.this);
                dialog.setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
                dialog.setItems(options, new DialogInterface.OnClickListener() {
                    @SuppressWarnings("ResultOfMethodCallIgnored")
                    @Override
                    public void onClick(DialogInterface dialog, int item) {

                        if (options[item].equals(getString(R.string.choose_menu_2))) {

                            if (pathFile.exists()) {
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("image/png");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, files_title);
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, files_title);
                                Uri bmpUri = Uri.fromFile(pathFile);
                                sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.app_share_file))));
                            }
                        }
                        if (options[item].equals(getString(R.string.choose_menu_4))) {

                            Snackbar snackbar = Snackbar
                                    .make(listView, R.string.bookmark_remove_confirmation, Snackbar.LENGTH_LONG)
                                    .setAction(R.string.toast_yes, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            pathFile.delete();
                                            setFilesList();
                                        }
                                    });
                            snackbar.show();
                        }
                        if (options[item].equals(getString(R.string.choose_menu_3))) {
                            sharedPref.edit().putString("pathFile", files_attachment).apply();
                            editText.setVisibility(View.VISIBLE);
                            helper_editText.showKeyboard(Popup_files.this, editText, 2, files_title,
                                    getString(R.string.bookmark_edit_title));
                        }
                    }
                });
                dialog.show();
            }

            return true;
        }
    });
}

From source file:de.baumann.hhsmoodle.data_files.Files_Fragment.java

public void setFilesList() {

    getActivity().deleteDatabase("files_DB_v01.db");

    File f = new File(sharedPref.getString("files_startFolder",
            Environment.getExternalStorageDirectory().getPath() + "/HHS_Moodle/"));
    final File[] files = f.listFiles();

    // looping through all items <item>
    if (files.length == 0) {
        Snackbar.make(lv, R.string.toast_files, Snackbar.LENGTH_LONG).show();
    }/*from  ww  w  .j  a  va 2s  .c o m*/

    for (File file : files) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

        String file_Name = file.getName();
        String file_Size = getReadableFileSize(file.length());
        String file_date = formatter.format(new Date(file.lastModified()));
        String file_path = file.getAbsolutePath();

        String file_ext;
        if (file.isDirectory()) {
            file_ext = ".";
        } else {
            file_ext = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
        }

        db.open();
        if (db.isExist(file_Name)) {
            Log.i(TAG, "Entry exists" + file_Name);
        } else {
            db.insert(file_Name, file_Size, file_ext, file_path, file_date);
        }
    }

    try {
        db.insert("...", "", "", "", "");
    } catch (Exception e) {
        Snackbar.make(lv, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
    }

    //display data
    final int layoutstyle = R.layout.list_item_notes;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "files_title", "files_content", "files_creation" };
    final Cursor row = db.fetchAllData(getActivity());
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String files_icon = row2.getString(row2.getColumnIndexOrThrow("files_icon"));
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));
            final File pathFile = new File(files_attachment);

            View v = super.getView(position, convertView, parent);
            final ImageView iv = (ImageView) v.findViewById(R.id.icon_notes);

            iv.setVisibility(View.VISIBLE);

            if (pathFile.isDirectory()) {
                iv.setImageResource(R.drawable.folder);
            } else {
                switch (files_icon) {
                case "":
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            iv.setImageResource(R.drawable.arrow_up);
                        }
                    }, 200);
                    break;
                case ".gif":
                case ".bmp":
                case ".tiff":
                case ".svg":
                case ".png":
                case ".jpg":
                case ".JPG":
                case ".jpeg":
                    try {
                        Glide.with(getActivity()).load(files_attachment) // or URI/path
                                .override(76, 76).centerCrop().into(iv); //imageView to set thumbnail to
                    } catch (Exception e) {
                        Log.w("HHS_Moodle", "Error load thumbnail", e);
                        iv.setImageResource(R.drawable.file_image);
                    }
                    break;
                case ".m3u8":
                case ".mp3":
                case ".wma":
                case ".midi":
                case ".wav":
                case ".aac":
                case ".aif":
                case ".amp3":
                case ".weba":
                case ".ogg":
                    iv.setImageResource(R.drawable.file_music);
                    break;
                case ".mpeg":
                case ".mp4":
                case ".webm":
                case ".qt":
                case ".3gp":
                case ".3g2":
                case ".avi":
                case ".f4v":
                case ".flv":
                case ".h261":
                case ".h263":
                case ".h264":
                case ".asf":
                case ".wmv":
                    try {
                        Glide.with(getActivity()).load(files_attachment) // or URI/path
                                .override(76, 76).centerCrop().into(iv); //imageView to set thumbnail to
                    } catch (Exception e) {
                        Log.w("HHS_Moodle", "Error load thumbnail", e);
                        iv.setImageResource(R.drawable.file_video);
                    }
                    break;
                case ".vcs":
                case ".vcf":
                case ".css":
                case ".ics":
                case ".conf":
                case ".config":
                case ".java":
                case ".html":
                    iv.setImageResource(R.drawable.file_xml);
                    break;
                case ".apk":
                    iv.setImageResource(R.drawable.android);
                    break;
                case ".pdf":
                    iv.setImageResource(R.drawable.file_pdf);
                    break;
                case ".rtf":
                case ".csv":
                case ".txt":
                case ".doc":
                case ".xls":
                case ".ppt":
                case ".docx":
                case ".pptx":
                case ".xlsx":
                case ".odt":
                case ".ods":
                case ".odp":
                    iv.setImageResource(R.drawable.file_document);
                    break;
                case ".zip":
                case ".rar":
                    iv.setImageResource(R.drawable.zip_box);
                    break;
                default:
                    iv.setImageResource(R.drawable.file);
                    break;
                }
            }
            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_filesBY", "files_title");
    sharedPref.edit().putString("filter_filesBY", "files_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));

            final File pathFile = new File(files_attachment);

            if (pathFile.isDirectory()) {
                try {
                    sharedPref.edit().putString("files_startFolder", files_attachment).apply();
                    setFilesList();
                } catch (Exception e) {
                    Snackbar.make(lv, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
                }
            } else if (files_attachment.equals("")) {
                try {
                    final File pathActual = new File(sharedPref.getString("files_startFolder",
                            Environment.getExternalStorageDirectory().getPath()));
                    sharedPref.edit().putString("files_startFolder", pathActual.getParent()).apply();
                    setFilesList();
                } catch (Exception e) {
                    Snackbar.make(lv, R.string.toast_directory, Snackbar.LENGTH_LONG).show();
                }
            } else {
                helper_main.openAtt(getActivity(), lv, files_attachment);
            }
        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String files_title = row2.getString(row2.getColumnIndexOrThrow("files_title"));
            final String files_attachment = row2.getString(row2.getColumnIndexOrThrow("files_attachment"));

            final File pathFile = new File(files_attachment);

            if (pathFile.isDirectory()) {

                Snackbar snackbar = Snackbar.make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                sharedPref.edit().putString("files_startFolder", pathFile.getParent()).apply();
                                deleteRecursive(pathFile);
                                setFilesList();
                            }
                        });
                snackbar.show();

            } else {

                final CharSequence[] options = { getString(R.string.choose_menu_2),
                        getString(R.string.choose_menu_3), getString(R.string.choose_menu_4) };

                final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                dialog.setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
                dialog.setItems(options, new DialogInterface.OnClickListener() {
                    @SuppressWarnings("ResultOfMethodCallIgnored")
                    @Override
                    public void onClick(DialogInterface dialog, int item) {

                        if (options[item].equals(getString(R.string.choose_menu_2))) {

                            if (pathFile.exists()) {
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("image/png");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, files_title);
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, files_title);
                                Uri bmpUri = Uri.fromFile(pathFile);
                                sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.app_share_file))));
                            }
                        }
                        if (options[item].equals(getString(R.string.choose_menu_4))) {
                            Snackbar snackbar = Snackbar
                                    .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                    .setAction(R.string.toast_yes, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            pathFile.delete();
                                            setFilesList();
                                        }
                                    });
                            snackbar.show();
                        }
                        if (options[item].equals(getString(R.string.choose_menu_3))) {

                            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(
                                    getActivity());
                            View dialogView = View.inflate(getActivity(), R.layout.dialog_edit_file, null);

                            final EditText edit_title = (EditText) dialogView.findViewById(R.id.pass_title);
                            edit_title.setText(files_title);

                            builder.setView(dialogView);
                            builder.setTitle(R.string.choose_title);
                            builder.setPositiveButton(R.string.toast_yes,
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(DialogInterface dialog, int whichButton) {

                                            String inputTag = edit_title.getText().toString().trim();

                                            File dir = pathFile.getParentFile();
                                            File to = new File(dir, inputTag);

                                            pathFile.renameTo(to);
                                            pathFile.delete();
                                            setFilesList();
                                        }
                                    });
                            builder.setNegativeButton(R.string.toast_cancel,
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            dialog.cancel();
                                        }
                                    });
                            AlertDialog dialog2 = builder.create();
                            // Display the custom alert dialog on interface
                            dialog2.show();
                            helper_main.showKeyboard(getActivity(), edit_title);
                        }
                    }
                });
                dialog.show();
            }

            return true;
        }
    });
}

From source file:org.totschnig.myexpenses.activity.ExpenseEdit.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one_expense);

    mDateFormat = android.text.format.DateFormat.getDateFormat(this);
    mTimeFormat = android.text.format.DateFormat.getTimeFormat(this);

    setupToolbar();/* w  w  w .  j a v a 2s.c  om*/
    mManager = getSupportLoaderManager();
    //we enable it only after accountcursor has been loaded, preventing NPE when user clicks on it early
    configTypeButton();
    mTypeButton.setEnabled(false);
    mCommentText = (EditText) findViewById(R.id.Comment);
    mTitleText = (EditText) findViewById(R.id.Title);
    mReferenceNumberText = (EditText) findViewById(R.id.Number);
    mDateButton = (Button) findViewById(R.id.DateButton);
    mAttachPictureButton = (ImageView) findViewById(R.id.AttachImage);
    mPictureViewContainer = (FrameLayout) findViewById(R.id.picture_container);
    mTimeButton = (Button) findViewById(R.id.TimeButton);
    mPayeeLabel = (TextView) findViewById(R.id.PayeeLabel);
    mPayeeText = (AutoCompleteTextView) findViewById(R.id.Payee);
    mTransferAmountText = (AmountEditText) findViewById(R.id.TranferAmount);
    mExchangeRate1Text = (AmountEditText) findViewById(R.id.ExchangeRate_1);
    mExchangeRate1Text.setFractionDigits(EXCHANGE_RATE_FRACTION_DIGITS);
    mExchangeRate1Text.addTextChangedListener(new LinkedExchangeRateTextWatchter(true));
    mExchangeRate2Text = (AmountEditText) findViewById(R.id.ExchangeRate_2);
    mExchangeRate2Text.setFractionDigits(EXCHANGE_RATE_FRACTION_DIGITS);
    mExchangeRate2Text.addTextChangedListener(new LinkedExchangeRateTextWatchter(false));

    mPayeeAdapter = new SimpleCursorAdapter(this, R.layout.support_simple_spinner_dropdown_item, null,
            new String[] { KEY_PAYEE_NAME }, new int[] { android.R.id.text1 }, 0);
    mPayeeText.setAdapter(mPayeeAdapter);
    mPayeeAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        @SuppressLint("NewApi")
        public Cursor runQuery(CharSequence str) {
            if (str == null) {
                return null;
            }
            String search = Utils.esacapeSqlLikeExpression(Utils.normalize(str.toString()));
            //we accept the string at the beginning of a word
            String selection = KEY_PAYEE_NAME_NORMALIZED + " LIKE ? OR " + KEY_PAYEE_NAME_NORMALIZED
                    + " LIKE ? OR " + KEY_PAYEE_NAME_NORMALIZED + " LIKE ?";
            String[] selectArgs = { search + "%", "% " + search + "%", "%." + search + "%" };
            return getContentResolver().query(TransactionProvider.PAYEES_URI, new String[] { KEY_ROWID,
                    KEY_PAYEE_NAME,
                    "(SELECT max(" + KEY_ROWID + ") FROM " + TABLE_TRANSACTIONS + " WHERE " + WHERE_NOT_SPLIT
                            + " AND " + KEY_PAYEEID + " = " + TABLE_PAYEES + "." + KEY_ROWID + ")" },
                    selection, selectArgs, null);
        }
    });

    mPayeeAdapter.setCursorToStringConverter(new CursorToStringConverter() {
        public CharSequence convertToString(Cursor cur) {
            return cur.getString(1);
        }
    });
    mPayeeText.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor c = (Cursor) mPayeeAdapter.getItem(position);
            if (c.moveToPosition(position)) {
                mTransaction.updatePayeeWithId(c.getString(1), c.getLong(0));
                if (mNewInstance && mTransaction != null
                        && !(mTransaction instanceof Template || mTransaction instanceof SplitTransaction)) {
                    //moveToPosition should not be necessary,
                    //but has been reported to not be positioned correctly on samsung GT-I8190N
                    if (!c.isNull(2)) {
                        if (PrefKey.AUTO_FILL_HINT_SHOWN.getBoolean(false)) {
                            if (PrefKey.AUTO_FILL.getBoolean(true)) {
                                startAutoFill(c.getLong(2));
                            }
                        } else {
                            Bundle b = new Bundle();
                            b.putLong(KEY_ROWID, c.getLong(2));
                            b.putInt(ConfirmationDialogFragment.KEY_TITLE, R.string.dialog_title_information);
                            b.putString(ConfirmationDialogFragment.KEY_MESSAGE,
                                    getString(R.string.hint_auto_fill));
                            b.putInt(ConfirmationDialogFragment.KEY_COMMAND_POSITIVE, R.id.AUTO_FILL_COMMAND);
                            b.putString(ConfirmationDialogFragment.KEY_PREFKEY,
                                    PrefKey.AUTO_FILL_HINT_SHOWN.getKey());
                            b.putInt(ConfirmationDialogFragment.KEY_POSITIVE_BUTTON_LABEL, R.string.yes);
                            b.putInt(ConfirmationDialogFragment.KEY_NEGATIVE_BUTTON_LABEL, R.string.no);
                            ConfirmationDialogFragment.newInstance(b).show(getSupportFragmentManager(),
                                    "AUTO_FILL_HINT");
                        }
                    }
                }
            }
        }
    });

    mCategoryButton = (Button) findViewById(R.id.Category);
    mPlanButton = (Button) findViewById(R.id.Plan);
    mMethodSpinner = (Spinner) findViewById(R.id.Method);
    mAccountSpinner = new SpinnerHelper(findViewById(R.id.Account));
    mTransferAccountSpinner = new SpinnerHelper(findViewById(R.id.TransferAccount));
    mTransferAccountSpinner.setOnItemSelectedListener(this);
    mStatusSpinner = new SpinnerHelper(findViewById(R.id.Status));
    mReccurenceSpinner = new SpinnerHelper(findViewById(R.id.Recurrence));
    mPlanToggleButton = (ToggleButton) findViewById(R.id.PlanExecutionAutomatic);
    TextPaint paint = mPlanToggleButton.getPaint();
    int automatic = (int) paint.measureText(getString(R.string.plan_automatic));
    int manual = (int) paint.measureText(getString(R.string.plan_manual));
    mPlanToggleButton.setWidth((automatic > manual ? automatic : manual) + +mPlanToggleButton.getPaddingLeft()
            + mPlanToggleButton.getPaddingRight());

    mRowId = Utils.getFromExtra(getIntent().getExtras(), KEY_ROWID, 0);

    //upon orientation change stored in instance state, since new splitTransactions are immediately persisted to DB
    if (savedInstanceState != null) {
        mSavedInstance = true;
        mRowId = savedInstanceState.getLong(KEY_ROWID);
        mPictureUri = savedInstanceState.getParcelable(KEY_PICTURE_URI);
        mPictureUriTemp = savedInstanceState.getParcelable(KEY_PICTURE_URI_TMP);
        setPicture();

        mCalendar = (Calendar) savedInstanceState.getSerializable(KEY_CALENDAR);
        mLabel = savedInstanceState.getString(KEY_LABEL);
        if ((mCatId = savedInstanceState.getLong(KEY_CATID)) == 0L) {
            mCatId = null;
        }
        if ((mMethodId = savedInstanceState.getLong(KEY_METHODID)) == 0L)
            mMethodId = null;
        if ((mAccountId = savedInstanceState.getLong(KEY_ACCOUNTID)) == 0L) {
            mAccountId = null;
        } else {
            //once user has selected account, we no longer want
            //the passed in KEY_CURRENCY to override it in onLoadFinished
            getIntent().removeExtra(KEY_CURRENCY);
        }
        if ((mTransferAccountId = savedInstanceState.getLong(KEY_TRANSFER_ACCOUNT)) == 0L)
            mTransferAccountId = null;
    }
    mTemplateId = getIntent().getLongExtra(KEY_TEMPLATEID, 0);
    //were we called from a notification
    int notificationId = getIntent().getIntExtra(MyApplication.KEY_NOTIFICATION_ID, 0);
    if (notificationId > 0) {
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationId);
    }

    CrStatusAdapter sAdapter = new CrStatusAdapter(this) {

        @Override
        public boolean isEnabled(int position) {
            //if the transaction is reconciled, the status can not be changed
            //otherwise only unreconciled and cleared can be set
            return mTransaction != null && mTransaction.crStatus != CrStatus.RECONCILED
                    && position != CrStatus.RECONCILED.ordinal();
        }
    };
    mStatusSpinner.setAdapter(sAdapter);

    //1. fetch the transaction or create a new instance
    if (mRowId != 0 || mTemplateId != 0) {
        mNewInstance = false;
        int taskId;
        Serializable extra = null;
        Long objectId;
        if (mRowId != 0) {
            taskId = TaskExecutionFragment.TASK_INSTANTIATE_TRANSACTION;
            //if called with extra KEY_CLONE, we ask the task to clone, but no longer after orientation change
            extra = getIntent().getBooleanExtra(KEY_CLONE, false) && savedInstanceState == null;
            objectId = mRowId;
        } else {
            objectId = mTemplateId;
            //are we editing the template or instantiating a new one
            if ((mPlanInstanceId = getIntent().getLongExtra(KEY_INSTANCEID, 0)) != 0L) {
                taskId = TaskExecutionFragment.TASK_INSTANTIATE_TRANSACTION_FROM_TEMPLATE;
                mPlanInstanceDate = getIntent().getLongExtra(KEY_DATE, 0);
                mRecordTemplateWidget = getIntent().getBooleanExtra(AbstractWidget.EXTRA_START_FROM_WIDGET,
                        false) && !ContribFeature.TEMPLATE_WIDGET.hasAccess();
            } else {
                taskId = TaskExecutionFragment.TASK_INSTANTIATE_TEMPLATE;
            }
        }
        FragmentManager fm = getSupportFragmentManager();
        if (fm.findFragmentByTag(ProtectionDelegate.ASYNC_TAG) == null) {
            startTaskExecution(taskId, new Long[] { objectId }, extra, R.string.progress_dialog_loading);
        }
    } else {
        mOperationType = getIntent().getIntExtra(MyApplication.KEY_OPERATION_TYPE, MyExpenses.TYPE_TRANSACTION);
        if (!isValidType(mOperationType)) {
            mOperationType = MyExpenses.TYPE_TRANSACTION;
        }
        if (mOperationType == MyExpenses.TYPE_SPLIT && !ContribFeature.SPLIT_TRANSACTION.hasAccess()
                && ContribFeature.SPLIT_TRANSACTION.usagesLeft() < 1) {
            Toast.makeText(this, ContribFeature.SPLIT_TRANSACTION.buildRequiresString(this), Toast.LENGTH_LONG)
                    .show();
            finish();
            return;
        }
        final Long parentId = getIntent().getLongExtra(KEY_PARENTID, 0);
        final boolean isNewTemplate = getIntent().getBooleanExtra(KEY_NEW_TEMPLATE, false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        View spinner = findViewById(R.id.OperationType);
        mOperationTypeSpinner = new SpinnerHelper(spinner);
        spinner.setVisibility(View.VISIBLE);
        List<Integer> allowedOperationTypes = new ArrayList<>();
        allowedOperationTypes.add(MyExpenses.TYPE_TRANSACTION);
        allowedOperationTypes.add(MyExpenses.TYPE_TRANSFER);
        if (!isNewTemplate && parentId == 0) {
            allowedOperationTypes.add(MyExpenses.TYPE_SPLIT);
        }
        mOperationTypeAdapter = new OperationTypeAdapter(this, allowedOperationTypes, isNewTemplate,
                parentId != 0);
        mOperationTypeSpinner.setAdapter(mOperationTypeAdapter);
        resetOperationType();
        mOperationTypeSpinner.setOnItemSelectedListener(this);
        Long accountId = getIntent().getLongExtra(KEY_ACCOUNTID, 0);
        if (isNewTemplate) {
            mTransaction = Template.getTypedNewInstance(mOperationType, accountId);
        } else {
            switch (mOperationType) {
            case MyExpenses.TYPE_TRANSACTION:
                if (accountId == 0L) {
                    accountId = MyApplication.getInstance().getSettings()
                            .getLong(PREFKEY_TRANSACTION_LAST_ACCOUNT_FROM_WIDGET, 0L);
                }
                mTransaction = parentId == 0L ? Transaction.getNewInstance(accountId)
                        : SplitPartCategory.getNewInstance(accountId, parentId);
                break;
            case MyExpenses.TYPE_TRANSFER:
                Long transfer_account = 0L;
                if (accountId == 0L) {
                    accountId = MyApplication.getInstance().getSettings()
                            .getLong(PREFKEY_TRANSFER_LAST_ACCOUNT_FROM_WIDGET, 0L);
                    transfer_account = MyApplication.getInstance().getSettings()
                            .getLong(PREFKEY_TRANSFER_LAST_TRANSFER_ACCOUNT_FROM_WIDGET, 0L);
                }
                mTransaction = parentId == 0L ? Transfer.getNewInstance(accountId, transfer_account)
                        : SplitPartTransfer.getNewInstance(accountId, parentId, transfer_account);
                break;
            case MyExpenses.TYPE_SPLIT:
                if (accountId == 0L) {
                    accountId = MyApplication.getInstance().getSettings()
                            .getLong(PREFKEY_SPLIT_LAST_ACCOUNT_FROM_WIDGET, 0L);
                }
                mTransaction = SplitTransaction.getNewInstance(accountId);
                //Split transactions are returned persisted to db and already have an id
                if (mTransaction != null) {
                    mRowId = mTransaction.getId();
                }
                break;
            }
        }
        if (mTransaction == null) {
            String errMsg = "Error instantiating transaction for account " + accountId;
            AcraHelper.report(new IllegalStateException(errMsg), "Extras", getIntent().getExtras().toString());
            Toast.makeText(this, errMsg, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        if (!mSavedInstance) {
            //processing data from user switching operation type
            Transaction cached = (Transaction) getIntent().getSerializableExtra(KEY_CACHED_DATA);
            if (cached != null) {
                mTransaction.accountId = cached.accountId;
                mCalendar.setTime(cached.getDate());
                mPictureUri = getIntent().getParcelableExtra(KEY_CACHED_PICTURE_URI);
                setPicture();
                mTransaction.methodId = cached.methodId;
            }
        }
        setup();
    }
}

From source file:de.baumann.hhsmoodle.data_notes.Notes_Fragment.java

public void setNotesList() {

    if (isFABOpen) {
        closeFABMenu();/* w  w w .j  a va  2s .  co m*/
    }

    //display data
    final int layoutstyle = R.layout.list_item_notes;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "note_title", "note_content", "note_creation" };
    final Cursor row = db.fetchAllData(getActivity());
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String note_title = row2.getString(row2.getColumnIndexOrThrow("note_title"));
            final String note_content = row2.getString(row2.getColumnIndexOrThrow("note_content"));
            final String note_icon = row2.getString(row2.getColumnIndexOrThrow("note_icon"));
            final String note_attachment = row2.getString(row2.getColumnIndexOrThrow("note_attachment"));
            final String note_creation = row2.getString(row2.getColumnIndexOrThrow("note_creation"));

            View v = super.getView(position, convertView, parent);
            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);
            ImageView iv_attachment = (ImageView) v.findViewById(R.id.att_notes);
            helper_main.switchIcon(getActivity(), note_icon, "note_icon", iv_icon);

            switch (note_attachment) {
            case "":
                iv_attachment.setVisibility(View.GONE);
                break;
            default:
                iv_attachment.setVisibility(View.VISIBLE);
                iv_attachment.setImageResource(R.drawable.ic_attachment);
                break;
            }

            File file = new File(note_attachment);
            if (!file.exists()) {
                iv_attachment.setVisibility(View.GONE);
            }

            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    final helper_main.Item[] items = {
                            new helper_main.Item(getString(R.string.note_priority_0), R.drawable.circle_green),
                            new helper_main.Item(getString(R.string.note_priority_1), R.drawable.circle_yellow),
                            new helper_main.Item(getString(R.string.note_priority_2), R.drawable.circle_red), };

                    ListAdapter adapter = new ArrayAdapter<helper_main.Item>(getActivity(),
                            android.R.layout.select_dialog_item, android.R.id.text1, items) {
                        @NonNull
                        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                            //Use super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView) v.findViewById(android.R.id.text1);
                            tv.setTextSize(18);
                            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                    new AlertDialog.Builder(getActivity())
                            .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).setAdapter(adapter, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int item) {
                                    if (item == 0) {
                                        db.update(Integer.parseInt(_id), note_title, note_content, "3",
                                                note_attachment, note_creation);
                                        setNotesList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), note_title, note_content, "2",
                                                note_attachment, note_creation);
                                        setNotesList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), note_title, note_content, "1",
                                                note_attachment, note_creation);
                                        setNotesList();
                                    }
                                }
                            }).show();
                }
            });
            iv_attachment.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    helper_main.openAtt(getActivity(), lv, note_attachment);
                }
            });
            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_noteBY", "note_title");
    sharedPref.edit().putString("filter_noteBY", "note_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String note_title = row2.getString(row2.getColumnIndexOrThrow("note_title"));
            final String note_content = row2.getString(row2.getColumnIndexOrThrow("note_content"));
            final String note_icon = row2.getString(row2.getColumnIndexOrThrow("note_icon"));
            final String note_attachment = row2.getString(row2.getColumnIndexOrThrow("note_attachment"));
            final String note_creation = row2.getString(row2.getColumnIndexOrThrow("note_creation"));

            final Button attachment;
            final TextView textInput;

            LayoutInflater inflater = getActivity().getLayoutInflater();

            final ViewGroup nullParent = null;
            final View dialogView = inflater.inflate(R.layout.dialog_note_show, nullParent);

            final String attName = note_attachment.substring(note_attachment.lastIndexOf("/") + 1);
            final String att = getString(R.string.app_att) + ": " + attName;

            attachment = (Button) dialogView.findViewById(R.id.button_att);
            if (attName.equals("")) {
                attachment.setVisibility(View.GONE);
            } else {
                attachment.setText(att);
            }

            textInput = (TextView) dialogView.findViewById(R.id.note_text_input);
            if (note_content.isEmpty()) {
                textInput.setVisibility(View.GONE);
            } else {
                textInput.setText(note_content);
                Linkify.addLinks(textInput, Linkify.WEB_URLS);
            }

            attachment.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    helper_main.openAtt(getActivity(), textInput, note_attachment);
                }
            });

            final ImageView be = (ImageView) dialogView.findViewById(R.id.imageButtonPri);
            final ImageView attImage = (ImageView) dialogView.findViewById(R.id.attImage);

            File file2 = new File(note_attachment);
            if (!file2.exists()) {
                attachment.setVisibility(View.GONE);
                attImage.setVisibility(View.GONE);
            } else if (note_attachment.contains(".gif") || note_attachment.contains(".bmp")
                    || note_attachment.contains(".tiff") || note_attachment.contains(".png")
                    || note_attachment.contains(".jpg") || note_attachment.contains(".JPG")
                    || note_attachment.contains(".jpeg") || note_attachment.contains(".mpeg")
                    || note_attachment.contains(".mp4") || note_attachment.contains(".3gp")
                    || note_attachment.contains(".3g2") || note_attachment.contains(".avi")
                    || note_attachment.contains(".flv") || note_attachment.contains(".h261")
                    || note_attachment.contains(".h263") || note_attachment.contains(".h264")
                    || note_attachment.contains(".asf") || note_attachment.contains(".wmv")) {
                attImage.setVisibility(View.VISIBLE);

                try {
                    Glide.with(getActivity()).load(note_attachment) // or URI/path
                            .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(attImage); //imageView to set thumbnail to
                } catch (Exception e) {
                    Log.w("HHS_Moodle", "Error load thumbnail", e);
                    attImage.setVisibility(View.GONE);
                }

                attImage.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        helper_main.openAtt(getActivity(), attImage, note_attachment);
                    }
                });
            }

            switch (note_icon) {
            case "3":
                be.setImageResource(R.drawable.circle_green);
                break;
            case "2":
                be.setImageResource(R.drawable.circle_yellow);
                break;
            case "1":
                be.setImageResource(R.drawable.circle_red);
                break;
            }

            android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(getActivity())
                    .setTitle(note_title).setView(dialogView)
                    .setPositiveButton(R.string.toast_yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setNegativeButton(R.string.note_edit, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            sharedPref.edit().putString("handleTextTitle", note_title)
                                    .putString("handleTextText", note_content)
                                    .putString("handleTextIcon", note_icon).putString("handleTextSeqno", _id)
                                    .putString("handleTextAttachment", note_attachment)
                                    .putString("handleTextCreate", note_creation).apply();
                            helper_main.switchToActivity(getActivity(), Activity_EditNote.class, false);
                        }
                    });
            dialog.show();
        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String note_title = row2.getString(row2.getColumnIndexOrThrow("note_title"));
            final String note_content = row2.getString(row2.getColumnIndexOrThrow("note_content"));
            final String note_icon = row2.getString(row2.getColumnIndexOrThrow("note_icon"));
            final String note_attachment = row2.getString(row2.getColumnIndexOrThrow("note_attachment"));
            final String note_creation = row2.getString(row2.getColumnIndexOrThrow("note_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark), getString(R.string.todo_share),
                    getString(R.string.todo_menu), getString(R.string.count_create),
                    getString(R.string.bookmark_createEvent) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.number_edit_entry))) {
                                Notes_helper.newNote(getActivity(), note_title, note_content, note_icon,
                                        note_attachment, note_creation, _id);
                            }

                            if (options[item].equals(getString(R.string.todo_share))) {
                                File attachment = new File(note_attachment);
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("text/plain");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, note_title);
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, note_content);

                                if (attachment.exists()) {
                                    Uri bmpUri = Uri.fromFile(attachment);
                                    sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                                }

                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.note_share_2))));
                            }

                            if (options[item].equals(getString(R.string.todo_menu))) {
                                Todo_helper.newTodo(getActivity(), note_title, note_content,
                                        getActivity().getString(R.string.note_content));
                            }

                            if (options[item].equals(getString(R.string.count_create))) {
                                Count_helper.newCount(getActivity(), note_title, note_content,
                                        getActivity().getString(R.string.note_content), false);
                            }

                            if (options[item].equals(getString(R.string.bookmark_createEvent))) {
                                helper_main.createCalendarEvent(getActivity(), note_title, note_content);
                            }

                            if (options[item].equals(getString(R.string.bookmark_remove_bookmark))) {
                                Snackbar snackbar = Snackbar
                                        .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                db.delete(Integer.parseInt(_id));
                                                setNotesList();
                                            }
                                        });
                                snackbar.show();
                            }
                        }
                    }).show();

            return true;
        }
    });
}

From source file:de.baumann.hhsmoodle.data_schedule.Schedule_Fragment.java

private void setScheduleList() {

    final int line = sharedPref.getInt("getLine", 1);
    //display data
    final int layoutstyle = R.layout.list_item_schedule;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.att_notes,
            R.id.textView_create_notes };
    String[] column = new String[] { "schedule_title", "schedule_content", "schedule_attachment",
            "schedule_creation" };
    final Cursor row = db.fetchAllData();
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override//from w w  w  .ja v a 2 s .co m
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String schedule_title = row2.getString(row2.getColumnIndexOrThrow("schedule_title"));
            final String schedule_content = row2.getString(row2.getColumnIndexOrThrow("schedule_content"));
            final String schedule_icon = row2.getString(row2.getColumnIndexOrThrow("schedule_icon"));
            final String schedule_attachment = row2
                    .getString(row2.getColumnIndexOrThrow("schedule_attachment"));
            final String schedule_creation = row2.getString(row2.getColumnIndexOrThrow("schedule_creation"));
            final String schedule_id = row2.getString(row2.getColumnIndexOrThrow("schedule_id"));

            View v = super.getView(position, convertView, parent);
            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);

            if (position == line) {
                v.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent_trans));
            } else {
                v.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_trans));
            }

            if (schedule_title.equals(getActivity().getString(R.string.schedule_weekend))
                    || schedule_title.equals(getActivity().getString(R.string.schedule_def_title))) {
                sharedPref.edit().putString("hour_" + schedule_id, "false").apply();
            } else {
                sharedPref.edit().putString("hour_" + schedule_id, "true").apply();
            }

            Subjects_helper.switchIcon(getActivity(), schedule_icon, "schedule_color", iv_icon);

            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    final helper_main.Item[] items = {
                            new helper_main.Item(getString(R.string.subjects_color_red), R.drawable.circle_red),
                            new helper_main.Item(getString(R.string.subjects_color_pink),
                                    R.drawable.circle_pink),
                            new helper_main.Item(getString(R.string.subjects_color_purple),
                                    R.drawable.circle_purple),
                            new helper_main.Item(getString(R.string.subjects_color_blue),
                                    R.drawable.circle_blue),
                            new helper_main.Item(getString(R.string.subjects_color_teal),
                                    R.drawable.circle_teal),
                            new helper_main.Item(getString(R.string.subjects_color_green),
                                    R.drawable.circle_green),
                            new helper_main.Item(getString(R.string.subjects_color_lime),
                                    R.drawable.circle_lime),
                            new helper_main.Item(getString(R.string.subjects_color_yellow),
                                    R.drawable.circle_yellow),
                            new helper_main.Item(getString(R.string.subjects_color_orange),
                                    R.drawable.circle_orange),
                            new helper_main.Item(getString(R.string.subjects_color_brown),
                                    R.drawable.circle_brown),
                            new helper_main.Item(getString(R.string.subjects_color_grey),
                                    R.drawable.circle_grey), };

                    ListAdapter adapter = new ArrayAdapter<helper_main.Item>(getActivity(),
                            android.R.layout.select_dialog_item, android.R.id.text1, items) {
                        @NonNull
                        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                            //Use super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView) v.findViewById(android.R.id.text1);
                            tv.setTextSize(18);
                            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                    new AlertDialog.Builder(getActivity())
                            .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).setAdapter(adapter, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int item) {
                                    if (item == 0) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "1",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "2",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "3",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 3) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "4",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 4) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "5",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 5) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "6",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 6) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "7",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 7) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "8",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 8) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "9",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 9) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "10",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    } else if (item == 10) {
                                        db.update(Integer.parseInt(_id), schedule_title, schedule_content, "11",
                                                schedule_attachment, schedule_creation, schedule_id);
                                        setScheduleList();
                                    }
                                }
                            }).show();
                }
            });

            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_scheduleBY", "schedule_title");
    sharedPref.edit().putString("filter_scheduleBY", "schedule_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String schedule_title = row2.getString(row2.getColumnIndexOrThrow("schedule_title"));

            final CharSequence[] options = { getString(R.string.schedule_todo),
                    getString(R.string.schedule_notes) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.schedule_todo))) {
                                sharedPref.edit().putString("filter_todo_subject", schedule_title).apply();
                                new Handler().postDelayed(new Runnable() {
                                    public void run() {
                                        helper_main.switchToActivity(getActivity(), Popup_todo.class, false);
                                    }
                                }, 200);
                            }
                            if (options[item].equals(getString(R.string.schedule_notes))) {
                                sharedPref.edit().putString("filter_note_subject", schedule_title).apply();
                                new Handler().postDelayed(new Runnable() {
                                    public void run() {
                                        helper_main.switchToActivity(getActivity(), Popup_note.class, false);
                                    }
                                }, 200);
                            }
                        }
                    }).show();
        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String schedule_title = row2.getString(row2.getColumnIndexOrThrow("schedule_title"));
            final String schedule_content = row2.getString(row2.getColumnIndexOrThrow("schedule_content"));
            final String schedule_icon = row2.getString(row2.getColumnIndexOrThrow("schedule_icon"));
            final String schedule_attachment = row2
                    .getString(row2.getColumnIndexOrThrow("schedule_attachment"));
            final String schedule_creation = row2.getString(row2.getColumnIndexOrThrow("schedule_creation"));
            final String schedule_id = row2.getString(row2.getColumnIndexOrThrow("schedule_id"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark), getString(R.string.todo_menu),
                    getString(R.string.bookmark_createNote), getString(R.string.count_create),
                    getString(R.string.bookmark_createEvent) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @SuppressWarnings("ConstantConditions")
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.number_edit_entry))) {

                                final CharSequence[] options = { getString(R.string.schedule_fromSubjectList),
                                        getString(R.string.todo_from_new) };
                                new AlertDialog.Builder(getActivity()).setPositiveButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                dialog.cancel();
                                            }
                                        }).setItems(options, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int item) {
                                                if (options[item]
                                                        .equals(getString(R.string.schedule_fromSubjectList))) {
                                                    sharedPref.edit().putString("edit_yes", "true")
                                                            .putInt("scroll", Integer.parseInt(schedule_id))
                                                            .putString("handleSubjectCreation",
                                                                    schedule_creation)
                                                            .putString("handleSubject_id", schedule_id)
                                                            .putString("handle_id", _id).apply();
                                                    helper_main.switchToActivity(getActivity(),
                                                            Popup_subjects.class, false);
                                                }

                                                if (options[item].equals(getString(R.string.todo_from_new))) {

                                                    LayoutInflater inflater = getActivity().getLayoutInflater();

                                                    final ViewGroup nullParent = null;
                                                    View dialogView = inflater
                                                            .inflate(R.layout.dialog_edit_subject, nullParent);

                                                    final EditText titleInput = (EditText) dialogView
                                                            .findViewById(R.id.subject_title_);
                                                    titleInput.setSelection(titleInput.getText().length());
                                                    titleInput.setText(schedule_title);
                                                    final EditText teacherInput = (EditText) dialogView
                                                            .findViewById(R.id.subject_teacher);
                                                    teacherInput.setText(schedule_content);
                                                    final EditText roomInput = (EditText) dialogView
                                                            .findViewById(R.id.subject_room);
                                                    roomInput.setText(schedule_attachment);

                                                    helper_main.showKeyboard(getActivity(), titleInput);

                                                    final ImageButton be = (ImageButton) dialogView
                                                            .findViewById(R.id.imageButtonPri);
                                                    assert be != null;

                                                    Subjects_helper.switchIcon(getActivity(), schedule_icon,
                                                            "schedule_color", be);

                                                    be.setOnClickListener(new View.OnClickListener() {

                                                        @Override
                                                        public void onClick(View arg0) {

                                                            final helper_main.Item[] items = {
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_red),
                                                                            R.drawable.circle_red),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_pink),
                                                                            R.drawable.circle_pink),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_purple),
                                                                            R.drawable.circle_purple),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_blue),
                                                                            R.drawable.circle_blue),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_teal),
                                                                            R.drawable.circle_teal),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_green),
                                                                            R.drawable.circle_green),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_lime),
                                                                            R.drawable.circle_lime),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_yellow),
                                                                            R.drawable.circle_yellow),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_orange),
                                                                            R.drawable.circle_orange),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_brown),
                                                                            R.drawable.circle_brown),
                                                                    new helper_main.Item(getString(
                                                                            R.string.subjects_color_grey),
                                                                            R.drawable.circle_grey), };

                                                            ListAdapter adapter = new ArrayAdapter<helper_main.Item>(
                                                                    getActivity(),
                                                                    android.R.layout.select_dialog_item,
                                                                    android.R.id.text1, items) {
                                                                @NonNull
                                                                public View getView(int position,
                                                                        View convertView,
                                                                        @NonNull ViewGroup parent) {
                                                                    //Use super class to create the View
                                                                    View v = super.getView(position,
                                                                            convertView, parent);
                                                                    TextView tv = (TextView) v
                                                                            .findViewById(android.R.id.text1);
                                                                    tv.setTextSize(18);
                                                                    tv.setCompoundDrawablesWithIntrinsicBounds(
                                                                            items[position].icon, 0, 0, 0);
                                                                    //Add margin between image and text (support various screen densities)
                                                                    int dp5 = (int) (24
                                                                            * getActivity().getResources()
                                                                                    .getDisplayMetrics().density
                                                                            + 0.5f);
                                                                    tv.setCompoundDrawablePadding(dp5);

                                                                    return v;
                                                                }
                                                            };

                                                            new android.app.AlertDialog.Builder(getActivity())
                                                                    .setPositiveButton(R.string.toast_cancel,
                                                                            new DialogInterface.OnClickListener() {

                                                                                public void onClick(
                                                                                        DialogInterface dialog,
                                                                                        int whichButton) {
                                                                                    dialog.cancel();
                                                                                }
                                                                            })
                                                                    .setAdapter(adapter,
                                                                            new DialogInterface.OnClickListener() {
                                                                                public void onClick(
                                                                                        DialogInterface dialog,
                                                                                        int item) {
                                                                                    if (item == 0) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_red);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "1")
                                                                                                .apply();
                                                                                    } else if (item == 1) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_pink);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "2")
                                                                                                .apply();
                                                                                    } else if (item == 2) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_purple);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "3")
                                                                                                .apply();
                                                                                    } else if (item == 3) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_blue);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "4")
                                                                                                .apply();
                                                                                    } else if (item == 4) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_teal);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "5")
                                                                                                .apply();
                                                                                    } else if (item == 5) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_green);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "6")
                                                                                                .apply();
                                                                                    } else if (item == 6) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_lime);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "7")
                                                                                                .apply();
                                                                                    } else if (item == 7) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_yellow);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "8")
                                                                                                .apply();
                                                                                    } else if (item == 8) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_orange);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "9")
                                                                                                .apply();
                                                                                    } else if (item == 9) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_brown);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "10")
                                                                                                .apply();
                                                                                    } else if (item == 10) {
                                                                                        be.setImageResource(
                                                                                                R.drawable.circle_grey);
                                                                                        sharedPref.edit()
                                                                                                .putString(
                                                                                                        "schedule_color",
                                                                                                        "11")
                                                                                                .apply();
                                                                                    }
                                                                                }
                                                                            })
                                                                    .show();
                                                        }
                                                    });

                                                    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(
                                                            getActivity());
                                                    builder.setTitle(R.string.subjects_edit);
                                                    builder.setView(dialogView);
                                                    builder.setPositiveButton(R.string.toast_yes,
                                                            new DialogInterface.OnClickListener() {

                                                                public void onClick(DialogInterface dialog,
                                                                        int whichButton) {

                                                                    String inputTitle = titleInput.getText()
                                                                            .toString().trim();
                                                                    String inputTeacher = teacherInput.getText()
                                                                            .toString().trim();
                                                                    String inputRoom = roomInput.getText()
                                                                            .toString().trim();

                                                                    db.update(Integer.parseInt(_id), inputTitle,
                                                                            inputTeacher,
                                                                            sharedPref.getString(
                                                                                    "schedule_color", ""),
                                                                            inputRoom, schedule_creation,
                                                                            schedule_id);
                                                                    dialog.dismiss();
                                                                    setScheduleList();
                                                                    lv.setSelection(
                                                                            Integer.parseInt(schedule_id) - 1);
                                                                    Snackbar.make(lv, R.string.bookmark_added,
                                                                            Snackbar.LENGTH_SHORT).show();
                                                                }
                                                            });
                                                    builder.setNegativeButton(R.string.toast_cancel,
                                                            new DialogInterface.OnClickListener() {

                                                                public void onClick(DialogInterface dialog,
                                                                        int whichButton) {
                                                                    dialog.cancel();
                                                                }
                                                            });

                                                    final android.support.v7.app.AlertDialog dialog2 = builder
                                                            .create();
                                                    dialog2.show();
                                                }

                                            }
                                        }).show();
                            }

                            if (options[item].equals(getString(R.string.bookmark_remove_bookmark))) {
                                Snackbar snackbar = Snackbar
                                        .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                db.update(Integer.parseInt(_id),
                                                        getString(R.string.schedule_def_teacher),
                                                        getString(R.string.schedule_def_teacher), "11",
                                                        getString(R.string.schedule_def_teacher),
                                                        schedule_creation, schedule_id);
                                                setScheduleList();
                                                lv.setSelection(Integer.parseInt(schedule_id) - 1);
                                            }
                                        });
                                snackbar.show();
                            }

                            if (options[item].equals(getString(R.string.todo_menu))) {
                                Todo_helper.newTodo(getActivity(), schedule_title, schedule_content,
                                        getActivity().getString(R.string.note_content));
                            }

                            if (options[item].equals(getString(R.string.count_create))) {
                                Count_helper.newCount(getActivity(), schedule_title, schedule_content,
                                        getActivity().getString(R.string.note_content), false);
                            }

                            if (options[item].equals(getString(R.string.bookmark_createEvent))) {
                                helper_main.createCalendarEvent(getActivity(), schedule_title,
                                        schedule_content);
                            }

                            if (options[item].equals(getString(R.string.bookmark_createNote))) {
                                Notes_helper.newNote(getActivity(), schedule_title, schedule_content, "", "",
                                        "", "");
                            }

                        }
                    }).show();
            return true;
        }
    });

    scrollToNow();
}

From source file:org.zirco.ui.activities.MainActivity.java

/**
 * Create main UI.//w w w .ja v a  2  s . c om
 */
private void buildComponents() {

    mToolsActionGrid = new QuickActionGrid(this);
    mToolsActionGrid.addQuickAction(new QuickAction(this, R.drawable.ic_btn_home, R.string.QuickAction_Home));
    mToolsActionGrid.addQuickAction(new QuickAction(this, R.drawable.ic_btn_share, R.string.QuickAction_Share));
    mToolsActionGrid
            .addQuickAction(new QuickAction(this, R.drawable.ic_btn_select, R.string.QuickAction_SelectText));
    mToolsActionGrid.addQuickAction(
            new QuickAction(this, R.drawable.ic_btn_mobile_view, R.string.QuickAction_MobileView));

    mToolsActionGrid.setOnQuickActionClickListener(new OnQuickActionClickListener() {
        @Override
        public void onQuickActionClicked(QuickActionWidget widget, int position) {
            switch (position) {
            case 0:
                navigateToHome();
                break;
            case 1:
                ApplicationUtils.sharePage(MainActivity.this, mCurrentWebView.getTitle(),
                        mCurrentWebView.getUrl());
                break;
            case 2:
                swithToSelectAndCopyTextMode();
                break;
            case 3:
                String currentUrl = mUrlEditText.getText().toString();

                // Do not reload mobile view if already on it.
                if (!currentUrl.startsWith(Constants.URL_GOOGLE_MOBILE_VIEW_NO_FORMAT)) {
                    String url = String.format(Constants.URL_GOOGLE_MOBILE_VIEW,
                            mUrlEditText.getText().toString());
                    navigateToUrl(url);
                }
                break;
            }
        }
    });

    mToolsActionGrid.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            mToolsActionGridVisible = false;
            startToolbarsHideRunnable();
        }
    });

    mGestureDetector = new GestureDetector(this, new GestureListener());

    mUrlBarVisible = true;

    mWebViews = new ArrayList<CustomWebView>();
    Controller.getInstance().setWebViewList(mWebViews);

    mBubbleRightView = (ImageView) findViewById(R.id.BubbleRightView);
    mBubbleRightView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setToolbarsVisibility(true);
        }
    });
    mBubbleRightView.setVisibility(View.GONE);

    mBubbleLeftView = (ImageView) findViewById(R.id.BubbleLeftView);
    mBubbleLeftView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setToolbarsVisibility(true);
        }
    });
    mBubbleLeftView.setVisibility(View.GONE);

    mViewPager = (CustomViewPager) findViewById(R.id.ViewPager);
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            Log.d("onPageSelected", Integer.toString(arg0));
            CustomWebView oldWebView = mPagerAdapter.getWebViewAtIndex(mCurrentDisplayedViewIndex);
            mCurrentDisplayedViewIndex = arg0;
            mCurrentWebView = mPagerAdapter.getWebViewAtIndex(mCurrentDisplayedViewIndex);

            oldWebView.doOnPause();
            mCurrentWebView.doOnResume();

            startToolbarsHideRunnable();

            showToastOnTabSwitch();

            updatePreviousNextTabViewsVisibility();
            updateUI();
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    mPagerAdapter = new CustomPagerAdapter(this, mViewPager);
    mViewPager.setAdapter(mPagerAdapter);

    mTopBar = (LinearLayout) findViewById(R.id.BarLayout);
    mTopBar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dummy event to steel it from the WebView, in case of clicking between the buttons.            
        }
    });

    mBottomBar = (LinearLayout) findViewById(R.id.BottomBarLayout);
    mBottomBar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dummy event to steel it from the WebView, in case of clicking between the buttons.            
        }
    });

    mPreviousTabView = (ImageView) findViewById(R.id.PreviousTabView);
    mPreviousTabView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPreviousTab(true);
        }
    });
    mPreviousTabView.setVisibility(View.GONE);

    mNextTabView = (ImageView) findViewById(R.id.NextTabView);
    mNextTabView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showNextTab(true);
        }
    });
    mNextTabView.setVisibility(View.GONE);

    String[] from = new String[] { UrlSuggestionCursorAdapter.URL_SUGGESTION_TITLE,
            UrlSuggestionCursorAdapter.URL_SUGGESTION_URL };
    int[] to = new int[] { R.id.AutocompleteTitle, R.id.AutocompleteUrl };

    UrlSuggestionCursorAdapter adapter = new UrlSuggestionCursorAdapter(this, R.layout.url_autocomplete_line,
            null, from, to);

    adapter.setCursorToStringConverter(new CursorToStringConverter() {
        @Override
        public CharSequence convertToString(Cursor cursor) {
            String aColumnString = cursor
                    .getString(cursor.getColumnIndex(UrlSuggestionCursorAdapter.URL_SUGGESTION_URL));
            return aColumnString;
        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            if ((constraint != null) && (constraint.length() > 0)) {
                return BookmarksProviderWrapper.getUrlSuggestions(getContentResolver(), constraint.toString(),
                        PreferenceManager.getDefaultSharedPreferences(MainActivity.this)
                                .getBoolean(Constants.PREFERENCE_USE_WEAVE, false));
            } else {
                return BookmarksProviderWrapper.getUrlSuggestions(getContentResolver(), null,
                        PreferenceManager.getDefaultSharedPreferences(MainActivity.this)
                                .getBoolean(Constants.PREFERENCE_USE_WEAVE, false));
            }
        }
    });

    mUrlEditText = (AutoCompleteTextView) findViewById(R.id.UrlText);
    mUrlEditText.setThreshold(1);
    mUrlEditText.setAdapter(adapter);

    mUrlEditText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                navigateToUrl();
                return true;
            }

            return false;
        }

    });

    mUrlTextWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            updateGoButton();
        }
    };

    mUrlEditText.addTextChangedListener(mUrlTextWatcher);

    mUrlEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // Select all when focus gained.
            if (hasFocus) {
                mUrlEditText.setSelection(0, mUrlEditText.getText().length());
            }
        }
    });

    mUrlEditText.setCompoundDrawablePadding(5);

    mGoButton = (ImageButton) findViewById(R.id.GoBtn);
    mGoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            if (mCurrentWebView.isLoading()) {
                mCurrentWebView.stopLoading();
            } else if (!mCurrentWebView.isSameUrl(mUrlEditText.getText().toString())) {
                navigateToUrl();
            } else {
                mCurrentWebView.reload();
            }
        }
    });

    mToolsButton = (ImageButton) findViewById(R.id.ToolsBtn);
    mToolsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mToolsActionGridVisible = true;
            mToolsActionGrid.show(v);
        }
    });

    mProgressBar = (ProgressBar) findViewById(R.id.WebViewProgress);
    mProgressBar.setMax(100);

    mPreviousButton = (ImageButton) findViewById(R.id.PreviousBtn);
    mNextButton = (ImageButton) findViewById(R.id.NextBtn);

    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            navigatePrevious();
        }
    });

    mNextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            navigateNext();
        }
    });

    mNewTabButton = (ImageButton) findViewById(R.id.NewTabBtn);
    mNewTabButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            addTab(true, mCurrentDisplayedViewIndex + 1);
        }
    });

    mRemoveTabButton = (ImageButton) findViewById(R.id.RemoveTabBtn);
    mRemoveTabButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            removeCurrentTab();
        }
    });

    mQuickButton = (ImageButton) findViewById(R.id.QuickBtn);
    mQuickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onQuickButton();
        }
    });

}

From source file:de.baumann.hhsmoodle.data_count.Count_Fragment.java

public void setCountList() {

    if (isFABOpen) {
        closeFABMenu();//from w  w  w. j av  a  2s.  c  o m
    }

    //display data
    final int layoutstyle = R.layout.list_item_notes;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "count_title", "count_content", "count_creation" };
    final Cursor row = db.fetchAllData(getActivity());
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String count_title = row2.getString(row2.getColumnIndexOrThrow("count_title"));
            final String count_content = row2.getString(row2.getColumnIndexOrThrow("count_content"));
            final String count_icon = row2.getString(row2.getColumnIndexOrThrow("count_icon"));
            final String count_attachment = row2.getString(row2.getColumnIndexOrThrow("count_attachment"));
            final String count_creation = row2.getString(row2.getColumnIndexOrThrow("count_creation"));

            View v = super.getView(position, convertView, parent);
            final TextView tv = (TextView) v.findViewById(R.id.text);

            if (count_attachment.isEmpty()) {

                tv.setText(count_content);

                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        int n = tv.getLineCount();

                        StringBuilder sb = new StringBuilder(n);
                        for (int i = 0; i < n; ++i) {
                            sb.append("0" + '\n');
                        }
                        String result = sb.toString();

                        db.update(Integer.parseInt(_id), count_title, count_content, count_icon, result,
                                count_creation);
                        setCountList();
                    }
                }, 500);
            }

            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);
            helper_main.switchIcon(getActivity(), count_icon, "count_icon", iv_icon);

            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    final helper_main.Item[] items = {
                            new helper_main.Item(getString(R.string.note_priority_0), R.drawable.circle_green),
                            new helper_main.Item(getString(R.string.note_priority_1), R.drawable.circle_yellow),
                            new helper_main.Item(getString(R.string.note_priority_2), R.drawable.circle_red), };

                    ListAdapter adapter = new ArrayAdapter<helper_main.Item>(getActivity(),
                            android.R.layout.select_dialog_item, android.R.id.text1, items) {
                        @NonNull
                        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                            //Use super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView) v.findViewById(android.R.id.text1);
                            tv.setTextSize(18);
                            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                    new AlertDialog.Builder(getActivity())
                            .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).setAdapter(adapter, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int item) {
                                    if (item == 0) {
                                        db.update(Integer.parseInt(_id), count_title, count_content, "3",
                                                count_attachment, count_creation);
                                        setCountList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), count_title, count_content, "2",
                                                count_attachment, count_creation);
                                        setCountList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), count_title, count_content, "1",
                                                count_attachment, count_creation);
                                        setCountList();
                                    }
                                }
                            }).show();
                }
            });
            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_countBY", "note_title");
    sharedPref.edit().putString("filter_countBY", "note_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String count_title = row2.getString(row2.getColumnIndexOrThrow("count_title"));
            final String count_content = row2.getString(row2.getColumnIndexOrThrow("count_content"));
            final String count_icon = row2.getString(row2.getColumnIndexOrThrow("count_icon"));
            final String count_attachment = row2.getString(row2.getColumnIndexOrThrow("count_attachment"));
            final String count_creation = row2.getString(row2.getColumnIndexOrThrow("count_creation"));

            sharedPref.edit().putString("count_title", count_title).apply();
            sharedPref.edit().putString("count_content", count_content).apply();
            sharedPref.edit().putString("count_seqno", _id).apply();
            sharedPref.edit().putString("count_icon", count_icon).apply();
            sharedPref.edit().putString("count_create", count_creation).apply();
            sharedPref.edit().putString("count_attachment", count_attachment).apply();

            helper_main.switchToActivity(getActivity(), Activity_count.class, false);
        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String count_title = row2.getString(row2.getColumnIndexOrThrow("count_title"));
            final String count_content = row2.getString(row2.getColumnIndexOrThrow("count_content"));
            final String count_icon = row2.getString(row2.getColumnIndexOrThrow("count_icon"));
            final String count_attachment = row2.getString(row2.getColumnIndexOrThrow("count_attachment"));
            final String count_creation = row2.getString(row2.getColumnIndexOrThrow("count_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark), getString(R.string.todo_share),
                    getString(R.string.bookmark_createNote), getString(R.string.bookmark_createEvent) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.number_edit_entry))) {

                                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                View dialogView = View.inflate(getActivity(), R.layout.dialog_edit_title, null);

                                final EditText edit_title = (EditText) dialogView.findViewById(R.id.pass_title);
                                edit_title.setHint(R.string.bookmark_edit_title);
                                edit_title.setText(count_title);

                                builder.setView(dialogView);
                                builder.setTitle(R.string.bookmark_edit_title);
                                builder.setPositiveButton(R.string.toast_yes,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                                String inputTag = edit_title.getText().toString().trim();
                                                db.update(Integer.parseInt(_id), inputTag, count_content,
                                                        count_icon, count_attachment, count_creation);
                                                setCountList();
                                                Snackbar.make(lv, R.string.bookmark_added,
                                                        Snackbar.LENGTH_SHORT).show();
                                            }
                                        });
                                builder.setNegativeButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                dialog.cancel();
                                            }
                                        });

                                final AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

                            if (options[item].equals(getString(R.string.todo_share))) {
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("text/plain");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, count_title);
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, count_content);
                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.note_share_2))));
                            }

                            if (options[item].equals(getString(R.string.bookmark_createEvent))) {
                                helper_main.createCalendarEvent(getActivity(), count_title, count_content);
                            }

                            if (options[item].equals(getString(R.string.bookmark_remove_bookmark))) {
                                Snackbar snackbar = Snackbar
                                        .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                db.delete(Integer.parseInt(_id));
                                                setCountList();
                                            }
                                        });
                                snackbar.show();
                            }

                            if (options[item].equals(getString(R.string.bookmark_createNote))) {
                                Notes_helper.newNote(getActivity(), count_title, count_content, "", "", "", "");
                            }

                        }
                    }).show();

            return true;
        }
    });
}

From source file:de.baumann.hhsmoodle.data_todo.Todo_Fragment.java

public void setTodoList() {

    if (isFABOpen) {
        closeFABMenu();/*from  w w w  . ja  va  2  s .com*/
    }

    NotificationManager nMgr = (NotificationManager) getActivity()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancelAll();

    //display data
    final int layoutstyle = R.layout.list_item_notes;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "todo_title", "todo_content", "todo_creation" };
    final Cursor row = db.fetchAllData(getActivity());
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String todo_title = row2.getString(row2.getColumnIndexOrThrow("todo_title"));
            final String todo_content = row2.getString(row2.getColumnIndexOrThrow("todo_content"));
            final String todo_icon = row2.getString(row2.getColumnIndexOrThrow("todo_icon"));
            final String todo_attachment = row2.getString(row2.getColumnIndexOrThrow("todo_attachment"));
            final String todo_creation = row2.getString(row2.getColumnIndexOrThrow("todo_creation"));

            View v = super.getView(position, convertView, parent);
            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);
            ImageView iv_attachment = (ImageView) v.findViewById(R.id.att_notes);
            helper_main.switchIcon(getActivity(), todo_icon, "todo_icon", iv_icon);

            switch (todo_attachment) {
            case "true":
                iv_attachment.setVisibility(View.VISIBLE);
                iv_attachment.setImageResource(R.drawable.alert_circle);
                break;
            default:
                iv_attachment.setVisibility(View.VISIBLE);
                iv_attachment.setImageResource(R.drawable.alert_circle_red);

                int n = Integer.valueOf(_id);

                android.content.Intent iMain = new android.content.Intent();
                iMain.setAction("shortcutToDo");
                iMain.setClassName(getActivity(), "de.baumann.hhsmoodle.activities.Activity_splash");
                PendingIntent piMain = PendingIntent.getActivity(getActivity(), n, iMain, 0);

                NotificationCompat.Builder builderSummary = new NotificationCompat.Builder(getActivity())
                        .setSmallIcon(R.drawable.school)
                        .setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary))
                        .setGroup("HHS_Moodle").setGroupSummary(true).setContentIntent(piMain);

                Notification notification = new NotificationCompat.Builder(getActivity())
                        .setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary))
                        .setSmallIcon(R.drawable.school).setContentTitle(todo_title)
                        .setContentText(todo_content).setContentIntent(piMain).setAutoCancel(true)
                        .setGroup("HHS_Moodle")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(todo_content))
                        .setPriority(Notification.PRIORITY_DEFAULT).setVibrate(new long[0]).build();

                NotificationManager notificationManager = (NotificationManager) getActivity()
                        .getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(n, notification);
                notificationManager.notify(0, builderSummary.build());
                break;
            }

            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    final helper_main.Item[] items = {
                            new helper_main.Item(getString(R.string.note_priority_0), R.drawable.circle_green),
                            new helper_main.Item(getString(R.string.note_priority_1), R.drawable.circle_yellow),
                            new helper_main.Item(getString(R.string.note_priority_2), R.drawable.circle_red), };

                    ListAdapter adapter = new ArrayAdapter<helper_main.Item>(getActivity(),
                            android.R.layout.select_dialog_item, android.R.id.text1, items) {
                        @NonNull
                        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                            //Use super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView) v.findViewById(android.R.id.text1);
                            tv.setTextSize(18);
                            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                    new AlertDialog.Builder(getActivity())
                            .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).setAdapter(adapter, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int item) {
                                    if (item == 0) {
                                        db.update(Integer.parseInt(_id), todo_title, todo_content, "3",
                                                todo_attachment, todo_creation);
                                        setTodoList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), todo_title, todo_content, "2",
                                                todo_attachment, todo_creation);
                                        setTodoList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), todo_title, todo_content, "1",
                                                todo_attachment, todo_creation);
                                        setTodoList();
                                    }
                                }
                            }).show();
                }
            });
            iv_attachment.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    switch (todo_attachment) {
                    case "true":
                        db.update(Integer.parseInt(_id), todo_title, todo_content, todo_icon, "",
                                todo_creation);
                        setTodoList();
                        break;
                    default:
                        db.update(Integer.parseInt(_id), todo_title, todo_content, todo_icon, "true",
                                todo_creation);
                        setTodoList();
                        break;
                    }
                }
            });
            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_todoBY", "note_title");
    sharedPref.edit().putString("filter_todoBY", "note_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String todo_title = row2.getString(row2.getColumnIndexOrThrow("todo_title"));
            final String todo_content = row2.getString(row2.getColumnIndexOrThrow("todo_content"));
            final String todo_icon = row2.getString(row2.getColumnIndexOrThrow("todo_icon"));
            final String todo_attachment = row2.getString(row2.getColumnIndexOrThrow("todo_attachment"));
            final String todo_creation = row2.getString(row2.getColumnIndexOrThrow("todo_creation"));

            sharedPref.edit().putString("toDo_title", todo_title).apply();
            sharedPref.edit().putString("toDo_text", todo_content).apply();
            sharedPref.edit().putString("toDo_seqno", _id).apply();
            sharedPref.edit().putString("toDo_icon", todo_icon).apply();
            sharedPref.edit().putString("toDo_create", todo_creation).apply();
            sharedPref.edit().putString("toDo_attachment", todo_attachment).apply();

            helper_main.switchToActivity(getActivity(), Activity_todo.class, false);
        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            if (isFABOpen) {
                closeFABMenu();
            }

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String todo_title = row2.getString(row2.getColumnIndexOrThrow("todo_title"));
            final String todo_content = row2.getString(row2.getColumnIndexOrThrow("todo_content"));
            final String todo_icon = row2.getString(row2.getColumnIndexOrThrow("todo_icon"));
            final String todo_attachment = row2.getString(row2.getColumnIndexOrThrow("todo_attachment"));
            final String todo_creation = row2.getString(row2.getColumnIndexOrThrow("todo_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark), getString(R.string.todo_share),
                    getString(R.string.bookmark_createNote), getString(R.string.count_create),
                    getString(R.string.bookmark_createEvent) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.number_edit_entry))) {

                                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                View dialogView = View.inflate(getActivity(), R.layout.dialog_edit_title, null);

                                final EditText edit_title = (EditText) dialogView.findViewById(R.id.pass_title);
                                edit_title.setHint(R.string.bookmark_edit_title);
                                edit_title.setText(todo_title);

                                builder.setView(dialogView);
                                builder.setTitle(R.string.bookmark_edit_title);
                                builder.setPositiveButton(R.string.toast_yes,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                                String inputTag = edit_title.getText().toString().trim();
                                                db.update(Integer.parseInt(_id), inputTag, todo_content,
                                                        todo_icon, todo_attachment, todo_creation);
                                                setTodoList();
                                                Snackbar.make(lv, R.string.bookmark_added,
                                                        Snackbar.LENGTH_SHORT).show();
                                            }
                                        });
                                builder.setNegativeButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                dialog.cancel();
                                            }
                                        });

                                final AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

                            if (options[item].equals(getString(R.string.todo_share))) {
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("text/plain");
                                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, todo_title);
                                sharingIntent.putExtra(Intent.EXTRA_TEXT, todo_content);
                                startActivity(Intent.createChooser(sharingIntent,
                                        (getString(R.string.note_share_2))));
                            }

                            if (options[item].equals(getString(R.string.bookmark_createEvent))) {
                                helper_main.createCalendarEvent(getActivity(), todo_title, todo_content);
                            }

                            if (options[item].equals(getString(R.string.bookmark_remove_bookmark))) {
                                Snackbar snackbar = Snackbar
                                        .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                db.delete(Integer.parseInt(_id));
                                                setTodoList();
                                            }
                                        });
                                snackbar.show();
                            }

                            if (options[item].equals(getString(R.string.bookmark_createNote))) {
                                Notes_helper.newNote(getActivity(), todo_title, todo_content, "", "", "", "");
                            }

                            if (options[item].equals(getString(R.string.count_create))) {
                                Count_helper.newCount(getActivity(), todo_title, todo_content,
                                        getActivity().getString(R.string.note_content), false);
                            }

                        }
                    }).show();

            return true;
        }
    });
}

From source file:de.baumann.hhsmoodle.data_bookmarks.Bookmarks_Fragment.java

public void setBookmarksList() {

    //display data
    final int layoutstyle = R.layout.list_item_notes;
    int[] xml_id = new int[] { R.id.textView_title_notes, R.id.textView_des_notes, R.id.textView_create_notes };
    String[] column = new String[] { "bookmarks_title", "bookmarks_content", "bookmarks_creation" };
    final Cursor row = db.fetchAllData(getActivity());
    adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override/*www.  j  a v a  2  s.  c  o m*/
        public View getView(final int position, View convertView, ViewGroup parent) {

            Cursor row = (Cursor) lv.getItemAtPosition(position);
            final String _id = row.getString(row.getColumnIndexOrThrow("_id"));
            final String bookmarks_title = row.getString(row.getColumnIndexOrThrow("bookmarks_title"));
            final String bookmarks_content = row.getString(row.getColumnIndexOrThrow("bookmarks_content"));
            final String bookmarks_icon = row.getString(row.getColumnIndexOrThrow("bookmarks_icon"));
            final String bookmarks_attachment = row
                    .getString(row.getColumnIndexOrThrow("bookmarks_attachment"));
            final String bookmarks_creation = row.getString(row.getColumnIndexOrThrow("bookmarks_creation"));

            View v = super.getView(position, convertView, parent);
            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);
            final ImageView iv_attachment = (ImageView) v.findViewById(R.id.att_notes);

            switch (bookmarks_icon) {
            case "01":
                iv_icon.setImageResource(R.drawable.circle_red);
                break;
            case "02":
                iv_icon.setImageResource(R.drawable.circle_yellow);
                break;
            case "03":
                iv_icon.setImageResource(R.drawable.circle_green);
                break;
            case "04":
                iv_icon.setImageResource(R.drawable.ic_school_grey600_48dp);
                break;
            case "05":
                iv_icon.setImageResource(R.drawable.ic_view_dashboard_grey600_48dp);
                break;
            case "06":
                iv_icon.setImageResource(R.drawable.ic_face_profile_grey600_48dp);
                break;
            case "07":
                iv_icon.setImageResource(R.drawable.ic_calendar_grey600_48dp);
                break;
            case "08":
                iv_icon.setImageResource(R.drawable.ic_chart_areaspline_grey600_48dp);
                break;
            case "09":
                iv_icon.setImageResource(R.drawable.ic_bell_grey600_48dp);
                break;
            case "10":
                iv_icon.setImageResource(R.drawable.ic_settings_grey600_48dp);
                break;
            case "11":
                iv_icon.setImageResource(R.drawable.ic_web_grey600_48dp);
                break;
            case "12":
                iv_icon.setImageResource(R.drawable.ic_magnify_grey600_48dp);
                break;
            case "13":
                iv_icon.setImageResource(R.drawable.ic_pencil_grey600_48dp);
                break;
            case "14":
                iv_icon.setImageResource(R.drawable.ic_check_grey600_48dp);
                break;
            case "15":
                iv_icon.setImageResource(R.drawable.ic_clock_grey600_48dp);
                break;
            case "16":
                iv_icon.setImageResource(R.drawable.ic_bookmark_grey600_48dp);
                break;
            }

            switch (bookmarks_attachment) {
            case "":
                iv_attachment.setVisibility(View.VISIBLE);
                iv_attachment.setImageResource(R.drawable.star_outline);
                break;
            default:
                iv_attachment.setVisibility(View.VISIBLE);
                iv_attachment.setImageResource(R.drawable.star_grey);
                break;
            }

            iv_attachment.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (bookmarks_attachment.equals("")) {

                        if (db.isExistFav("true")) {
                            Snackbar.make(lv, R.string.bookmark_setFav_not, Snackbar.LENGTH_LONG).show();
                        } else {
                            iv_attachment.setImageResource(R.drawable.star_grey);
                            db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content, bookmarks_icon,
                                    "true", bookmarks_creation);
                            setBookmarksList();
                            sharedPref.edit().putString("favoriteURL", bookmarks_content)
                                    .putString("favoriteTitle", bookmarks_title).apply();
                            Snackbar.make(lv, R.string.bookmark_setFav, Snackbar.LENGTH_LONG).show();
                        }
                    } else {
                        iv_attachment.setImageResource(R.drawable.star_outline);
                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content, bookmarks_icon, "",
                                bookmarks_creation);
                        setBookmarksList();
                    }
                }
            });

            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    final helper_main.Item[] items = {
                            new helper_main.Item(getString(R.string.note_priority_2), R.drawable.circle_red),
                            new helper_main.Item(getString(R.string.note_priority_1), R.drawable.circle_yellow),
                            new helper_main.Item(getString(R.string.note_priority_0), R.drawable.circle_green),
                            new helper_main.Item(getString(R.string.text_tit_11),
                                    R.drawable.ic_school_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_1),
                                    R.drawable.ic_view_dashboard_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_2),
                                    R.drawable.ic_face_profile_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_8),
                                    R.drawable.ic_calendar_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_3),
                                    R.drawable.ic_chart_areaspline_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_4),
                                    R.drawable.ic_bell_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_5),
                                    R.drawable.ic_settings_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_6),
                                    R.drawable.ic_web_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_7),
                                    R.drawable.ic_magnify_grey600_48dp),
                            new helper_main.Item(getString(R.string.title_notes),
                                    R.drawable.ic_pencil_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_9),
                                    R.drawable.ic_check_grey600_48dp),
                            new helper_main.Item(getString(R.string.text_tit_10),
                                    R.drawable.ic_clock_grey600_48dp),
                            new helper_main.Item(getString(R.string.title_bookmarks),
                                    R.drawable.ic_bookmark_grey600_48dp), };

                    ListAdapter adapter = new ArrayAdapter<helper_main.Item>(getActivity(),
                            android.R.layout.select_dialog_item, android.R.id.text1, items) {
                        @NonNull
                        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
                            //Use super class to create the View
                            View v = super.getView(position, convertView, parent);
                            TextView tv = (TextView) v.findViewById(android.R.id.text1);
                            tv.setTextSize(18);
                            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                            //Add margin between image and text (support various screen densities)
                            int dp5 = (int) (24 * getResources().getDisplayMetrics().density + 0.5f);
                            tv.setCompoundDrawablePadding(dp5);

                            return v;
                        }
                    };

                    new AlertDialog.Builder(getActivity())
                            .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).setAdapter(adapter, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int item) {
                                    if (item == 0) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "01", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "02", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "03", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 3) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "04", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 4) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "05", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 5) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "06", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 6) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "07", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 7) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "08", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 8) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "09", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 9) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "10", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 10) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "11", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 11) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "12", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 12) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "13", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 13) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "14", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 14) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "15", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    } else if (item == 15) {
                                        db.update(Integer.parseInt(_id), bookmarks_title, bookmarks_content,
                                                "16", bookmarks_attachment, bookmarks_creation);
                                        setBookmarksList();
                                    }
                                }
                            }).show();
                }
            });
            return v;
        }
    };

    //display data by filter
    final String note_search = sharedPref.getString("filter_bookmarksBY", "bookmarks_title");
    sharedPref.edit().putString("filter_bookmarksBY", "bookmarks_title").apply();
    filter.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s.toString());
        }
    });
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return db.fetchDataByFilter(constraint.toString(), note_search);
        }
    });

    lv.setAdapter(adapter);
    //onClick function
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String bookmarks_content = row2.getString(row2.getColumnIndexOrThrow("bookmarks_content"));
            sharedPref.edit().putString("load_next", "true").apply();
            sharedPref.edit().putString("loadURL", bookmarks_content).apply();

            ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
            viewPager.setCurrentItem(0);

        }
    });

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor row2 = (Cursor) lv.getItemAtPosition(position);
            final String _id = row2.getString(row2.getColumnIndexOrThrow("_id"));
            final String bookmarks_title = row2.getString(row2.getColumnIndexOrThrow("bookmarks_title"));
            final String bookmarks_content = row2.getString(row2.getColumnIndexOrThrow("bookmarks_content"));
            final String bookmarks_icon = row2.getString(row2.getColumnIndexOrThrow("bookmarks_icon"));
            final String bookmarks_attachment = row2
                    .getString(row2.getColumnIndexOrThrow("bookmarks_attachment"));
            final String bookmarks_creation = row2.getString(row2.getColumnIndexOrThrow("bookmarks_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark), getString(R.string.todo_menu),
                    getString(R.string.bookmark_createNote), getString(R.string.count_create),
                    getString(R.string.bookmark_createShortcut), getString(R.string.bookmark_createEvent) };
            new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.toast_cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    }).setItems(options, new DialogInterface.OnClickListener() {
                        @SuppressWarnings("ConstantConditions")
                        @Override
                        public void onClick(DialogInterface dialog, int item) {
                            if (options[item].equals(getString(R.string.number_edit_entry))) {

                                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                View dialogView = View.inflate(getActivity(), R.layout.dialog_edit_title, null);

                                final EditText edit_title = (EditText) dialogView.findViewById(R.id.pass_title);
                                edit_title.setHint(R.string.bookmark_edit_title);
                                edit_title.setText(bookmarks_title);

                                builder.setView(dialogView);
                                builder.setTitle(R.string.bookmark_edit_title);
                                builder.setPositiveButton(R.string.toast_yes,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                                String inputTag = edit_title.getText().toString().trim();
                                                db.update(Integer.parseInt(_id), inputTag, bookmarks_content,
                                                        bookmarks_icon, bookmarks_attachment,
                                                        bookmarks_creation);
                                                setBookmarksList();
                                                Snackbar.make(lv, R.string.bookmark_added,
                                                        Snackbar.LENGTH_SHORT).show();
                                            }
                                        });
                                builder.setNegativeButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                dialog.cancel();
                                            }
                                        });

                                final AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

                            if (options[item].equals(getString(R.string.todo_menu))) {
                                Todo_helper.newTodo(getActivity(), bookmarks_title, "", "");
                            }

                            if (options[item].equals(getString(R.string.count_create))) {
                                Count_helper.newCount(getActivity(), bookmarks_title, bookmarks_content,
                                        getActivity().getString(R.string.note_content), false);
                            }

                            if (options[item].equals(getString(R.string.bookmark_createEvent))) {
                                helper_main.createCalendarEvent(getActivity(), bookmarks_title,
                                        bookmarks_content);
                            }

                            if (options[item].equals(getString(R.string.bookmark_remove_bookmark))) {
                                Snackbar snackbar = Snackbar
                                        .make(lv, R.string.note_remove_confirmation, Snackbar.LENGTH_LONG)
                                        .setAction(R.string.toast_yes, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                db.delete(Integer.parseInt(_id));
                                                setBookmarksList();
                                            }
                                        });
                                snackbar.show();
                            }

                            if (options[item].equals(getString(R.string.bookmark_createNote))) {
                                Notes_helper.newNote(getActivity(), bookmarks_title, bookmarks_content, "", "",
                                        "", "");
                            }

                            if (options[item].equals(getString(R.string.bookmark_createShortcut))) {
                                Intent i = new Intent();
                                i.setAction(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(bookmarks_content));

                                Intent shortcut = new Intent();
                                shortcut.putExtra("android.intent.extra.shortcut.INTENT", i);
                                shortcut.putExtra("android.intent.extra.shortcut.NAME",
                                        "THE NAME OF SHORTCUT TO BE SHOWN");
                                shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, bookmarks_title);
                                shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                                        Intent.ShortcutIconResource.fromContext(
                                                getActivity().getApplicationContext(), R.mipmap.ic_launcher));
                                shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                                getActivity().sendBroadcast(shortcut);
                                Snackbar.make(lv, R.string.toast_shortcut, Snackbar.LENGTH_LONG).show();
                            }

                        }
                    }).show();

            return true;
        }
    });
}