Java Code Examples for android.widget.ListView.setSelection()

The following are Jave code examples for showing how to use setSelection() of the android.widget.ListView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: q-mail   File: K9ListActivity.java   View Source Code Vote up 6 votes
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Shortcuts that work no matter what is selected
    if (QMail.useVolumeKeysForListNavigationEnabled() &&
            (keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
            keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {

        final ListView listView = getListView();

        int currentPosition = listView.getSelectedItemPosition();
        if (currentPosition == AdapterView.INVALID_POSITION || listView.isInTouchMode()) {
            currentPosition = listView.getFirstVisiblePosition();
        }

        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && currentPosition > 0) {
            listView.setSelection(currentPosition - 1);
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN &&
                currentPosition < listView.getCount()) {
            listView.setSelection(currentPosition + 1);
        }

        return true;
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 2
Project: AssistantBySDK   File: ItemIncomeDialog.java   View Source Code Vote up 6 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_income);
    dao = AccountItemDao.getInstance();
    EventBus.getDefault().register(this);
    itemListView = (ListView) findViewById(R.id.aii_item_list);
    items = dao.findAllByExpense(0);
    inflater = LayoutInflater.from(mContext);
    itemListView.setAdapter(itemAdapter);
 //   itemListView.setOnItemClickListener(itemItemListener);

    findViewById(R.id.aii_back).setOnClickListener(clickListener);
    findViewById(R.id.aii_edit).setOnClickListener(clickListener);

    if (!TextUtils.isEmpty(item)) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getItem().equals(item)) {
                seletectedItemPosition = i;
                break;
            }
        }
    }
    itemListView.setSelection(seletectedItemPosition);
}
 
Example 3
Project: Android-DFU-App   File: UARTLogFragment.java   View Source Code Vote up 6 votes
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
	// Here we have to restore the old saved scroll position, or scroll to the bottom if before adding new events it was scrolled to the bottom.  
	final ListView list = getListView();
	final int position = mLogScrollPosition;
	final boolean scrolledToBottom = position == LOG_SCROLLED_TO_BOTTOM || (list.getCount() > 0 && list.getLastVisiblePosition() == list.getCount() - 1);

	mLogAdapter.swapCursor(data);

	if (position > LOG_SCROLL_NULL) {
		list.setSelectionFromTop(position, 0);
	} else {
		if (scrolledToBottom)
			list.setSelection(list.getCount() - 1);
	}
	mLogScrollPosition = LOG_SCROLL_NULL;
}
 
Example 4
Project: boohee_v5.6   File: ListViewUtils.java   View Source Code Vote up 6 votes
public static void smoothScrollListView(ListView listView, int position) {
    if (VERSION.SDK_INT >= 11) {
        listView.smoothScrollToPositionFromTop(0, 0);
    } else if (VERSION.SDK_INT >= 8) {
        listView.smoothScrollToPosition(0);
    } else {
        listView.setSelection(position);
    }
}
 
Example 5
Project: Android_Labs   File: ListActivity.java   View Source Code Vote up 6 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    final String MEDIA_PATH = Environment.getExternalStorageDirectory().toString();
    Log.d(TAG,MEDIA_PATH);
    if (MainActivity.isFirstTime){
        try {
            copyResourcesToStorage(MEDIA_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (songsList.size()>0)songsList.clear();
        ScanDirectory(MEDIA_PATH);
        Log.d(TAG,"Songs_Found" + songsList.size());
    }
    else{
        songsList.get(indexOfTrackSelected).setISsongSelected(false);
    }
    songsDisplayAdapter = new SongsDisplayAdapter(this,songsList);
    ListView listView = (ListView) findViewById(R.id.Songs_List);
    listView.setAdapter(songsDisplayAdapter);
    listView.setSelection(indexOfTrackSelected);
}
 
Example 6
Project: LuaViewPlayground   File: UDBaseListView.java   View Source Code Vote up 5 votes
/**
 * listview滚动到顶部
 *
 * @param animate 是否动画
 * @return
 */
public UDBaseListOrRecyclerView scrollToTop(final int offset, final boolean animate) {
    final ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            if (lv.getFirstVisiblePosition() > 7) {//hack fast scroll
                lv.setSelection(7);
            }
            lv.smoothScrollToPositionFromTop(0, offset);
        } else {
            lv.setSelectionFromTop(0, offset);
        }
    }
    return this;
}
 
