Example usage for android.widget SimpleCursorAdapter SimpleCursorAdapter

List of usage examples for android.widget SimpleCursorAdapter SimpleCursorAdapter

Introduction

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

Prototype

public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) 

Source Link

Document

Standard constructor.

Usage

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

public void setTodoList() {

    if (isFABOpen) {
        closeFABMenu();/*from  ww  w .  j  a v a 2 s  .c  om*/
    }

    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_notes.Notes_Fragment.java

public void setNotesList() {

    if (isFABOpen) {
        closeFABMenu();/*from   ww w.  j  av  a  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_random.Random_Fragment.java

private void setRandomList() {

    if (isFABOpen) {
        closeFABMenu();//from   ww w. j  a va2s. 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[] { "random_title", "random_content", "random_creation" };
    final Cursor row = db.fetchAllData();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

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

            return v;
        }
    };

    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 random_content = row2.getString(row2.getColumnIndexOrThrow("random_content"));
            final String random_title = row2.getString(row2.getColumnIndexOrThrow("random_title"));

            if (random_content.isEmpty()) {
                Snackbar.make(lv, getActivity().getString(R.string.number_enterData), Snackbar.LENGTH_LONG)
                        .show();
            } else {
                getActivity().setTitle(random_title);
                lv.setVisibility(View.GONE);
                lvItems.setVisibility(View.VISIBLE);

                try {
                    FileOutputStream fOut = new FileOutputStream(newFile());
                    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                    myOutWriter.append(random_content);
                    myOutWriter.close();

                    fOut.flush();
                    fOut.close();
                } catch (IOException e) {
                    Log.e("Exception", "File write failed: " + e.toString());
                }

                items = new ArrayList<>();
                readItems();

                setAdapter(1000);

                fab.setVisibility(View.GONE);
                fab_dice.setVisibility(View.VISIBLE);
            }
        }
    });

    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 random_title = row2.getString(row2.getColumnIndexOrThrow("random_title"));
            final String random_content = row2.getString(row2.getColumnIndexOrThrow("random_content"));
            final String random_icon = row2.getString(row2.getColumnIndexOrThrow("random_icon"));
            final String random_attachment = row2.getString(row2.getColumnIndexOrThrow("random_attachment"));
            final String random_creation = row2.getString(row2.getColumnIndexOrThrow("random_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark) };
            new android.app.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))) {

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

                                final EditText edit_title = (EditText) dialogView
                                        .findViewById(R.id.note_title_input);
                                edit_title.setHint(R.string.title_hint);
                                edit_title.setText(random_title);

                                final EditText edit_cont = (EditText) dialogView
                                        .findViewById(R.id.note_text_input);
                                edit_cont.setHint(R.string.text_hint);
                                edit_cont.setText(random_content);

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

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                                String inputTitle = edit_title.getText().toString().trim();
                                                String inputCont = edit_cont.getText().toString().trim();
                                                db.update(Integer.parseInt(_id), inputTitle, inputCont,
                                                        random_icon, random_attachment, random_creation);
                                                setRandomList();
                                                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.app.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.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));
                                                setRandomList();
                                            }
                                        });
                                snackbar.show();
                            }

                        }
                    }).show();

            return true;
        }
    });
}

From source file:de.baumann.hhsmoodle.data_courses.Courses_Fragment.java

private void setCoursesList() {

    if (isFABOpen) {
        closeFABMenu();/*from   w ww .j  a v a  2  s . c  om*/
    }

    //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[] { "courses_title", "courses_content", "courses_creation" };
    final Cursor row = db.fetchAllData();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

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

            return v;
        }
    };

    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 courses_title = row2.getString(row2.getColumnIndexOrThrow("courses_title"));
            final String courses_content = row2.getString(row2.getColumnIndexOrThrow("courses_content"));

            final CharSequence[] options = { getString(R.string.courseList_todo),
                    getString(R.string.courseList_note), getString(R.string.courseList_random),
                    getString(R.string.courseList_subject), getString(R.string.bookmark_createEvent) };

            new android.app.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.courseList_random))) {

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

                                final EditText edit_title = (EditText) dialogView
                                        .findViewById(R.id.note_title_input);
                                edit_title.setHint(R.string.title_hint);
                                edit_title.setText(courses_title);

                                final EditText edit_cont = (EditText) dialogView
                                        .findViewById(R.id.note_text_input);
                                edit_cont.setHint(R.string.text_hint);
                                edit_cont.setText(courses_content);

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

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

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

                                final android.app.AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();

                                dialog2.getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE)
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //Do stuff, possibly set wantToCloseDialog to true then...
                                                Random_DbAdapter db = new Random_DbAdapter(getActivity());
                                                db.open();

                                                String inputTitle = edit_title.getText().toString().trim();
                                                String inputCont = edit_cont.getText().toString().trim();

                                                if (db.isExist(inputTitle)) {
                                                    Snackbar.make(edit_title,
                                                            getString(R.string.toast_newTitle),
                                                            Snackbar.LENGTH_LONG).show();
                                                } else {
                                                    db.insert(inputTitle, inputCont, "", "",
                                                            helper_main.createDate());
                                                    dialog2.dismiss();
                                                }
                                            }
                                        });
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

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

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

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

                                LayoutInflater inflater = getActivity().getLayoutInflater();

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

                                titleInput = (EditText) dialogView.findViewById(R.id.subject_title_);
                                titleInput.setSelection(titleInput.getText().length());
                                titleInput.setText("");
                                teacherInput = (EditText) dialogView.findViewById(R.id.subject_teacher);
                                teacherInput.setText(courses_title);
                                roomInput = (EditText) dialogView.findViewById(R.id.subject_room);
                                roomInput.setText("");
                                helper_main.showKeyboard(getActivity(), titleInput);

                                final ImageButton be = (ImageButton) dialogView
                                        .findViewById(R.id.imageButtonPri);
                                assert be != null;
                                be.setImageResource(R.drawable.circle_grey);
                                sharedPref.edit().putString("subject_color", "11").apply();

                                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("subject_color", "1")
                                                                    .apply();
                                                        } else if (item == 1) {
                                                            be.setImageResource(R.drawable.circle_pink);
                                                            sharedPref.edit().putString("subject_color", "2")
                                                                    .apply();
                                                        } else if (item == 2) {
                                                            be.setImageResource(R.drawable.circle_purple);
                                                            sharedPref.edit().putString("subject_color", "3")
                                                                    .apply();
                                                        } else if (item == 3) {
                                                            be.setImageResource(R.drawable.circle_blue);
                                                            sharedPref.edit().putString("subject_color", "4")
                                                                    .apply();
                                                        } else if (item == 4) {
                                                            be.setImageResource(R.drawable.circle_teal);
                                                            sharedPref.edit().putString("subject_color", "5")
                                                                    .apply();
                                                        } else if (item == 5) {
                                                            be.setImageResource(R.drawable.circle_green);
                                                            sharedPref.edit().putString("subject_color", "6")
                                                                    .apply();
                                                        } else if (item == 6) {
                                                            be.setImageResource(R.drawable.circle_lime);
                                                            sharedPref.edit().putString("subject_color", "7")
                                                                    .apply();
                                                        } else if (item == 7) {
                                                            be.setImageResource(R.drawable.circle_yellow);
                                                            sharedPref.edit().putString("subject_color", "8")
                                                                    .apply();
                                                        } else if (item == 8) {
                                                            be.setImageResource(R.drawable.circle_orange);
                                                            sharedPref.edit().putString("subject_color", "9")
                                                                    .apply();
                                                        } else if (item == 9) {
                                                            be.setImageResource(R.drawable.circle_brown);
                                                            sharedPref.edit().putString("subject_color", "10")
                                                                    .apply();
                                                        } else if (item == 10) {
                                                            be.setImageResource(R.drawable.circle_grey);
                                                            sharedPref.edit().putString("subject_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) {
                                                Subject_DbAdapter db = new Subject_DbAdapter(getActivity());
                                                db.open();

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

                                                Date date = new Date();
                                                DateFormat dateFormat = new SimpleDateFormat(
                                                        "yy-MM-dd_HH-mm-ss", Locale.getDefault());
                                                String creation = dateFormat.format(date);

                                                if (db.isExist(inputTitle)) {
                                                    Snackbar.make(titleInput,
                                                            getString(R.string.toast_newTitle),
                                                            Snackbar.LENGTH_LONG).show();
                                                } else {
                                                    db.insert(inputTitle, inputTeacher,
                                                            sharedPref.getString("subject_color", ""),
                                                            inputRoom, creation);
                                                    dialog.dismiss();
                                                }

                                            }
                                        });
                                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();
                            }

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

                        }
                    }).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 courses_title = row2.getString(row2.getColumnIndexOrThrow("courses_title"));
            final String courses_content = row2.getString(row2.getColumnIndexOrThrow("courses_content"));
            final String courses_icon = row2.getString(row2.getColumnIndexOrThrow("courses_icon"));
            final String courses_attachment = row2.getString(row2.getColumnIndexOrThrow("courses_attachment"));
            final String courses_creation = row2.getString(row2.getColumnIndexOrThrow("courses_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.bookmark_remove_bookmark) };
            new android.app.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))) {

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

                                final EditText edit_title = (EditText) dialogView
                                        .findViewById(R.id.note_title_input);
                                edit_title.setHint(R.string.title_hint);
                                edit_title.setText(courses_title);

                                final EditText edit_cont = (EditText) dialogView
                                        .findViewById(R.id.note_text_input);
                                edit_cont.setHint(R.string.text_hint);
                                edit_cont.setText(courses_content);

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

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                                String inputTitle = edit_title.getText().toString().trim();
                                                String inputCont = edit_cont.getText().toString().trim();
                                                db.update(Integer.parseInt(_id), inputTitle, inputCont,
                                                        courses_icon, courses_attachment, courses_creation);
                                                setCoursesList();
                                                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.app.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.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));
                                                setCoursesList();
                                            }
                                        });
                                snackbar.show();
                            }

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

