Example usage for android.widget AdapterView.OnItemClickListener AdapterView.OnItemClickListener

List of usage examples for android.widget AdapterView.OnItemClickListener AdapterView.OnItemClickListener

Introduction

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

Prototype

AdapterView.OnItemClickListener

Source Link

Usage

From source file:org.gnucash.android.ui.transaction.TransactionFormFragment.java

/**
 * Initializes the transaction name field for autocompletion with existing transaction names in the database
 *///from   w  w  w. j  a  v  a  2 s  .  co  m
private void initTransactionNameAutocomplete() {
    final int[] to = new int[] { android.R.id.text1 };
    final String[] from = new String[] { DatabaseHelper.KEY_NAME };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_dropdown_item_1line, null, from, to, 0);

    adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        @Override
        public CharSequence convertToString(Cursor cursor) {
            final int colIndex = cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_NAME);
            return cursor.getString(colIndex);
        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence name) {
            return mTransactionsDbAdapter.fetchTransactionsStartingWith(name.toString());
        }
    });

    mNameEditText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            mTransaction = mTransactionsDbAdapter.getTransaction(id);
            mTransaction.setUID(UUID.randomUUID().toString());
            mTransaction.setExported(false);
            mTransaction.setTime(System.currentTimeMillis());
            long accountId = ((TransactionsActivity) getSherlockActivity()).getCurrentAccountID();
            mTransaction.setAccountUID(mTransactionsDbAdapter.getAccountUID(accountId));
            initializeViewsWithTransaction();
        }
    });

    mNameEditText.setAdapter(adapter);
}

From source file:dess15proj5.fau.cs.osr_amos.mobiletimerecording.ui.SelectedProjectFragment.java

/**
 * This method is used to set a click listener to the list view.
 *
 * methodtype set method/*from  w w w  .  j  a  v  a  2s  . c om*/
 */
private void setClickListenerToListView() {
    sessionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (firstTimeStartup) {
                selectedView = view;
                firstTimeStartup = false;
            }
            adapter.setSelectedItemPosition(i);
            setPreviousBtnInvisible();
            SessionArrayAdapter.ViewHolder viewHolder = (SessionArrayAdapter.ViewHolder) view.getTag();
            viewHolder.deleteSessionBtn.setVisibility(View.VISIBLE);
            selectedView = view;
            selectedPosition = i;
        }
    });
}

From source file:mroza.forms.ChooseProgramActivity.java

private void handleListViewBehavior(final List<ChildTable> childTables, Term term) {
    final ListView childTableList = (ListView) findViewById(R.id.childTable_list);
    ChildTableListViewAdapter adapter = new ChildTableListViewAdapter(this, childTables, term);
    childTableList.setAdapter(adapter);//from ww w  . jav a2  s  .  co  m

    childTableList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(ChooseProgramActivity.this, ProgramActivity.class);
            ChildTable childTable = childTables.get(position);
            intent.putExtra("CHILD_TABLE_ID", childTable.getId());
            intent.putExtra("CHILD_ID", child.getId());
            ChooseProgramActivity.this.startActivity(intent);
        }
    });

}

