private static final int USER_ID=1;
private static final int DT=2;
private static final int VALUES_ALL=3;
private static final UriMatcher sUriMacher= new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMacher.addURI("gr.hua.android.contentproviderapp","COORDINATES",VALUES_ALL);
sUriMacher.addURI("gr.hua.android.contentproviderapp","COORDINATES/#",USER_ID);
sUriMacher.addURI("gr.hua.android.contentproviderapp","COORDINATES/#",DT);
}
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = new DBHelper(getContext()).getReadableDatabase();
switch (sUriMacher.match(uri)){
case USER_ID:
String[] selargs =new String[1];
selargs[0]=uri.getLastPathSegment(); //the user_Id that i got
String user=DBHelper.USER_ID;
String[] coll =new String[1]; //the collums that i want to select
coll[0]=DBHelper.KEY_LATITUDE;
coll[1]=DBHelper.KEY_LONGITUDE;
return db.query(DBHelper.TABLE_NAME,coll,user+"=?",selargs,null,null,null);
case DT:
String[] selargs2 =new String[1];
selargs2[0]=uri.getLastPathSegment();
String[] coll2 =new String[1];
coll2[0]=DBHelper.KEY_LATITUDE;
coll2[1]=DBHelper.KEY_LONGITUDE;
return db.query(DBHelper.TABLE_NAME,coll2,"_DT=?",selargs2,null,null,null); //the same as the user_id but we get the dt here
case VALUES_ALL:
return db.query(DBHelper.TABLE_NAME,null,null,null,null,null,null);
default:
}
return null;
}
有我的内容提供商,我在这里执行插入方法
Uri result =Uri.withAppendedPath(url,get3);
Cursor cursor =resolver.query(result,null,null,null,null);
if (cursor!=null && cursor.getCount()>0){ //i got null here
StringBuilder stringBuilder =new StringBuilder("");
while (cursor.moveToNext())
stringBuilder.append(cursor.getString(0) + " " + cursor.getString(1) + "");
}
get.setText(stringBuilder.toString());
}else{
get.setText("This user not found");
上面的代码是我想从edittext(get3)获取的内容解析器,它是一个String类型的user_id,并将其作为LastPathSegment放在url路径中但是游标重新返回null总是 如果我发送做选择的网址所有的工作 我在网上搜索我做了一些改变,但是当我调试时,我没有得到Cursor = null