Example usage for android.widget ListView setSelection

List of usage examples for android.widget ListView setSelection

Introduction

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

Prototype

@Override
public void setSelection(int position) 

Source Link

Document

Sets the currently selected item.

Usage

From source file:com.sentaroh.android.SMBSync2.SyncTaskUtility.java

private void startRemoteNetworkScanThread(final Handler handler, final ThreadCtrl tc, final Dialog dialog,
        final NotifyEvent p_ntfy, final ListView lv_ipaddr, final AdapterScanAddressResultList adap,
        final TextView tvmsg, final String addr, final ArrayList<ScanAddressResultListItem> ipAddressList,
        final String scan_port) {
    final String scan_prog = mContext.getString(R.string.msgs_ip_address_scan_progress);
    Thread th = new Thread(new Runnable() {
        @Override//  ww w  .j  a  va 2  s  .c  o  m
        public void run() {
            if (isIpAddrSmbHost(addr, scan_port)) {
                final String srv_name = getSmbHostName(addr);
                handler.post(new Runnable() {// UI thread
                    @Override
                    public void run() {
                        synchronized (mLockScanCompleteCount) {
                            mScanCompleteCount++;
                            ScanAddressResultListItem li = new ScanAddressResultListItem();
                            li.server_address = addr;
                            li.server_name = srv_name;
                            ipAddressList.add(li);
                            Collections.sort(ipAddressList, new Comparator<ScanAddressResultListItem>() {
                                @Override
                                public int compare(ScanAddressResultListItem lhs,
                                        ScanAddressResultListItem rhs) {
                                    return lhs.server_address.compareTo(rhs.server_address);
                                }
                            });
                        }
                    }
                });
            } else {
                synchronized (mLockScanCompleteCount) {
                    mScanCompleteCount++;
                }
            }
            handler.post(new Runnable() {// UI thread
                @Override
                public void run() {
                    synchronized (mLockScanCompleteCount) {
                        lv_ipaddr.setSelection(lv_ipaddr.getCount());
                        adap.notifyDataSetChanged();
                        String p_txt = String.format(scan_prog, (mScanCompleteCount * 100) / mScanAddrCount);
                        tvmsg.setText(p_txt);

                        if (mScanCompleteCount >= mScanAddrCount) {
                            closeScanRemoteNetworkProgressDlg(dialog, p_ntfy, lv_ipaddr, adap, tvmsg);
                        }
                    }
                }
            });
        }
    });
    th.start();
}

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