Example 7
Project: AssistantBySDK   File: ItemIncomeActivity.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_item_income);
	dao=AccountItemDao.getInstance();
	itemListView=(ListView) findViewById(R.id.aii_item_list);
	items=dao.findAllByExpense(0);
	itemListView.setAdapter(itemAdapter);
	itemListView.setOnItemClickListener(itemItemListener);
	
	findViewById(R.id.aii_back).setOnClickListener(clickListener);
	findViewById(R.id.aii_edit).setOnClickListener(clickListener);
	
	intent=getIntent();
	if(intent!=null){
		String item=intent.getStringExtra(ITEM);
		if(!TextUtils.isEmpty(item)){
				boolean r=false;
				for(int i=0;i<items.size();i++){
					if(items.get(i).getItem().equals(item)){
						seletectedItemPosition=i;
						break;
					}
				}
		}
	}
	itemListView.setSelection(seletectedItemPosition);
}
 
Example 8
Project: chromium-for-android-56-debug-video   File: PassphraseTypeDialogFragment.java   View Source Code Vote up 5 votes
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.sync_passphrase_types, null);

    // Configure the passphrase type list
    ListView list = (ListView) v.findViewById(R.id.passphrase_types);
    Adapter adapter = createAdapter(getCurrentTypeFromArguments());
    list.setAdapter(adapter);
    list.setId(R.id.passphrase_type_list);
    list.setOnItemClickListener(this);
    list.setDividerHeight(0);
    PassphraseType currentType = getCurrentTypeFromArguments();
    list.setSelection(adapter.getPositionForType(currentType));

    // Configure the hint to reset the passphrase settings
    // Only show this hint if encryption has been set to use sync passphrase
    if (currentType == PassphraseType.CUSTOM_PASSPHRASE) {
        TextView instructionsView = (TextView) v.findViewById(R.id.reset_sync_text);
        instructionsView.setVisibility(View.VISIBLE);
        instructionsView.setMovementMethod(LinkMovementMethod.getInstance());
        instructionsView.setText(getResetText());
    }

    // Create and return the dialog
    return new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme)
            .setNegativeButton(R.string.cancel, this)
            .setTitle(R.string.sync_passphrase_type_title)
            .setView(v)
            .create();
}
 
Example 9
Project: MultiColumnsPicker   File: MultiColumnsPicker.java   View Source Code Vote up 5 votes
private void initializeListView(final int page, final List<T> data) {
    final ListView listView = mListViewList.get(page);

    final ColumnAdapter adapter;

    if (mOnAdapterProvideList != null && mOnAdapterProvideList.get(page) != null) {
        adapter = mOnAdapterProvideList.get(page).provideAdapter(this, data);
    } else if (mOnAdapterProvide != null) {
        adapter = mOnAdapterProvide.provideAdapter(this, data);
    } else {
        adapter = new SimpleColumnAdapter<>(data, MultiColumnsPicker.this);
    }

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            for (T t : data) {
                setChecked(t, false);
            }
            setChecked(data.get(position), true);
            mOnSelected.onSelected(page, data.get(position));
            adapter.notifyDataSetChanged();
        }
    });
    for (int i = 0; i < data.size(); i++) {
        if (isChecked(data.get(i))) {
            listView.setSelection(i);
            break;
        }
    }

}
 
Example 10
Project: sctalk   File: MessageActivity.java   View Source Code Vote up 5 votes
/**
 * @Description 滑动到列表底部
 */
