我不想从CallLog.Calls.CONTENT_URI非Activity类中获取数据.我使用游标从CallLog获取这些数据.但在非活动类游标显示一些错误,所以我怎么能这样做呢?
现在我这样做,
public class CallReceiver extends Activity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
Cursor cursor = managedQuery(android.provider.CallLog.Calls.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor);
Run Code Online (Sandbox Code Playgroud) 我真的很惊讶我无法在任何地方找到答案.所以这就是问题所在.
ContentResolver resolver = getContentResolver();
String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
String where = Phone.NUMBER+"=?";
String[] params = new String[] {"777 777 7777"};
Cursor cursor = resolver.query(Phone.CONTENT_URI, columns, where, params, null);
while (cursor.moveToNext()) { /* So on....*/
Run Code Online (Sandbox Code Playgroud)
当params中的电话号码格式化为"777 777 7777"时,我没有记录.当它是"777-777-7777"时,它会得到记录.更糟糕的是,TelephonyManager.EXTRA_INCOMING_NUMBER给我的号码为"7777777777".如何查询此电话号码?文档说Phone.NUMBER是用户输入它的方式.好吧,这对我没有任何好处!
我rawQuery在数据库类中使用,并将光标返回到我的适配器中,该适配器CursorAdapter与自定义ListView项一起使用。
在屏幕上绘制视图后,此光标会自动关闭还是如何管理此光标?在这种情况下,最佳做法是什么?
如果关闭DB类中的此光标,则无法从适配器访问它们。
感谢您的时间和精力为我提供帮助。
编辑以添加一些代码片段以更好地理解
这是我的活动代码:
calAdapter = new CalendarListAdapter(context, dbHelper.getAllCalendars());
drawerList.setAdapter(calAdapter);
Run Code Online (Sandbox Code Playgroud)
这是我的光标
public Cursor getAllCalendars() {
String query = "SELECT calendarId as _id, calendarName, calState, calShow FROM "
+ TABLE_CALENDAR_LIST;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
Run Code Online (Sandbox Code Playgroud) 问题是有一个edittext我想设置它默认有一个光标就可以了(但没有焦点和隐藏键盘)
尝试
android:textCursorDrawable="@null"
android:cursorVisible="true"
<EditText
android:id="@+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="@drawable/comment"
android:hint="@string/cmt"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#AAACAD"
android:textColorHint="#AAACAD"
android:textCursorDrawable="@null"
android:cursorVisible="true"
android:textSize="15sp" />
Run Code Online (Sandbox Code Playgroud)
但不行.请帮忙.非常感谢
我的查询是 SELECT DefaultId FROM tblsample where ('567' = DefaultId) || ('567' = Name)
在上面的查询中
table name = tblsample
Column Names = DefaultId , Name
Input Value = 567
Run Code Online (Sandbox Code Playgroud)
现在我想检查这个值在表中的哪一列中可用并返回它的DefaultId。我能够在 Sqlite 中实现这一点,想知道是否还有更好的方法来优化查询和在 android 中使用
database.query(boolean distinct, String table, String[] columns,String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
Run Code Online (Sandbox Code Playgroud)
我必须在android中使用哪个查询。