private void init(final boolean moveToTop) {
    if (implicitlyCreated)
        return;//w w  w. j  a  v  a  2  s . c  o m

    parent.imagePreviewHelper = listAdapter.imagePreviewHelper = new ImagePreviewHelper(
            (ViewGroup) getView().findViewById(R.id.imagepreview_container), parent);

    final MessageListBackingData savedMainList = JuickAdvancedApplication.instance.getSavedList(getActivity());
    final ListView lv = getListView();
    boolean canUseMainList = getActivity() instanceof MainActivity; //
    if (savedMainList != null && canUseMainList) {
        messagesSource = savedMainList.messagesSource;
        initListWithMessages(savedMainList.messages);
        int selectItem = 0;
        ListAdapter wrappedAdapter = lv.getAdapter();
        for (int i = 0; i < wrappedAdapter.getCount(); i++) {
            Object ai = wrappedAdapter.getItem(i);
            if (ai != null && ai instanceof JuickMessage) {
                if (((JuickMessage) ai).getMID().equals(savedMainList.topMessageId)) {
                    selectItem = i;
                }
            }
        }
        lv.setSelectionFromTop(selectItem, savedMainList.topMessageScrollPos);
        JuickAdvancedApplication.instance.setSavedList(null, false);
    } else {
        final MessagesLoadNotification messagesLoadNotification = new MessagesLoadNotification(getActivity(),
                handler);
        Thread thr = new Thread("Download messages (init)") {

            public void run() {
                final MessagesLoadNotification notification = messagesLoadNotification;
                final Utils.Function<Void, RetainedData> then = new Utils.Function<Void, RetainedData>() {
                    @Override
                    public Void apply(final RetainedData mespos) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                notification.statusText.setText("Filter and format..");
                            }
                        });
                        Log.w("com.juick.advanced", "getFirst: before filter");
                        final ArrayList<JuickMessage> messages = filterMessages(mespos.messages);
                        Log.w("com.juick.advanced", "getFirst: after filter");
                        Boolean ownView = null;
                        if (!JuickMessagesAdapter.dontKeepParsed(parent)) {
                            for (JuickMessage juickMessage : messages) {
                                if (ownView == null) {
                                    MicroBlog blog = MainActivity.microBlogs
                                            .get(juickMessage.getMID().getMicroBlogCode());
                                    ownView = blog instanceof OwnRenderItems;
                                }
                                if (!ownView) {
                                    juickMessage.parsedText = JuickMessagesAdapter.formatMessageText(parent,
                                            juickMessage, false);
                                }
                            }
                        }
                        final Parcelable listPosition = mespos.viewState;
                        if (isAdded()) {
                            if (messages.size() == 0) {
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (notification.lastError == null) {
                                            notification.statusText
                                                    .setText(parent.getString(R.string.EmptyList));
                                        } else {
                                            notification.statusText.setText(
                                                    "Error obtaining messages: " + notification.lastError);

                                        }
                                        notification.progressBar.setVisibility(View.GONE);
                                    }
                                });
                            }
                            final Activity activity = getActivity();
                            if (activity != null) {
                                final Parcelable finalListPosition = listPosition;
                                activity.runOnUiThread(new Runnable() {

                                    public void run() {
                                        try {
                                            if (isAdded()) {
                                                lastPrepareMessages(messages, new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        if (!hasListView()) {
                                                            handler.postDelayed(this, 300);
                                                            return;
                                                        }
                                                        initListWithMessages(messages);
                                                        if (moveToTop) {
                                                            lv.setSelection(0);
                                                        } else {
                                                            if (finalListPosition != null) {
                                                                lv.onRestoreInstanceState(finalListPosition);
                                                            } else {
                                                                //setSelection(messagesSource.supportsBackwardRefresh() ? 1 : 0);
                                                                setSelection(0);
                                                            }
                                                        }
                                                        Log.w("com.juick.advanced", "getFirst: end.");
                                                        handler.postDelayed(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                onListLoaded();
                                                            }
                                                        }, 10);
                                                    }
                                                });
                                            }
                                        } catch (IllegalStateException e) {
                                            Toast.makeText(activity, e.toString(), Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                            }
                        } else {
                            Log.w("com.juick.advanced", "getFirst: not added!");
                        }
                        return null;
                    }
                };
                if (getActivity() != null)
                    messagesSource.setContext(getActivity());
                if (restoreData == null) {
                    messagesSource.getFirst(notification, new Utils.Function<Void, ArrayList<JuickMessage>>() {
                        @Override
                        public Void apply(ArrayList<JuickMessage> juickMessages) {
                            return then.apply(new RetainedData(juickMessages, null));
                        }
                    });
                } else {
                    then.apply((RetainedData) restoreData);
                    restoreData = null;
                }
            }
        };
        thr.setPriority(Thread.MIN_PRIORITY);
        thr.start();
    }
}

From source file:com.sentaroh.android.SMBExplorer.SMBExplorerMain.java