From source file:com.sage42.androidappaddicts.app.main.MainActivity.java

/**
 * Dummy code for searchview suggestion list.
 * // w ww  . jav a2 s  .  c  o m
 * @return
 */
@SuppressWarnings("resource")
public SimpleCursorAdapter getData() {

    final String[] columnNames = { "_id", "suggestion_applist_title" }; //$NON-NLS-1$//$NON-NLS-2$
    final MatrixCursor cursor = new MatrixCursor(columnNames);

    final String[] temp = new String[2];
    int id = 0;
    for (final String item : this.mArray) {
        temp[0] = Integer.toString(id++);
        temp[1] = item;
        cursor.addRow(temp);
    }
    final String[] from = { "suggestion_applist_title" }; //$NON-NLS-1$
    final int[] to = { R.id.suggestion_applist_title };
    return new SimpleCursorAdapter(this, R.layout.suggestion_applist_item, cursor, from, to, 1);

}

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  om*/
    }

    //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:com.forrestguice.suntimeswidget.LocationConfigView.java

/**
 *
 * @param context a context used to access resources
 *///from   w  w  w  .ja  v a2 s .c o  m
protected void initViews(Context context) {
    //Log.d("DEBUG", "LocationConfigView initViews");
    WidgetSettings.initDisplayStrings(context);

    flipper = (ViewFlipper) findViewById(R.id.view_flip);
    flipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in));
    flipper.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));

    flipper2 = (ViewFlipper) findViewById(R.id.view_flip2);
    flipper2.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in));
    flipper2.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));

    ArrayAdapter<WidgetSettings.LocationMode> spinner_locationModeAdapter = new LocationModeAdapter(myParent,
            WidgetSettings.LocationMode.values());
    spinner_locationModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner_locationMode = (Spinner) findViewById(R.id.appwidget_location_mode);
    spinner_locationMode.setAdapter(spinner_locationModeAdapter);
    spinner_locationMode.setOnItemSelectedListener(onLocationModeSelected);

    layout_locationName = (LinearLayout) findViewById(R.id.appwidget_location_name_layout);
    labl_locationName = (TextView) findViewById(R.id.appwidget_location_name_label);
    text_locationName = (EditText) findViewById(R.id.appwidget_location_name);

    String[] from = new String[] { "name" };
    int[] to = new int[] { android.R.id.text1 };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        getFixAdapter = new SimpleCursorAdapter(myParent, R.layout.layout_listitem_locations, null, from, to,
                0);
    else
        getFixAdapter = new SimpleCursorAdapter(myParent, R.layout.layout_listitem_locations, null, from, to);

    getFixAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spin_locationName = (Spinner) findViewById(R.id.appwidget_location_nameSelect);
    spin_locationName.setAdapter(getFixAdapter);
    spin_locationName.setOnItemSelectedListener(onCustomLocationSelected);

    labl_locationLat = (TextView) findViewById(R.id.appwidget_location_lat_label);
    text_locationLat = (EditText) findViewById(R.id.appwidget_location_lat);

    inputOverlay = findViewById(R.id.appwidget_location_latlon_overlay);
    inputOverlay.setVisibility(View.GONE);
    inputOverlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mode == LocationViewMode.MODE_CUSTOM_SELECT) {
                setMode(LocationViewMode.MODE_CUSTOM_EDIT);
            }
        }
    });

    labl_locationLon = (TextView) findViewById(R.id.appwidget_location_lon_label);
    text_locationLon = (EditText) findViewById(R.id.appwidget_location_lon);

    // custom mode: toggle edit mode
    button_edit = (ImageButton) findViewById(R.id.appwidget_location_edit);
    button_edit.setOnClickListener(onEditButtonClicked);

    // custom mode: save location
    button_save = (ImageButton) findViewById(R.id.appwidget_location_save);
    button_save.setOnClickListener(onSaveButtonClicked);

    // custom mode: get GPS fix
    progress_getfix = (ProgressBar) findViewById(R.id.appwidget_location_getfixprogress);
    progress_getfix.setVisibility(View.GONE);

    button_getfix = (ImageButton) findViewById(R.id.appwidget_location_getfix);
    getFixUI_editMode = new GetFixUI1(text_locationName, text_locationLat, text_locationLon, progress_getfix,
            button_getfix);

    button_getfix.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFixHelper.getFix(0);
        }
    });

    // auto mode: get GPS fix
    progress_auto = (ProgressBar) findViewById(R.id.appwidget_location_auto_progress);
    progress_auto.setVisibility(View.GONE);

    button_auto = (ImageButton) findViewById(R.id.appwidget_location_auto);
    getFixUI_autoMode = new GetFixUI2(text_locationName, text_locationLat, text_locationLon, progress_auto,
            button_auto);
    button_auto.setOnClickListener(onAutoButtonClicked);

    getFixHelper = new GetFixHelper(myParent, getFixUI_editMode); // 0; getFixUI_editMode
    getFixHelper.addUI(getFixUI_autoMode); // 1; getFixUI_autoMode
    updateGPSButtonIcons();
}