private void scrollToBottomListItem() {
    logger.d("message_activity#scrollToBottomListItem");

    // todo eric, why use the last one index + 2 can real scroll to the
    // bottom?
    ListView lv = lvPTR.getRefreshableView();
    if (lv != null) {
        lv.setSelection(adapter.getCount() + 1);
    }
    textView_new_msg_tip.setVisibility(View.GONE);
}
 
Example 11
Project: AssistantBySDK   File: ItemExpenseDialog.java   View Source Code Vote up 4 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_expense);
    dao = AccountItemDao.getInstance();
    EventBus.getDefault().register(this);
    itemListView = (ListView) findViewById(R.id.aie_item_list);
    subItemListView = (ListView) findViewById(R.id.aie_subitem_list);
    items = dao.findAllByExpense(1);
    subItems = dao.findAllSubItem();
    inflater = LayoutInflater.from(mContext);
    Log.w(TAG, "items.size=" + items.size() + ",subItems.size=" + subItems.size());
    itemListView.setAdapter(itemAdapter);
    itemListView.setOnItemClickListener(itemItemListener);
    subItemListView.setAdapter(subAdapter);
  //  subItemListView.setOnItemClickListener(subItemItemListener);

    findViewById(R.id.aie_back).setOnClickListener(clickListener);
    findViewById(R.id.aie_edit).setOnClickListener(clickListener);

    if (!TextUtils.isEmpty(item)) {
        String is[] = item.split(",");
        if (is.length == 2) {
            boolean r = false;
            for (int i = 0; i < subItems.size(); i++) {
                if (subItems.get(i).getName().equals(is[1])) {
                    for (int j = 0; j < items.size(); j++) {
                        if (items.get(j).getId() == subItems.get(i).getItemid()) {
                            if (items.get(j).getItem().equals(is[0])) {
                                selectedSubItemId = subItems.get(i).getId();
                                seletectedItemPosition = j;
                                r = true;
                                break;
                            }
                        }
                    }
                    if (r)
                        break;
                }
            }
        }
    }
    setShowSubItems(items.get(seletectedItemPosition).getId());
    itemListView.setSelection(seletectedItemPosition);
}
 
Example 12
Project: AssistantBySDK   File: ItemExpenseActivity.java   View Source Code Vote up 4 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_expense);
    dao = AccountItemDao.getInstance();
    itemListView = (ListView) findViewById(R.id.aie_item_list);
    subItemListView = (ListView) findViewById(R.id.aie_subitem_list);
    items = dao.findAllByExpense(1);
    subItems = dao.findAllSubItem();
    Log.w(TAG, "items.size=" + items.size() + ",subItems.size=" + subItems.size());
    itemListView.setAdapter(itemAdapter);
    itemListView.setOnItemClickListener(itemItemListener);
    subItemListView.setAdapter(subAdapter);
    subItemListView.setOnItemClickListener(subItemItemListener);

    findViewById(R.id.aie_back).setOnClickListener(clickListener);
    findViewById(R.id.aie_edit).setOnClickListener(clickListener);

    intent = getIntent();
    if (intent != null) {
        String item = intent.getStringExtra(ITEM);
        Log.w(TAG, "item=" + item);
        if (!TextUtils.isEmpty(item)) {
            String is[] = item.split(",");
            if (is.length == 2) {
                boolean r = false;
                for (int i = 0; i < subItems.size(); i++) {
                    if (subItems.get(i).getName().equals(is[1])) {
                        for (int j = 0; j < items.size(); j++) {
                            if (items.get(j).getId() == subItems.get(i).getItemid()) {
                                if (items.get(j).getItem().equals(is[0])) {
                                    selectedSubItemId = subItems.get(i).getId();
                                    seletectedItemPosition = j;
                                    r = true;
                                    break;
                                }
                            }
                        }
                        if (r)
                            break;
                    }
                }
            }
        }
    }
    setShowSubItems(items.get(seletectedItemPosition).getId());
    itemListView.setSelection(seletectedItemPosition);
}