private void startRemoteNetworkScanThread(final Handler handler, final ThreadCtrl tc, final Dialog dialog,
        final NotifyEvent p_ntfy, final ListView lv_ipaddr, final AdapterScanAddressResultList adap,
        final TextView tvmsg, final String addr, final ArrayList<ScanAddressResultListItem> ipAddressList,
        final String scan_port) {
    final String scan_prog = mContext.getString(R.string.msgs_ip_address_scan_progress);
    Thread th = new Thread(new Runnable() {
        @Override/*from www .  j a  va2s. co m*/
        public void run() {
            if (isIpAddrSmbHost(addr, scan_port)) {
                synchronized (mLockScanCompleteCount) {
                    mScanCompleteCount++;
                    String srv_name = getSmbHostName(addr);
                    ScanAddressResultListItem li = new ScanAddressResultListItem();
                    li.server_address = addr;
                    li.server_name = srv_name;
                    ipAddressList.add(li);
                    Collections.sort(ipAddressList, new Comparator<ScanAddressResultListItem>() {
                        @Override
                        public int compare(ScanAddressResultListItem lhs, ScanAddressResultListItem rhs) {
                            return lhs.server_address.compareTo(rhs.server_address);
                        }
                    });
                }
            } else {
                synchronized (mLockScanCompleteCount) {
                    mScanCompleteCount++;
                }
            }
            handler.post(new Runnable() {// UI thread
                @Override
                public void run() {
                    synchronized (mLockScanCompleteCount) {
                        lv_ipaddr.setSelection(lv_ipaddr.getCount());
                        adap.notifyDataSetChanged();
                        String p_txt = String.format(scan_prog, (mScanCompleteCount * 100) / mScanAddrCount);
                        tvmsg.setText(p_txt);

                        if (mScanCompleteCount >= mScanAddrCount) {
                            closeScanRemoteNetworkProgressDlg(dialog, p_ntfy, lv_ipaddr, adap, tvmsg);
                        }
                    }
                }
            });
        }
    });
    th.start();
}

From source file:com.sentaroh.android.SMBExplorer.SMBExplorerMain.java

private void showFileListCache(final ArrayList<FileListCacheItem> fcl, final FileListAdapter fla,
        final ListView flv, final Spinner spinner) {
    // ??//from   w w  w  .ja  v  a  2  s  .  com
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setContentView(R.layout.select_file_cache_dlg);
    //      final Button btnOk = 
    //            (Button) dialog.findViewById(R.id.select_file_cache_dlg_ok);
    final Button btnCancel = (Button) dialog.findViewById(R.id.select_file_cache_dlg_cancel);

    CommonDialog.setDlgBoxSizeCompact(dialog);

    ((TextView) dialog.findViewById(R.id.select_file_cache_dlg_title)).setText("Select file cache");

    ListView lv = (ListView) dialog.findViewById(R.id.select_file_cache_dlg_listview);

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < fcl.size(); i++) {
        list.add(fcl.get(i).directory);
    }
    Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {
            return lhs.compareToIgnoreCase(rhs);
        }
    });
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.simple_list_item_1o, list);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String t_dir = list.get(position);
            FileListCacheItem dhi = getFileListCache(t_dir, fcl);
            if (dhi != null) {
                mIgnoreSpinnerSelection = true;
                int s_no = -1;
                for (int i = 0; i < spinner.getCount(); i++) {
                    if (spinner.getItemAtPosition(i).toString().equals(dhi.profile_name)) {
                        s_no = i;
                        break;
                    }
                }
                fla.setDataList(dhi.file_list);
                fla.notifyDataSetChanged();
                if (currentTabName.equals(SMBEXPLORER_TAB_LOCAL)) {
                    localBase = dhi.base;
                    if (dhi.base.equals(dhi.directory))
                        localDir = "";
                    else
                        localDir = dhi.directory.replace(localBase + "/", "");
                    replaceDirHist(localDirHist, dhi.directory_history);
                    setFilelistCurrDir(localFileListDirSpinner, localBase, localDir);
                    setFileListPathName(localFileListPathBtn, localFileListCache, localBase, localDir);
                    setEmptyFolderView();
                    localCurrFLI = dhi;
                    flv.setSelection(0);
                    for (int j = 0; j < localFileListView.getChildCount(); j++)
                        localFileListView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
                    localFileListReloadBtn.performClick();
                } else if (currentTabName.equals(SMBEXPLORER_TAB_REMOTE)) {
                    remoteBase = dhi.base;
                    if (dhi.base.equals(dhi.directory))
                        remoteDir = "";
                    else
                        remoteDir = dhi.directory.replace(remoteBase + "/", "");
                    replaceDirHist(remoteDirHist, dhi.directory_history);
                    setFilelistCurrDir(remoteFileListDirSpinner, remoteBase, remoteDir);
                    setFileListPathName(remoteFileListPathBtn, remoteFileListCache, remoteBase, remoteDir);
                    setEmptyFolderView();
                    remoteCurrFLI = dhi;
                    flv.setSelection(0);

                    //                  Log.v("","base="+remoteBase+", dir="+remoteDir+", histsz="+remoteDirHist.size());
                    for (int j = 0; j < remoteFileListView.getChildCount(); j++)
                        remoteFileListView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
                    if (remoteDir.equals("")) {
                        remoteFileListTopBtn.setEnabled(false);
                        remoteFileListUpBtn.setEnabled(false);
                    } else {
                        remoteFileListTopBtn.setEnabled(true);
                        remoteFileListUpBtn.setEnabled(true);
                    }
                }

                if (s_no != -1)
                    spinner.setSelection(s_no);
                Handler hndl = new Handler();
                hndl.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mIgnoreSpinnerSelection = false;
                    }
                }, 100);
                dialog.dismiss();
            }
        }
    });

    btnCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();

}