From source file:cz.maresmar.sfm.view.credential.CredentialDetailFragment.java

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
    case CREDENTIAL_LOADER_ID:
        Timber.d("Credential data loaded");

        cursor.moveToFirst();//w w w.  j av a  2s.c  o m
        if (BuildConfig.DEBUG) {
            Assert.isOne(cursor.getCount());
        }

        if (mPortalUri == null) {
            mPortalGroupId = cursor.getLong(4);

            String selection = ProviderContract.Portal.PORTAL_GROUP_ID + " = " + mPortalGroupId;
            Uri uri = ProviderContract.Portal.getUri();

            Bundle args = new Bundle();
            args.putParcelable(ARG_QUERY_URI, uri);
            args.putString(ARG_QUERY_SELECTION, selection);

            getLoaderManager().initLoader(PORTAL_LOADER_ID, args, this);
        }

        if (mLoadDataFromDb) {
            // Do not override changes on screen rotation
            mNameText.setText(cursor.getString(0));
            mPasswordText.setText(cursor.getString(1));
            long credentialGroupId = cursor.getLong(2);
            setExtraData(cursor.getString(3));

            @ProviderContract.CredentialFlags
            int flags = cursor.getInt(5);
            boolean increaseNotification = !((flags
                    & ProviderContract.CREDENTIAL_FLAG_DISABLE_CREDIT_INCREASE_NOTIFICATION) == ProviderContract.CREDENTIAL_FLAG_DISABLE_CREDIT_INCREASE_NOTIFICATION);
            mCreditIncreaseNotificationCheckBox.setChecked(increaseNotification);
            boolean lowCreditNotification = !((flags
                    & ProviderContract.CREDENTIAL_FLAG_DISABLE_LOW_CREDIT_NOTIFICATION) == ProviderContract.CREDENTIAL_FLAG_DISABLE_LOW_CREDIT_NOTIFICATION);
            mLowCreditNotificationCheckBox.setChecked(lowCreditNotification);
            mLowCreditText.setText(String.valueOf(cursor.getInt(6)));

            // Set credential group Id
            AsyncTask.execute(() -> {
                try {
                    blockingLoaders.await();
                } catch (InterruptedException e) {
                    Timber.e(e);
                }
                getActivity().runOnUiThread(() -> setCredentialGroupId(credentialGroupId));
            });
            mLoadDataFromDb = false;
        }
        break;
    case CREDENTIAL_GROUPS_LOADER_ID:
        Timber.d("Credential group data loaded");

        mCredentialGroupAdapter = new SimpleCursorAdapter(getContext(),
                R.layout.support_simple_spinner_dropdown_item, cursor,
                new String[] { ProviderContract.CredentialsGroup.NAME }, new int[] { android.R.id.text1 }, 0);
        mCredentialGroupSpinner.setAdapter(mCredentialGroupAdapter);
        blockingLoaders.countDown();
        break;
    case PORTAL_LOADER_ID:
        Timber.d("Portal data loaded");

        cursor.moveToFirst();
        if (BuildConfig.DEBUG) {
            Assert.that(cursor.getCount() > 0, "You should have at least one portal," + "but has %d",
                    cursor.getCount());
        }

        String pluginId = cursor.getString(0);
        requestExtraFormat(pluginId);
        if (mLastPlugin != null && !mLastPlugin.equals(pluginId)) {
            setExtraData(null);
        }
        mLastPlugin = pluginId;

        mPortalGroupId = cursor.getLong(1);
        break;
    default:
        throw new UnsupportedOperationException("Unknown loader id: " + loader.getId());
    }
}

