===關於我===Android:創建有效的內容提供商URI的問題
我是新來的android開發人員,幾乎不能「說出」Java。不過,我在PHP5和C++ C++/CLI(.net> 2.0)方面經驗豐富,所以我希望我能夠創建這個小應用程序,這是我的想法。
但經過幾天的試驗,我現在處於一種陷阱,希望你能幫助我。
===有關項目===
我有一個列表視圖(嵌套在其他佈局內),並嘗試從一個SQLite數據來填充它。我設法使用一個簡單的「arrayAdapter」來啓動並運行,但是研究如果我使用彈出窗口在數據庫中插入新元素,我可以更新它,但我發現需要實現自己的內容提供者。
===這個問題===
我瀏覽了幾個教程和這樣做,但現在我有稱這是因爲URI的問題。我不明白它應該如何創建,所以我不能創建一個有效的。
這裏是代碼的問題:
TBTTManageBudgetsActivity.java
package com.TBTT;
// ...
// some imports
// ...
public class TBTTManageBudgetsActivity extends Activity {
// ...
// some declarations
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// Initialisation
super .onCreate(savedInstanceState);
setContentView(R.layout.manage_budgets);
// ...
// some stuff concerning other elements
// ...
// Fill List View
// Get the list view
ListView listView = (ListView) findViewById(R.id.list_box);
// Get content provider and cursor
String[] projection = { TBTCPHBudget.KEY_ROWID, TBTCPHBudget.KEY_NAME };
String[] uiBindFrom = { TBTCPHBudget.KEY_ROWID};
int[] uiBindTo = { R.id.name };
TBTCPBdugets ContentProvider = new TBTCPBdugets();
Cursor cursor = ContentProvider.query(TBTCPBdugets.CONTENT_URI, projection,null, null, null);
// Let activity manage the cursor
startManagingCursor(cursor);
// Get value from content provider
cursor.moveToFirst();
ArrayList list = new ArrayList<String>();
do {
list.add(cursor.getString(1));
} while (cursor.moveToNext());
// Set Values with Adapter
CursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_budgets_item, cursor, uiBindFrom, uiBindTo);
listView.setAdapter(adapter);
}
// ...
// some other functions
// ...
}
TBTCPBdugets.java
package com.TBTT;
// ...
// some imports
// ...
public class TBTCPBdugets extends ContentProvider {
// ...
// some declarations
// ...
public static final String AUTHORITY = "com.TBTT";
public static final String DATAPATH = "TBTCPHBudget";
public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY+"/" + DATAPATH);
private static final UriMatcher sUriMatcher;
private static class DatabaseHelper extends SQLiteOpenHelper {
// ...
// some other functions
// ...
}
private DatabaseHelper dbHelper;
// ...
// some other functions
// ...
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
switch (sUriMatcher.match(uri)) {
case BUDGETS:
qb.setTables(BUDGETS_TABLE_NAME);
qb.setProjectionMap(notesProjectionMap);
break;
default:
Log.d(TAG, "Unknown URI " + uri+" with URIMatcher Result "+sUriMatcher.match(uri));
throw new IllegalArgumentException("Unknown URI " + uri);
}
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AUTHORITY, BUDGETS_TABLE_NAME, BUDGETS);
// ...
// some declarations
// ...
}
}
當我運行這一點,打開TBTTManageBudgetsActivity程序會崩潰。來自TBTCPBdugets.java的調試輸出顯示:「未知的URI內容://com.TBTT/TBTCPBdugets with URIMatcher Result -1」。
有人可以告訴我,請問如何爲此創建VALID URI?
與荷馬辛普森說:「哦!」 這就是問題所在。非常感謝! – HOK