From source file:com.rfo.basic.Run.java

private void doDebugDialog() {

    int lineIndex = 0;
    String text = "";
    if (mInterpreter != null) {
        lineIndex = mInterpreter.ExecutingLineIndex;
        text = mInterpreter.ExecutingLineBuffer.text();
    }//from   ww w  .  jav a 2  s .co  m

    ArrayList<String> msg = new ArrayList<String>();

    if (!dbDialogProgram) {
        msg = mInterpreter.dbDoFunc();
        msg.add("Executable Line #:    " + Integer.toString(lineIndex + 1) + '\n' + chomp(text));
    }

    if (dbDialogScalars)
        msg.addAll(mInterpreter.dbDoScalars("  "));
    if (dbDialogArray)
        msg.addAll(mInterpreter.dbDoArray("  "));
    if (dbDialogList)
        msg.addAll(mInterpreter.dbDoList("  "));
    if (dbDialogStack)
        msg.addAll(mInterpreter.dbDoStack("  "));
    if (dbDialogBundle)
        msg.addAll(mInterpreter.dbDoBundle("  "));
    if (dbDialogWatch)
        msg.addAll(mInterpreter.dbDoWatch("  "));

    if (dbDialogProgram) {
        for (int i = 0; i < Basic.lines.size(); ++i) {
            msg.add(((i == lineIndex) ? " >>" : "   ") // mark current line
                    + (i + 1) + ": " // one-based line index
                    + chomp(Basic.lines.get(i).text())); // remove newline
        }
    }

    LayoutInflater inflater = getLayoutInflater();
    View dialogLayout = inflater.inflate(R.layout.debug_dialog_layout, null);

    ListView debugView = (ListView) dialogLayout.findViewById(R.id.debug_list);
    debugView.setAdapter(new ArrayAdapter<String>(Run.this, R.layout.debug_list_layout, msg));
    debugView.setVerticalScrollBarEnabled(true);
    if (dbDialogProgram) {
        debugView.setSelection(lineIndex);
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(Run.this).setCancelable(true)
            .setTitle(R.string.debug_name).setView(dialogLayout);

    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        public void onCancel(DialogInterface arg0) {
            DebuggerHalt = true;
            WaitForDebugResume = false;
            releaseDebugLOCK();
        }
    });

    builder.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            WaitForDebugResume = false;
            releaseDebugLOCK();
        }
    });

    builder.setNeutralButton("Step", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            DebuggerStep = true;
            WaitForDebugResume = true;
            releaseDebugLOCK();
        }
    });

    builder.setNegativeButton("View Swap", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dbSwap = true;
            releaseDebugLOCK();
        }
    });

    dbDialog = builder.show();
    dbDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
}