From source file:de.baumann.hhsmoodle.data_subjects.Subjects_Fragment.java

private void setSubjectsList() {

    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), };

    //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 };
    String[] column = new String[] { "subject_title", "subject_content", "subject_attachment" };
    final Cursor row = db.fetchAllData();
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), layoutstyle, row, column, xml_id, 0) {
        @Override//w w  w .j av  a  2s  .  c  om
        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 subject_title = row.getString(row.getColumnIndexOrThrow("subject_title"));
            final String subject_content = row.getString(row.getColumnIndexOrThrow("subject_content"));
            final String subject_icon = row.getString(row.getColumnIndexOrThrow("subject_icon"));
            final String subject_attachment = row.getString(row.getColumnIndexOrThrow("subject_attachment"));
            final String subject_creation = row.getString(row.getColumnIndexOrThrow("subject_creation"));

            View v = super.getView(position, convertView, parent);
            ImageView iv_icon = (ImageView) v.findViewById(R.id.icon_notes);
            Subjects_helper.switchIcon(getActivity(), subject_icon, "subject_color", iv_icon);
            iv_icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    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), subject_title, subject_content, "1",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 1) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "2",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 2) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "3",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 3) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "4",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 4) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "5",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 5) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "6",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 6) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "7",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 7) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "8",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 8) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "9",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 9) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "10",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    } else if (item == 10) {
                                        db.update(Integer.parseInt(_id), subject_title, subject_content, "11",
                                                subject_attachment, subject_creation);
                                        setSubjectsList();
                                    }
                                }
                            }).show();
                }
            });

            return v;
        }
    };

    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 subject_title = row2.getString(row2.getColumnIndexOrThrow("subject_title"));
            final String subject_content = row2.getString(row2.getColumnIndexOrThrow("subject_content"));

            final CharSequence[] options = { getString(R.string.courseList_todo),
                    getString(R.string.courseList_note), getString(R.string.courseList_random),
                    getString(R.string.courseList_course), 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.courseList_random))) {

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

                                final EditText edit_title = (EditText) dialogView
                                        .findViewById(R.id.note_title_input);
                                edit_title.setHint(R.string.title_hint);
                                edit_title.setText(subject_title);

                                final EditText edit_cont = (EditText) dialogView
                                        .findViewById(R.id.note_text_input);
                                edit_cont.setHint(R.string.text_hint);

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

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                            }
                                        });
                                builder.setNegativeButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

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

                                final android.app.AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();

                                dialog2.getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE)
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //Do stuff, possibly set wantToCloseDialog to true then...
                                                Random_DbAdapter db = new Random_DbAdapter(getActivity());
                                                db.open();

                                                if (db.isExist(subject_title)) {
                                                    Snackbar.make(edit_title,
                                                            getString(R.string.toast_newTitle),
                                                            Snackbar.LENGTH_LONG).show();
                                                } else {
                                                    String inputTitle = edit_title.getText().toString().trim();
                                                    String inputCont = edit_cont.getText().toString().trim();
                                                    db.insert(inputTitle, inputCont, "", "",
                                                            helper_main.createDate());
                                                    dialog2.cancel();
                                                }
                                            }
                                        });
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

                            if (options[item].equals(getString(R.string.courseList_note))) {
                                Notes_helper.newNote(getActivity(), subject_title, subject_content, "", "", "",
                                        "");
                            }

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

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

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

                                final EditText edit_title = (EditText) dialogView
                                        .findViewById(R.id.note_title_input);
                                edit_title.setHint(R.string.title_hint);
                                edit_title.setText(subject_title);

                                final EditText edit_cont = (EditText) dialogView
                                        .findViewById(R.id.note_text_input);
                                edit_cont.setHint(R.string.text_hint);

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

                                            public void onClick(DialogInterface dialog, int whichButton) {

                                            }
                                        });
                                builder.setNegativeButton(R.string.toast_cancel,
                                        new DialogInterface.OnClickListener() {

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

                                final android.app.AlertDialog dialog2 = builder.create();
                                // Display the custom alert dialog on interface
                                dialog2.show();

                                dialog2.getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE)
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                //Do stuff, possibly set wantToCloseDialog to true then...
                                                Courses_DbAdapter db = new Courses_DbAdapter(getActivity());
                                                db.open();

                                                if (db.isExist(subject_title)) {
                                                    Snackbar.make(edit_title,
                                                            getString(R.string.toast_newTitle),
                                                            Snackbar.LENGTH_LONG).show();
                                                } else {
                                                    String inputTitle = edit_title.getText().toString().trim();
                                                    String inputCont = edit_cont.getText().toString().trim();
                                                    db.insert(inputTitle, inputCont, "", "",
                                                            helper_main.createDate());
                                                    dialog2.cancel();
                                                }
                                            }
                                        });
                                helper_main.showKeyboard(getActivity(), edit_title);
                            }

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

                        }
                    }).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 subject_title = row2.getString(row2.getColumnIndexOrThrow("subject_title"));
            final String subject_content = row2.getString(row2.getColumnIndexOrThrow("subject_content"));
            final String subject_icon = row2.getString(row2.getColumnIndexOrThrow("subject_icon"));
            final String subject_attachment = row2.getString(row2.getColumnIndexOrThrow("subject_attachment"));
            final String subject_creation = row2.getString(row2.getColumnIndexOrThrow("subject_creation"));

            final CharSequence[] options = { getString(R.string.number_edit_entry),
                    getString(R.string.subjects_copy), getString(R.string.bookmark_remove_bookmark) };
            new android.app.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))) {

                                LayoutInflater inflater = getActivity().getLayoutInflater();

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

                                titleInput = (EditText) dialogView.findViewById(R.id.subject_title_);
                                titleInput.setSelection(titleInput.getText().length());
                                titleInput.setText(subject_title);
                                teacherInput = (EditText) dialogView.findViewById(R.id.subject_teacher);
                                teacherInput.setText(subject_content);
                                roomInput = (EditText) dialogView.findViewById(R.id.subject_room);
                                roomInput.setText(subject_attachment);

                                helper_main.showKeyboard(getActivity(), titleInput);

                                final ImageButton be = (ImageButton) dialogView
                                        .findViewById(R.id.imageButtonPri);
                                assert be != null;
                                Subjects_helper.switchIcon(getActivity(), subject_icon, "subject_color", be);

                                be.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {

                                        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("subject_color", "1")
                                                                    .apply();
                                                        } else if (item == 1) {
                                                            be.setImageResource(R.drawable.circle_pink);
                                                            sharedPref.edit().putString("subject_color", "2")
                                                                    .apply();
                                                        } else if (item == 2) {
                                                            be.setImageResource(R.drawable.circle_purple);
                                                            sharedPref.edit().putString("subject_color", "3")
                                                                    .apply();
                                                        } else if (item == 3) {
                                                            be.setImageResource(R.drawable.circle_blue);
                                                            sharedPref.edit().putString("subject_color", "4")
                                                                    .apply();
                                                        } else if (item == 4) {
                                                            be.setImageResource(R.drawable.circle_teal);
                                                            sharedPref.edit().putString("subject_color", "5")
                                                                    .apply();
                                                        } else if (item == 5) {
                                                            be.setImageResource(R.drawable.circle_green);
                                                            sharedPref.edit().putString("subject_color", "6")
                                                                    .apply();
                                                        } else if (item == 6) {
                                                            be.setImageResource(R.drawable.circle_lime);
                                                            sharedPref.edit().putString("subject_color", "7")
                                                                    .apply();
                                                        } else if (item == 7) {
                                                            be.setImageResource(R.drawable.circle_yellow);
                                                            sharedPref.edit().putString("subject_color", "8")
                                                                    .apply();
                                                        } else if (item == 8) {
                                                            be.setImageResource(R.drawable.circle_orange);
                                                            sharedPref.edit().putString("subject_color", "9")
                                                                    .apply();
                                                        } else if (item == 9) {
                                                            be.setImageResource(R.drawable.circle_brown);
                                                            sharedPref.edit().putString("subject_color", "10")
                                                                    .apply();
                                                        } else if (item == 10) {
                                                            be.setImageResource(R.drawable.circle_grey);
                                                            sharedPref.edit().putString("subject_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) {
                                                Subject_DbAdapter db = new Subject_DbAdapter(getActivity());
                                                db.open();

                                                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("subject_color", ""), inputRoom,
                                                        subject_creation);
                                                dialog.dismiss();
                                                setSubjectsList();
                                                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();
                            }

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

                                LayoutInflater inflater = getActivity().getLayoutInflater();

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

                                titleInput = (EditText) dialogView.findViewById(R.id.subject_title_);
                                titleInput.setSelection(titleInput.getText().length());
                                titleInput.setText(subject_title);
                                teacherInput = (EditText) dialogView.findViewById(R.id.subject_teacher);
                                teacherInput.setText(subject_content);
                                roomInput = (EditText) dialogView.findViewById(R.id.subject_room);
                                roomInput.setText(subject_attachment);

                                helper_main.showKeyboard(getActivity(), titleInput);

                                final ImageButton be = (ImageButton) dialogView
                                        .findViewById(R.id.imageButtonPri);
                                Subjects_helper.switchIcon(getActivity(), subject_icon, "subject_color", be);

                                be.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {

                                        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("subject_color", "1")
                                                                    .apply();
                                                        } else if (item == 1) {
                                                            be.setImageResource(R.drawable.circle_pink);
                                                            sharedPref.edit().putString("subject_color", "2")
                                                                    .apply();
                                                        } else if (item == 2) {
                                                            be.setImageResource(R.drawable.circle_purple);
                                                            sharedPref.edit().putString("subject_color", "3")
                                                                    .apply();
                                                        } else if (item == 3) {
                                                            be.setImageResource(R.drawable.circle_blue);
                                                            sharedPref.edit().putString("subject_color", "4")
                                                                    .apply();
                                                        } else if (item == 4) {
                                                            be.setImageResource(R.drawable.circle_teal);
                                                            sharedPref.edit().putString("subject_color", "5")
                                                                    .apply();
                                                        } else if (item == 5) {
                                                            be.setImageResource(R.drawable.circle_green);
                                                            sharedPref.edit().putString("subject_color", "6")
                                                                    .apply();
                                                        } else if (item == 6) {
                                                            be.setImageResource(R.drawable.circle_lime);
                                                            sharedPref.edit().putString("subject_color", "7")
                                                                    .apply();
                                                        } else if (item == 7) {
                                                            be.setImageResource(R.drawable.circle_yellow);
                                                            sharedPref.edit().putString("subject_color", "8")
                                                                    .apply();
                                                        } else if (item == 8) {
                                                            be.setImageResource(R.drawable.circle_orange);
                                                            sharedPref.edit().putString("subject_color", "9")
                                                                    .apply();
                                                        } else if (item == 9) {
                                                            be.setImageResource(R.drawable.circle_brown);
                                                            sharedPref.edit().putString("subject_color", "10")
                                                                    .apply();
                                                        } else if (item == 10) {
                                                            be.setImageResource(R.drawable.circle_grey);
                                                            sharedPref.edit().putString("subject_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) {
                                                Subject_DbAdapter db = new Subject_DbAdapter(getActivity());
                                                db.open();

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

                                                Date date = new Date();
                                                DateFormat dateFormat = new SimpleDateFormat(
                                                        "yy-MM-dd_HH-mm-ss", Locale.getDefault());
                                                String creation = dateFormat.format(date);

                                                if (db.isExist(creation)) {
                                                    Snackbar.make(titleInput,
                                                            getString(R.string.toast_newTitle),
                                                            Snackbar.LENGTH_LONG).show();
                                                } else {
                                                    db.insert(inputTitle, inputTeacher,
                                                            sharedPref.getString("subject_color", creation),
                                                            inputRoom, helper_main.createDate());
                                                    dialog.dismiss();
                                                    setSubjectsList();
                                                }

                                            }
                                        });
                                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();
                            }

                            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));
                                                setSubjectsList();
                                            }
                                        });
                                snackbar.show();
                            }

                        }
                    }).show();

            return true;
        }
    });
}