From source file:com.juick.android.UserCenterActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    JuickAdvancedApplication.setupTheme(this);
    handler = new Handler();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_center);
    final ListView list = (ListView) findViewById(R.id.list);
    final View listWait = findViewById(R.id.list_wait);
    final TextView userRealName = (TextView) findViewById(R.id.user_realname);
    final ImageView userPic = (ImageView) findViewById(R.id.userpic);
    final TextView userName = (TextView) findViewById(R.id.username);
    search = findViewById(R.id.search);//from www  .j a  v a2 s .  co m
    final View stats = findViewById(R.id.stats);
    userRealName.setText("...");
    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        finish();
        return;
    }
    uname = extras.getString("uname");
    final int uid = extras.getInt("uid");

    final MessageID mid = (MessageID) extras.getSerializable("mid");
    final MessagesSource messagesSource = (MessagesSource) extras.getSerializable("messagesSource");
    if (uname == null || mid == null) {
        finish();
        return;
    }
    int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
    final int userpicSize = height <= 320 ? 32 : 96;
    float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
    userPic.setMinimumHeight((int) (scaledDensity * userpicSize));
    userPic.setMinimumWidth((int) (scaledDensity * userpicSize));
    stats.setEnabled(false);
    userName.setText("@" + uname);
    final boolean russian = Locale.getDefault().getLanguage().equals("ru");
    new Thread() {
        @Override
        public void run() {
            final Utils.RESTResponse json = Utils.getJSON(UserCenterActivity.this,
                    "http://" + Utils.JA_ADDRESS + "/api/userinfo?uname=" + Uri.encode(uname), null);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    stats.setEnabled(true);
                    if (json.getErrorText() != null) {
                        Toast.makeText(UserCenterActivity.this, "JA server: " + json.getErrorText(),
                                Toast.LENGTH_LONG).show();
                        listWait.setVisibility(View.GONE);
                    } else {
                        final UserInfo userInfo = new Gson().fromJson(json.getResult(), UserInfo.class);
                        if (userInfo == null) {
                            Toast.makeText(UserCenterActivity.this, "Unable to parse JSON", Toast.LENGTH_LONG)
                                    .show();
                            listWait.setVisibility(View.GONE);
                        } else {
                            userRealName.setText(userInfo.fullName);
                            listWait.setVisibility(View.GONE);
                            list.setVisibility(View.VISIBLE);
                            list.setAdapter(new BaseAdapter() {
                                @Override
                                public int getCount() {
                                    return userInfo.getExtraInfo().size();
                                }

                                @Override
                                public Object getItem(int position) {
                                    return userInfo.getExtraInfo().get(position);
                                }

                                @Override
                                public long getItemId(int position) {
                                    return position;
                                }

                                @Override
                                public View getView(int position, View convertView, ViewGroup parent) {
                                    if (convertView == null) {
                                        convertView = getLayoutInflater().inflate(R.layout.listitem_userinfo,
                                                null);
                                    }
                                    TextView text = (TextView) convertView.findViewById(R.id.text);
                                    TextView text2 = (TextView) convertView.findViewById(R.id.text2);
                                    String info = userInfo.getExtraInfo().get(position);
                                    int ix = info.indexOf("|");
                                    if (ix == -1) {
                                        text.setText(info);
                                        if (russian && UserInfo.translations.containsKey(info)) {
                                            info = UserInfo.translations.get(info);
                                        }
                                        text2.setText("");
                                    } else {
                                        String theInfo = info.substring(0, ix);
                                        if (russian && UserInfo.translations.containsKey(theInfo)) {
                                            theInfo = UserInfo.translations.get(theInfo);
                                        }
                                        text.setText(theInfo);
                                        String value = info.substring(ix + 1);
                                        if (value.startsWith("Date:")) {
                                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                            value = value.substring(5);
                                            value = sdf.format(new Date(Long.parseLong(value)));
                                        }
                                        text2.setText(value);
                                    }
                                    return convertView;
                                }
                            });
                        }
                    }
                }
            });
        }

    }.start();

    View subscribe_user = findViewById(R.id.subscribe_user);
    View unsubscribe_user = findViewById(R.id.unsubscribe_user);
    View subscribe_comments = findViewById(R.id.subscribe_comments);
    View unsubscribe_comments = findViewById(R.id.unsubscribe_comments);
    View filter_user = findViewById(R.id.filter_user);
    View blacklist_user = findViewById(R.id.blacklist_user);
    View show_blog = findViewById(R.id.show_blog);
    MicroBlog microBlog = MainActivity.getMicroBlog(mid.getMicroBlogCode());
    final MessageMenu mm = microBlog.getMessageMenu(this, messagesSource, null, null);
    JuickMessage message = microBlog.createMessage();
    mm.listSelectedItem = message;
    message.User = new JuickUser();
    message.User.UName = uname;
    message.User.UID = uid;
    message.setMID(mid);
    final UserpicStorage.AvatarID avatarID = microBlog.getAvatarID(message);

    final UserpicStorage.Listener userpicListener = new UserpicStorage.Listener() {
        @Override
        public void onUserpicReady(UserpicStorage.AvatarID id, int size) {
            final UserpicStorage.Listener thiz = this;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    UserpicStorage.instance.removeListener(avatarID, userpicSize, thiz);
                    final Bitmap userpic = UserpicStorage.instance.getUserpic(UserCenterActivity.this, avatarID,
                            userpicSize, thiz);
                    userPic.setImageBitmap(userpic); // can be null
                }
            });
        }
    };
    Bitmap userpic = UserpicStorage.instance.getUserpic(this, avatarID, userpicSize, userpicListener);
    userPic.setImageBitmap(userpic); // can be null

    subscribe_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionSubscribeUser();
        }
    });
    show_blog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mm.listSelectedItem.User.UID == 0) {
                JuickMicroBlog.obtainProperUserIdByName(UserCenterActivity.this, mm.listSelectedItem.User.UName,
                        "Getting Juick User Id", new Utils.Function<Void, Pair<String, String>>() {
                            @Override
                            public Void apply(Pair<String, String> cred) {
                                mm.listSelectedItem.User.UID = Integer.parseInt(cred.first);
                                mm.actionUserBlog();
                                return null;
                            }
                        });
            } else {
                mm.actionUserBlog();
            }
        }
    });
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            search.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenu.ContextMenuInfo menuInfo) {
                    menu.add(0, SEARCH_PAST_CONVERSATIONS, 0, "My dialogs with user");
                    menu.add(0, SEARCH_MORE, 1, "More");
                }
            });
            search.showContextMenu();
        }
    });
    stats.setOnClickListener(new View.OnClickListener() {

        NewJuickPreferenceActivity.MenuItem[] items = new NewJuickPreferenceActivity.MenuItem[] {
                new NewJuickPreferenceActivity.MenuItem(R.string.UserAllTimeActivityReport,
                        R.string.UserAllTimeActivityReport2, new Runnable() {
                            @Override
                            public void run() {
                                NewJuickPreferenceActivity.showChart(UserCenterActivity.this,
                                        "USER_ACTIVITY_VOLUME", "uid=" + uid);
                            }
                        }),
                new NewJuickPreferenceActivity.MenuItem(R.string.UserHoursReport, R.string.UserHoursReport2,
                        new Runnable() {
                            @Override
                            public void run() {
                                NewJuickPreferenceActivity.showChart(UserCenterActivity.this,
                                        "USER_HOURS_ACTIVITY", "uid=" + uid + "&tzoffset="
                                                + TimeZone.getDefault().getRawOffset() / 1000 / 60 / 60);
                            }
                        }) };

        @Override
        public void onClick(View v) {
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    items[position].action.run();
                }
            });
            list.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return items.length;
                }

                @Override
                public Object getItem(int position) {
                    return items[position];
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    LayoutInflater layoutInflater = getLayoutInflater();
                    View listItem = layoutInflater.inflate(android.R.layout.simple_list_item_2, null);
                    TextView text = (TextView) listItem.findViewById(android.R.id.text1);
                    text.setText(items[position].labelId);
                    TextView text2 = (TextView) listItem.findViewById(android.R.id.text2);
                    text2.setText(items[position].label2Id);
                    return listItem;
                }
            });
        }
    });
    blacklist_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionBlacklistUser();
        }
    });
    filter_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionFilterUser(uname);
        }
    });
    unsubscribe_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionUnsubscribeUser();
        }
    });
    subscribe_comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enableJAM(new Runnable() {
                @Override
                public void run() {
                    JAMService.instance.client.subscribeToComments(uname);
                }
            });
        }
    });
    unsubscribe_comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enableJAM(new Runnable() {
                @Override
                public void run() {
                    JAMService.instance.client.unsubscribeFromComments(uname);
                }
            });
        }
    });
}

