/** * 关注1 * 模糊查询数据 & 显示到ListView列表上 */ private void queryData(String tempName) { // 1. 模糊搜索 Cursor cursor = helper.getReadableDatabase().rawQuery( "select id as _id,name from Search where name like '%" + tempName + "%' order by id desc ", null); // 2. 创建adapter适配器对象 & 装入模糊搜索的结果 adapter = new SimpleCursorAdapter(mContext, android.R.layout.simple_list_item_1, cursor, new String[] { "name" }, new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); // 3. 设置适配器 listView.setAdapter(adapter); adapter.notifyDataSetChanged(); System.out.println(cursor.getCount()); // 当输入框为空 & 数据库中有搜索记录时,显示 "删除搜索记录"按钮 if (tempName.equals("") && cursor.getCount() != 0){ tv_clear.setVisibility(VISIBLE); } else { tv_clear.setVisibility(INVISIBLE); }; }
@Override protected void delete(final BaseModel model) { final RoadModel road = (RoadModel) model; if (road.isDefaultRoad()) { showDefaultProjectToast(); return; } new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { MeasurementsDataHelper.getInstance().deleteRoad(road.getId(), true); final Cursor cursor = roadDAO.getAllRoadByFolderIdCursor(((RoadModel) model).getFolderId()); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
@Override protected void delete(final BaseModel model) { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { MeasurementModel measurement = (MeasurementModel) model; MeasurementsDataHelper.getInstance().deleteMeasurement(measurement.getId(), true); final Cursor cursor = measurementDAO.getAllMeasurementByRoadIdCursor(roadModel.getId()); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
@Override protected void delete(final BaseModel model) { final FolderModel project = (FolderModel) model; if (project.isDefaultProject()) { showDefaultProjectToast(); return; } new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { MeasurementsDataHelper.getInstance().deleteProject(project.getId(), true); final Cursor cursor = folderDAO.getAllFolderCursor(); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
public void refreshFolders() { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { //long folderId = folderDAO.putFolder(folderModel); MeasurementsDataHelper.getInstance().refreshFoldersCountersSync(); final Cursor cursor = folderDAO.getAllFolderCursor(); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
@Override protected void delete(final BaseModel model) { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { TagModel tagModel = (TagModel) model; tagDAO.delete(tagModel); final Cursor cursor = tagDAO. getTagsByRoadIdCursor(RAApplication.getInstance().getCurrentRoadId()); return cursor; } @Override protected void onPostExecute(Cursor o) { ((CursorAdapter) getAdapter()).swapCursor(o); } }.execute(); }
private void swapCursor(){ int offsetFromTop = 0; int firstVisiblePosition = Math.max(0, feedListView.getFirstVisiblePosition()); if(feedListView.getChildCount() > 0) { offsetFromTop = feedListView.getChildAt(0).getTop(); } CursorAdapter newAdapter = ((CursorAdapter) feedListView.getAdapter()); newAdapter.swapCursor(getCursor()); if(SearchHelper.searchToSQL(query) == null) { ((FeedAdapter) newAdapter).setHighlight(Utils.getKeywords(query)); } feedListView.setAdapter(newAdapter); feedListView.setSelectionFromTop(firstVisiblePosition, offsetFromTop); }
private void setListInSelectionMode(){ inSelectionMode = true; ((FeedAdapter) feedListView.getAdapter()).setSelectionMode(true); swapCursor(); feedListView.setOnItemLongClickListener(null); feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor c = ((CursorAdapter) feedListView.getAdapter()).getCursor(); c.moveToPosition(position); boolean isChecked = c.getInt(c.getColumnIndex(MessageStore.COL_CHECKED)) == MessageStore.TRUE; String message = c.getString(c.getColumnIndex(MessageStore.COL_MESSAGE)); MessageStore.getInstance(getActivity()).checkMessage(message, !isChecked); swapCursor(); setActionbar(); } }); setActionbar(); newPostButton.setVisibility(View.INVISIBLE); }
private void swapCursor(){ int offsetFromTop = 0; int firstVisiblePosition = Math.max(0, contactListView.getFirstVisiblePosition()); if(contactListView.getChildCount() > 0) { offsetFromTop = contactListView.getChildAt(0).getTop(); } CursorAdapter newAdapter = ((CursorAdapter) contactListView.getAdapter()); newAdapter.swapCursor(getCursor()); if(SearchHelper.searchToSQL(query) == null) { ((ContactAdapter) newAdapter).setHighlight(Utils.getKeywords(query)); } contactListView.setAdapter(newAdapter); contactListView.setSelectionFromTop(firstVisiblePosition, offsetFromTop); }
private void swapCursor(){ int offsetFromTop = 0; int firstVisiblePosition = Math.max(0, listView.getFirstVisiblePosition()); if(listView.getChildCount() > 0) { offsetFromTop = listView.getChildAt(0).getTop(); } CursorAdapter newAdapter = ((CursorAdapter) listView.getAdapter()); newAdapter.swapCursor(getCursor()); if(SearchHelper.searchToSQL(query) == null) { ((FeedReplyAdapter) newAdapter).setHighlight(Utils.getKeywords(query)); } listView.setAdapter(newAdapter); listView.setSelectionFromTop(firstVisiblePosition, offsetFromTop); parentMessage.setAdapter(new FeedAdapter(getActivity(), MessageStore.getInstance(getActivity()).getMessageById(messageId), false, feedAdapterCallbacks)); }
/** * 关注1 * 模糊查询数据 & 显示到ListView列表上 */ private void queryData(String tempName) { // 1. 模糊搜索 Cursor cursor = helper.getReadableDatabase().rawQuery( "select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null); // 2. 创建adapter适配器对象 & 装入模糊搜索的结果 adapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, cursor, new String[] { "name" }, new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); // 3. 设置适配器 listView.setAdapter(adapter); adapter.notifyDataSetChanged(); System.out.println(cursor.getCount()); // 当输入框为空 & 数据库中有搜索记录时,显示 "删除搜索记录"按钮 if (tempName.equals("") && cursor.getCount() != 0){ tv_clear.setVisibility(VISIBLE); } else { tv_clear.setVisibility(INVISIBLE); }; }
private void handleSelectAll() { selectedContacts.clear(); Cursor cursor = null; try { cursor = ContactAccessor.getInstance().getCursorForContactsWithNumbers(getActivity()); while (cursor != null && cursor.moveToNext()) { ContactData contactData = ContactAccessor.getInstance().getContactData(getActivity(), cursor); if (contactData.numbers.isEmpty()) continue; else if (contactData.numbers.size() == 1) addSingleNumberContact(contactData); else addMultipleNumberContact(contactData, null); } } finally { if (cursor != null) cursor.close(); } ((CursorAdapter)getListView().getAdapter()).notifyDataSetChanged(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_conversation); Bundle extras = getIntent().getExtras(); mShowSentMessages = extras.getBoolean("sentMessages", false); if(mShowSentMessages) { getActionBar().setTitle(R.string.sent_messages); } else { mUserId = extras.getString("fromUserId"); String userEmail = extras.getString("fromUserEmail"); getActionBar().setTitle(userEmail); } final ListAdapter adapter = new ConversationAdapter(this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); getListView().setAdapter(adapter); getLoaderManager().initLoader(0, null, this); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listDrinks = getListView(); try { SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); db = starbuzzDatabaseHelper.getReadableDatabase(); cursor = db.query("DRINK", new String[]{"_id", "NAME"}, null, null, null, null, null); CursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{"NAME"}, new int[]{android.R.id.text1}, 0); listDrinks.setAdapter(listAdapter); } catch(SQLiteException e) { Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT); toast.show(); } }
@Override public void onRestart() { super.onRestart(); try { StarbuzzDatabaseHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); db = starbuzzDatabaseHelper.getReadableDatabase(); Cursor newCursor = db.query("DRINK", new String[] { "_id", "NAME"}, "FAVORITE = 1", null, null, null, null); ListView listFavorites = (ListView)findViewById(R.id.list_favorites); CursorAdapter adapter = (CursorAdapter) listFavorites.getAdapter(); adapter.changeCursor(newCursor); favoritesCursor = newCursor; } catch(SQLiteException e) { Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT); toast.show(); } }
private void displayStock(Stock stock, InvestmentTransactionViewHolder viewHolder) { if (mAccount == null) return; // Date String dateDisplay = new MmxDate(stock.getPurchaseDate()).toString(Constants.LONG_DATE_PATTERN); viewHolder.dateView.setText(dateDisplay); // Account. Cursor cursor = ((CursorAdapter) viewHolder.accountSpinner.getAdapter()).getCursor(); int accountIndex = SpinnerHelper.getPosition(mAccount.getName(), Account.ACCOUNTNAME, cursor); if (accountIndex >= 0) { viewHolder.accountSpinner.setSelection(accountIndex, true); } viewHolder.stockNameEdit.setText(stock.getName()); viewHolder.symbolEdit.setText(stock.getSymbol()); showNumberOfShares(); showPurchasePrice(); viewHolder.notesEdit.setText(stock.getNotes()); showCommission(); showCurrentPrice(); showValue(); }
void init(final Cursor c, int flags) { if ((flags & CursorAdapter.FLAG_AUTO_REQUERY) == CursorAdapter.FLAG_AUTO_REQUERY) { flags |= CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER; mAutoRequery = true; } else { mAutoRequery = false; } boolean cursorPresent = c != null; mCursor = c; mDataValid = cursorPresent; mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1; if ((flags & CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) == CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) { mChangeObserver = new ChangeObserver(); mDataSetObserver = new MyDataSetObserver(); } else { mChangeObserver = null; mDataSetObserver = null; } if (cursorPresent) { if (mChangeObserver != null) c.registerContentObserver(mChangeObserver); if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver); } }
@Override protected void onPause() { // Remember position in listview (before any adapter change) prevItemVisible = getListView().getFirstVisiblePosition(); CursorAdapter adapter = (CursorAdapter) getListAdapter(); if (adapter != null) { // Prevents on-screen 'no tracks' message getListView().setEmptyView(findViewById(android.R.id.empty)); // Properly close the adapter cursor Cursor cursor = adapter.getCursor(); stopManagingCursor(cursor); cursor.close(); setListAdapter(null); } super.onPause(); }
private void assignListAdapter() { // Use the content resolver to get a list of // contacts from the People app content provider // and assign member cursor. getContacts(); // Create the cursor adapter and assign cursor from above. _adapter = new SimpleCursorAdapter(this, R.layout.contact_list_row, _cursor, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, new int[] { R.id.textViewName }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); // Assign adapter to list fragment. _fragmentList.setListAdapter(_adapter); }
private void initDomainPasswordEntry() { mDomainEdit = (AutoCompleteTextView) findViewById(R.id.domain_edit); mMasterPwEdit = (VisualHashEditText) findViewById(R.id.password_edit); mMasterPwEdit.setOnEditorActionListener(this); final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, new String[] { "domain" }, new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); adapter.setFilterQueryProvider(this); adapter.setStringConversionColumn(DOMAIN_COLUMN); // initialize the autocompletion mDomainEdit.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(final AdapterView<?> arg0, final View arg1, final int arg2, final long arg3) { mMasterPwEdit.requestFocus(); } }); mDomainEdit.setAdapter(adapter); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CONNECTION_FAILURE_RESOLUTION_REQUEST: if (resultCode == RESULT_OK) { Toast.makeText(this, "Resolution now ok, could retry now.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Resolution failed.", Toast.LENGTH_SHORT).show(); } break; case ENTRY_ACTION_REQUEST: if (resultCode == RESULT_OK) { // refresh dbc = queryEntries(); ((CursorAdapter) listLocations.getAdapter()).changeCursor(dbc); } break; default: Toast.makeText(this, "Unknown activity result.", Toast.LENGTH_SHORT).show(); } }
protected void updateLocations(Location location) { // display location as latitude/longitude if (location != null) { textLatLng.setText(String.format("%s %s%n%s %s", getResources().getText(R.string.text_latitude), Utilities.formatLatLon(location.getLatitude()), getResources().getText(R.string.text_longitude), Utilities.formatLatLon(location.getLongitude()))); } else { textLatLng.setText(R.string.latlng_unknown); } // remember last position lastLocation = location; // force list view to update ((CursorAdapter) listLocations.getAdapter()).notifyDataSetChanged(); }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); try { setHasOptionsMenu(true); } catch(Exception ex) { Log.e(TAG, "Failed to create menu: " + ex.getMessage()); } adapter_ = new ActionItemAdapter(getActivity(), CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); setListAdapter(adapter_); getLoaderManager().initLoader(LOADER_ACTIONS, null, lm_ = new LoaderMgr()); if (savedInstanceState != null) { // Restore last state for checked position. cur_list_position_ = savedInstanceState.getInt("cur_list_position", 0); } registerForContextMenu(getListView()); }
public void ShowProvince() { Cursor cursor = DataSupport.findBySQL("SELECT id AS _id, province FROM Location GROUP BY province ORDER BY province"); adapter = new SimpleCursorAdapter(this, R.layout.choose_area_item, cursor, new String[] {"province"}, new int[] {R.id.place}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(adapter); chooseWhat.setText("选择省份"); }
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if(loader.getId()==0) { //if was called with this fragment, not a child if (mCursor == null) { mCursor = cursor; bindAdapter(); } else { boolean forceReloadUI = (mCursor.getCount() == 0 || cursor.getCount() == 0); mCursor = cursor; ((CursorAdapter) mBrowserAdapter).changeCursor(cursor); if (forceReloadUI) { postBindAdapter(); } } } }
private void queryData(String tempName) { Cursor cursor = helper.getReadableDatabase().rawQuery( "select id as _id,name from records where name like '%" + tempName + "%' order by id desc ", null); // 创建adapter适配器对象 adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] { "name" }, new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); // 设置适配器 listview.setAdapter(adapter); adapter.notifyDataSetChanged(); }
public void refreshRoads() { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { MeasurementsDataHelper.getInstance().refreshRoadsCountersSync(); final Cursor cursor = roadDAO.getAllRoadByFolderIdCursor(folder.getId()); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
public void refreshItems() { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { MeasurementsDataHelper.getInstance().refreshMeasurementsCountersSync(); final Cursor cursor = measurementDAO.getAllMeasurementByRoadIdCursor(roadModel.getId()); return cursor; } @Override protected void onPostExecute(Cursor o) { super.onPostExecute(o); ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
protected void initAdapter(boolean force) { if (force || adapter == null) { adapter = createAdapter(); } if (adapter != null && adapter instanceof CursorAdapter) { ((CursorAdapter) adapter).setFilterQueryProvider(new MyFilterQueryProvider()); } startSearch(getSearchText()); listView.setAdapter(adapter); }
public void refreshTags() { new AsyncTask<Void, Void, Cursor>() { @Override protected Cursor doInBackground(Void[] params) { final Cursor cursor = tagDAO.getTagsByRoadIdCursor(RAApplication.getInstance().getCurrentRoadId()); return cursor; } @Override protected void onPostExecute(Cursor o) { ((CursorAdapter)getAdapter()).swapCursor(o); } }.execute(); }
private void setListInSelectionMode(){ inSelectionMode = true; ((ContactAdapter)contactListView.getAdapter()).setSelectionMode(inSelectionMode); swapCursor(); contactListView.setOnItemLongClickListener(null); contactListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor c = ((CursorAdapter) contactListView.getAdapter()).getCursor(); c.moveToPosition(position); boolean isChecked = c.getInt(c.getColumnIndex(FriendStore.COL_CHECKED)) == FriendStore.TRUE; String publicKey = c.getString(c.getColumnIndex(FriendStore.COL_PUBLIC_KEY)); FriendStore.getInstance(getActivity()).setChecked(publicKey, !isChecked); swapCursor(); int checkedCount = (int) FriendStore.getInstance(getActivity()).getCheckedCount(); ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(checkedCount <= 99 ? String.valueOf(checkedCount) : "+99"); updateSelectAll(); if (menu != null) menu.findItem(R.id.action_delete).setEnabled(checkedCount > 0); } }); setActionbar(); }
public static SubjectFactory<CursorAdapterSubject, CursorAdapter> type() { return new SubjectFactory<CursorAdapterSubject, CursorAdapter>() { @Override public CursorAdapterSubject getSubject(FailureStrategy fs, CursorAdapter that) { return new CursorAdapterSubject(fs, that); } }; }
public void onClick(DialogInterface dialog, int which) { ContactData selected = selectedContacts.get(contactData.id); if (selected == null && textView != null) { if (textView != null) textView.setChecked(false); } else if (selected.numbers.size() == 0) { selectedContacts.remove(selected.id); if (textView != null) textView.setChecked(false); } if (textView == null) ((CursorAdapter)getListView().getAdapter()).notifyDataSetChanged(); }
private void setupLocationsAdapter(String server_id) { LocationTasks.getAllLocations(this, server_id, new OnDataSetReceivedListener() { @Override public void onDataSetReceived(Cursor cursor) { mLocationAdapter = new LocationCursorAdapter(ListLocationsActivity.this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); ListView listView = (ListView) findViewById(R.id.detected_activities_listview); listView.setAdapter(mLocationAdapter); } }); }
@Override public void onLoaderReset(Loader<Cursor> loader) { final CursorAdapter adapter = (CursorAdapter)getListAdapter(); if(adapter != null) { adapter.swapCursor(null); } }
@Override public void onLoadFinished(Loader loader, Cursor data) { switch(loader.getId()) { case LOADER_MAKERS: ((CursorAdapter)mTxtMaker.getAdapter()).swapCursor(data); } }