From source file:com.android.mms.quickmessage.QuickMessagePopup.java

/**
 * This displays the Templates selection dialog
 *///from www .j  ava2 s.  co  m
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    switch (id) {
    case DIALOG_TEMPLATE_NOT_AVAILABLE:
        builder.setTitle(R.string.template_not_present_error_title);
        builder.setMessage(R.string.template_not_present_error);
        return builder.create();

    case DIALOG_TEMPLATE_SELECT:
        builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.template_select);
        mTemplatesCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
                new String[] { Template.TEXT }, new int[] { android.R.id.text1 },
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        builder.setAdapter(mTemplatesCursorAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Get the selected template and text
                Cursor c = (Cursor) mTemplatesCursorAdapter.getItem(which);
                String text = c.getString(c.getColumnIndex(Template.TEXT));

                // Get the currently visible message and append text
                QuickMessage qm = mMessageList.get(mCurrentPage);
                if (qm != null) {
                    // insert the template text at the cursor location or replace selected
                    int start = qm.getEditText().getSelectionStart();
                    int end = qm.getEditText().getSelectionEnd();
                    qm.getEditText().getText().replace(Math.min(start, end), Math.max(start, end), text);
                }
            }
        });
        return builder.create();
    }
    return super.onCreateDialog(id, args);
}