From source file:com.juick.android.MainActivity.java

public void updateNavigation() {
    navigationItems = new ArrayList<NavigationItem>();

    List<MicroBlog> blogs = new ArrayList<MicroBlog>(microBlogs.values());
    Collections.<MicroBlog>sort(blogs, new Comparator<MicroBlog>() {
        @Override/* w w  w .j  a  va  2s . c om*/
        public int compare(MicroBlog microBlog, MicroBlog microBlog2) {
            return microBlog.getPiority() - microBlog2.getPiority();
        }
    });
    for (MicroBlog blog : blogs) {
        blog.addNavigationSources(navigationItems, this);
    }
    navigationItems.add(new NavigationItem(NAVITEM_UNREAD, R.string.navigationUnread,
            R.drawable.navicon_juickadvanced, "msrcUnread") {
        @Override
        public void action() {
            final NavigationItem thisNi = this;
            final ProgressDialog pd = new ProgressDialog(MainActivity.this);
            pd.setIndeterminate(true);
            pd.setTitle(R.string.navigationUnread);
            pd.setCancelable(true);
            pd.show();
            UnreadSegmentsView.loadPeriods(MainActivity.this,
                    new Utils.Function<Void, ArrayList<DatabaseService.Period>>() {
                        @Override
                        public Void apply(ArrayList<DatabaseService.Period> periods) {
                            if (pd.isShowing()) {
                                pd.cancel();
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                final AlertDialog alerDialog;
                                if (periods.size() == 0) {
                                    alerDialog = builder.setTitle(getString(R.string.UnreadSegments))
                                            .setMessage(getString(R.string.YouHaveNotEnabledForUnreadSegments))
                                            .setCancelable(true)
                                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    dialogInterface.dismiss();
                                                    restoreLastNavigationPosition();
                                                }
                                            }).create();
                                } else {
                                    UnreadSegmentsView unreadSegmentsView = new UnreadSegmentsView(
                                            MainActivity.this, periods);
                                    final int myIndex = navigationItems.indexOf(thisNi);
                                    alerDialog = builder.setTitle(getString(R.string.ChooseUnreadSegment))
                                            .setView(unreadSegmentsView).setCancelable(true)
                                            .setNegativeButton(android.R.string.cancel,
                                                    new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialogInterface,
                                                                int i) {
                                                            dialogInterface.dismiss();
                                                            restoreLastNavigationPosition();
                                                        }
                                                    })
                                            .create();
                                    unreadSegmentsView.setListener(new UnreadSegmentsView.PeriodListener() {
                                        @Override
                                        public void onPeriodClicked(DatabaseService.Period period) {
                                            alerDialog.dismiss();
                                            int beforeMid = period.beforeMid;
                                            Bundle args = new Bundle();
                                            args.putSerializable("messagesSource",
                                                    new UnreadSegmentMessagesSource(
                                                            getString(R.string.navigationUnread),
                                                            MainActivity.this, period));
                                            if (getSelectedNavigationIndex() != myIndex) {
                                                setSelectedNavigationItem(myIndex);
                                            }
                                            runDefaultFragmentWithBundle(args, thisNi);
                                        }
                                    });
                                }
                                alerDialog.show();
                                restyleChildrenOrWidget(alerDialog.getWindow().getDecorView());
                            }
                            return null;
                        }
                    });
            return;
        }
    });
    navigationItems.add(new NavigationItem(NAVITEM_SAVED, R.string.navigationSaved,
            R.drawable.navicon_juickadvanced, "msrcSaved") {
        @Override
        public void action() {
            final Bundle args = new Bundle();
            args.putSerializable("messagesSource", new SavedMessagesSource(MainActivity.this));
            runDefaultFragmentWithBundle(args, this);
        }
    });
    navigationItems.add(new NavigationItem(NAVITEM_RECENT_READ, R.string.navigationRecentlyOpened,
            R.drawable.navicon_juickadvanced, "msrcRecentOpen") {
        @Override
        public void action() {
            final Bundle args = new Bundle();
            args.putSerializable("messagesSource", new RecentlyOpenedMessagesSource(MainActivity.this));
            runDefaultFragmentWithBundle(args, this);
        }
    });
    navigationItems.add(new NavigationItem(NAVITEM_RECENT_WRITE, R.string.navigationRecentlyCommented,
            R.drawable.navicon_juickadvanced, "msrcRecentComment") {
        @Override
        public void action() {
            final Bundle args = new Bundle();
            args.putSerializable("messagesSource", new RecentlyCommentedMessagesSource(MainActivity.this));
            runDefaultFragmentWithBundle(args, this);
        }
    });
    navigationItems.add(new NavigationItem(NAVITEM_ALL_COMBINED, R.string.navigationAllCombined,
            R.drawable.navicon_juickadvanced, "msrcAllCombined") {
        @Override
        public void action() {
            final Bundle args = new Bundle();
            args.putSerializable("messagesSource",
                    new CombinedAllMessagesSource(MainActivity.this, "combined_all"));
            runDefaultFragmentWithBundle(args, this);
        }

        @Override
        public ArrayList<String> getMenuItems() {
            String s = getString(R.string.SelectSources);
            ArrayList<String> strings = new ArrayList<String>();
            strings.add(s);
            return strings;
        }

        @Override
        public void handleMenuAction(int which, String value) {
            switch (which) {
            case 0:
                selectSourcesForAllCombined();
                break;
            }
        }
    });
    navigationItems.add(new NavigationItem(NAVITEM_SUBS_COMBINED, R.string.navigationSubsCombined,
            R.drawable.navicon_juickadvanced, "msrcSubsCombined") {
        @Override
        public void action() {
            final NavigationItem thiz = this;
            new Thread("MessageSource Initializer") {
                @Override
                public void run() {
                    final Bundle args = new Bundle();
                    args.putSerializable("messagesSource",
                            new CombinedSubscriptionMessagesSource(MainActivity.this));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            runDefaultFragmentWithBundle(args, thiz);
                        }

                    });
                }
            }.start();
            final Bundle args = new Bundle();
            runDefaultFragmentWithBundle(args, this);
        }

        @Override
        public ArrayList<String> getMenuItems() {
            String s = getString(R.string.SelectSources);
            ArrayList<String> strings = new ArrayList<String>();
            strings.add(s);
            return strings;
        }

        @Override
        public void handleMenuAction(int which, String value) {
            switch (which) {
            case 0:
                selectSourcesForAllSubs();
                break;
            }
        }
    });
    int index = 10000;
    final SharedPreferences sp_order = getSharedPreferences("messages_source_ordering", MODE_PRIVATE);
    for (NavigationItem navigationItem : navigationItems) {
        navigationItem.itemOrder = sp_order.getInt("order_" + navigationItem.id, -1);
        if (navigationItem.itemOrder == -1) {
            navigationItem.itemOrder = index;
            sp_order.edit().putInt("order_" + navigationItem.id, navigationItem.itemOrder).commit();
        }
        index++;
    }
    Collections.sort(navigationItems, new Comparator<NavigationItem>() {
        @Override
        public int compare(NavigationItem lhs, NavigationItem rhs) {
            return lhs.itemOrder - rhs.itemOrder; // increasing
        }
    });
    allNavigationItems = new ArrayList<NavigationItem>(navigationItems);
    final Iterator<NavigationItem> iterator = navigationItems.iterator();
    while (iterator.hasNext()) {
        NavigationItem next = iterator.next();
        if (next.sharedPrefsKey != null) {
            if (!sp.getBoolean(next.sharedPrefsKey, defaultValues(next.sharedPrefsKey))) {
                iterator.remove();
            }
        }
    }
    sp_order.edit().commit(); // save

    final boolean compressedMenu = sp.getBoolean("compressedMenu", false);
    float menuFontScale = 1;
    try {
        menuFontScale = Float.parseFloat(sp.getString("menuFontScale", "1.0"));
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    final float finalMenuFontScale = menuFontScale;
    navigationList = (DragSortListView) findViewById(R.id.navigation_list);

    // adapter for old-style navigation
    final BaseAdapter navigationAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return navigationItems.size();
        }

        @Override
        public Object getItem(int position) {
            return navigationItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == -1) {
                // NOOK is funny
                return convertView;
            }
            final int screenHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight();
            final int layoutId = R.layout.simple_list_item_1_mine;
            final PressableLinearLayout retval = convertView != null ? (PressableLinearLayout) convertView
                    : (PressableLinearLayout) getLayoutInflater().inflate(layoutId, null);
            TextView tv = (TextView) retval.findViewById(android.R.id.text1);
            if (parent instanceof Spinner) {
                tv.setTextSize(18 * finalMenuFontScale);
                tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            } else {
                tv.setTextSize(22 * finalMenuFontScale);
            }
            tv.setText(getString(navigationItems.get(position).labelId));
            if (compressedMenu) {
                int minHeight = (int) ((screenHeight * 0.7) / getCount());
                tv.setMinHeight(minHeight);
                tv.setMinimumHeight(minHeight);
            }
            retval.setPressedListener(new PressableLinearLayout.PressedListener() {
                @Override
                public void onPressStateChanged(boolean selected) {
                    MainActivity.restyleChildrenOrWidget(retval, false);
                }

                @Override
                public void onSelectStateChanged(boolean selected) {
                    MainActivity.restyleChildrenOrWidget(retval, false);
                }
            });
            MainActivity.restyleChildrenOrWidget(retval, false);
            return retval;
        }
    };
    // adapter for new-style navigation
    final BaseAdapter navigationListAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return getItems().size();
        }

        private ArrayList<NavigationItem> getItems() {
            if (navigationList.isDragEnabled()) {
                return allNavigationItems;
            } else {
                return navigationItems;
            }
        }

        @Override
        public Object getItem(int position) {
            return getItems().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        float textSize = -1;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == -1) {
                // NOOK is funny
                return convertView;
            }
            if (textSize < 0) {
                View inflate = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
                TextView text = (TextView) inflate.findViewById(android.R.id.text1);
                textSize = text.getTextSize();
            }
            final int layoutId = R.layout.navigation_list_item;
            final PressableLinearLayout retval = convertView != null ? (PressableLinearLayout) convertView
                    : (PressableLinearLayout) getLayoutInflater().inflate(layoutId, null);
            TextView tv = (TextView) retval.findViewById(android.R.id.text1);
            final ArrayList<NavigationItem> items = getItems();
            final NavigationItem theItem = items.get(position);
            tv.setText(getString(theItem.labelId));
            ImageButton menuButton = (ImageButton) retval.findViewById(R.id.menu_button);
            menuButton.setFocusable(false);
            ArrayList<String> menuItems = theItem.getMenuItems();
            menuButton.setVisibility(menuItems != null && menuItems.size() > 0 ? View.VISIBLE : View.GONE);
            menuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    doNavigationItemMenu(theItem);
                }
            });
            ImageView iv = (ImageView) retval.findViewById(android.R.id.icon);
            iv.setImageResource(theItem.imageId);
            retval.findViewById(R.id.draggable)
                    .setVisibility(items != allNavigationItems ? View.GONE : View.VISIBLE);
            retval.findViewById(R.id.checkbox).setVisibility(
                    items != allNavigationItems || theItem.sharedPrefsKey == null ? View.GONE : View.VISIBLE);
            CheckBox cb = (CheckBox) retval.findViewById(R.id.checkbox);
            cb.setOnCheckedChangeListener(null);
            if (theItem.sharedPrefsKey != null) {
                cb.setChecked(sp.getBoolean(theItem.sharedPrefsKey, defaultValues(theItem.sharedPrefsKey)));
            }
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sp.edit().putBoolean(theItem.sharedPrefsKey, isChecked).commit();
                }
            });
            int spacing = sp.getInt("navigation_spacing", 0);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize + spacing);

            retval.setPadding(0, spacing, 0, spacing);
            return retval;
        }
    };
    ActionBar bar = getSupportActionBar();
    updateActionBarMode();
    navigationList.setDragEnabled(false);
    ((DragSortController) navigationList.getFloatViewManager()).setDragHandleId(R.id.draggable);
    navigationList.setAdapter(navigationListAdapter);
    navigationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            setSelectedNavigationItem(position);
            closeNavigationMenu(true, false);
        }
    });
    navigationList.setDropListener(new DragSortListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            final NavigationItem item = allNavigationItems.remove(from);
            allNavigationItems.add(to, item);

            int index = 0;
            for (NavigationItem allNavigationItem : allNavigationItems) {
                if (allNavigationItem.itemOrder != index) {
                    allNavigationItem.itemOrder = index;
                    sp_order.edit().putInt("order_" + allNavigationItem.id, allNavigationItem.itemOrder)
                            .commit();
                }
                index++;
            }
            sp_order.edit().commit(); // save
            navigationListAdapter.notifyDataSetChanged();
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });
    bar.setListNavigationCallbacks(navigationAdapter, this);
    final View navigationMenuButton = (View) findViewById(R.id.navigation_menu_button);
    navigationMenuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(MainActivity.this)
                    .setItems(navigationList.isDragEnabled() ? R.array.navigation_menu_end
                            : R.array.navigation_menu_start, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    int spacing = sp.getInt("navigation_spacing", 0);
                                    switch (which) {
                                    case 0:
                                        if (navigationList.isDragEnabled()) {
                                            navigationList.setDragEnabled(false);
                                            updateNavigation();
                                        } else {
                                            navigationList.setDragEnabled(true);
                                        }
                                        break;
                                    case 1:
                                        final Map<String, ?> all = sp_order.getAll();
                                        for (String s : all.keySet()) {
                                            sp_order.edit().remove(s).commit();
                                        }
                                        sp_order.edit().commit();
                                        updateNavigation();
                                        break;
                                    case 2: // wider
                                        sp.edit().putInt("navigation_spacing", spacing + 1).commit();
                                        ((BaseAdapter) navigationList.getAdapter()).notifyDataSetInvalidated();
                                        break;
                                    case 3: // narrower
                                        if (spacing > 0) {
                                            sp.edit().putInt("navigation_spacing", spacing - 1).commit();
                                            ((BaseAdapter) navigationList.getAdapter())
                                                    .notifyDataSetInvalidated();
                                        }
                                        break;
                                    case 4: // logout from..
                                        logoutFromSomeServices();
                                        break;

                                    }
                                    navigationListAdapter.notifyDataSetChanged();
                                }
                            })
                    .setCancelable(true).create().show();
        }
    });

    final SharedPreferences spn = getSharedPreferences("saved_last_navigation_type", MODE_PRIVATE);
    int restoredLastNavItem = spn.getInt("last_navigation", 0);
    if (restoredLastNavItem != 0) {
        NavigationItem allSources = null;
        for (int i = 0; i < navigationItems.size(); i++) {
            NavigationItem navigationItem = navigationItems.get(i);
            if (navigationItem.labelId == restoredLastNavItem) {
                lastNavigationItem = navigationItem;
            }
            if (navigationItem.labelId == R.string.navigationAll) {
                allSources = navigationItem;
            }
        }
        if (lastNavigationItem == null) {
            lastNavigationItem = allSources; // could be null if not configured
        }
        if (lastNavigationItem == null && navigationItems.size() > 0) {
            lastNavigationItem = navigationItems.get(0); // last default
        }
        if (lastNavigationItem != null) {
            restoreLastNavigationPosition();
            lastNavigationItem.action();
        }
    }

}