===关于我===
我是Android开发新手,几乎无法“说”Java。不过我在PHP5和C ++ C ++ / CLI(.net> 2.0)方面经验丰富,所以我希望我能够创建这个小应用程序,这在我的脑海里。
但经过几天的实验,我现在陷入了一种陷阱,希望你能帮助我。
===关于项目===
我有一个listview(嵌套在其他布局中)并尝试使用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吗?
答案 0 :(得分:1)
你的UriMatcher不知道你的CONTENT_URI。 CONTENT_URI中有DATAPATH,但匹配器中有BUDGETS_TABLE_NAME。