StartActivity Class
public class StartActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
FloatingActionButton newContact = (FloatingActionButton)findViewById(R.id.add_new);
newContact.setOnClickListener(this);
String n = DataProvider.mTable1Uri;
Uri un = Uri.parse(n);
Cursor c = managedQuery(un, null, null, null, null);
if (c.moveToFirst()) {
do {
Toast.makeText(this,c.getString(c.getColumnIndex(table0coloum.mFirstName)),Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.add_new :
Intent addN = new Intent(StartActivity.this,MainActivity.class);
startActivity(addN);
break;
}
}
}
MainActivity Class
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText firstName,middleName,lastName,phone,email,address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilize();
FloatingActionButton save = (FloatingActionButton)findViewById(R.id.save_save);
save.setOnClickListener(this);
}
private void initilize() {
firstName = (EditText)findViewById(R.id.save_firstName);
middleName = (EditText)findViewById(R.id.save_middleName);
lastName = (EditText)findViewById(R.id.save_lastName);
phone = (EditText)findViewById(R.id.save_phoneNo);
email = (EditText)findViewById(R.id.save_Email);
address = (EditText)findViewById(R.id.save_address);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id)
{
case R.id.save_save :
saveToTable();
Toast.makeText(this,"Save",Toast.LENGTH_SHORT).show();
break;
}
}
private void saveToTable() {
ContentValues val =new ContentValues();
if(!firstName.getText().toString().isEmpty())
{
val.put(table0coloum.mFirstName,firstName.getText().toString());
}
if(!middleName.getText().toString().isEmpty())
{
val.put(table0coloum.mMiddleName,middleName.getText().toString());
}
if(!lastName.getText().toString().isEmpty())
{
val.put(table0coloum.mLastName,lastName.getText().toString());
}
if(!phone.getText().toString().isEmpty())
{
val.put(table0coloum.mPhoneNo,phone.getText().toString());
}
if(!email.getText().toString().isEmpty())
{
val.put(table0coloum.mEmail,email.getText().toString());
}
if(!address.getText().toString().isEmpty())
{
val.put(table0coloum.mAddress,address.getText().toString());
}
Uri uri = getContentResolver().insert(DataProvider.mContentUri, val);
clearFileds();
Toast.makeText(getBaseContext(),uri.toString(), Toast.LENGTH_LONG).show();
}
private void clearFileds() {
firstName.setText("");
middleName.setText("");
lastName.setText("");
phone.setText("");
email.setText("");
address.setText("");
}
}
DataProvider Class
public class DataProvider extends ContentProvider{
public static final String mBaseAuthority = "com.nexus.nsnik.contentsave";
public static final String mBaseUri = "content://"+mBaseAuthority;
public static final String mTable1Uri = mBaseUri+"/"+Table.mTableName;
public static final Uri mContentUri = Uri.parse(mTable1Uri);
private static final int mFirstLastName = 1;
private static final int mAllData = 2;
private static HashMap<String, String> mContactProjectionMap;
private SQLiteDatabase sbt;
public static final UriMatcher uriMatch ;
static {
uriMatch = new UriMatcher(UriMatcher.NO_MATCH);
uriMatch.addURI(mTable1Uri,Table.mTableName+"/#",mFirstLastName);
uriMatch.addURI(mTable1Uri,Table.mTableName,mAllData);
}
public static class Databse extends SQLiteOpenHelper{
private static final String mDataBaseName = "contactdb";
private static final int mDataBaseVersion = 2;
private static final String mDropTable = "DROP TABLE IF EXISTS " + Table.mTableName;
public Databse(Context context) {
super(context, mDataBaseName, null, mDataBaseVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
String table = "CREATE TABLE "+ Table.mTableName + " ("
+ table0coloum.mUid + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ table0coloum.mFirstName + " VARCHAR(255),"
+ table0coloum.mMiddleName + " VARCHAR(255),"
+ table0coloum.mLastName + " VARCHAR(255),"
+ table0coloum.mPhoneNo + " VARCHAR(255),"
+ table0coloum.mEmail + " VARCHAR(255),"
+ table0coloum.mAddress + " VARCHAR(255)"
+");";
db.execSQL(table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(mDropTable);
onCreate(db);
}
}
@Override
public boolean onCreate() {
Databse dbt = new Databse(getContext());
sbt = dbt.getWritableDatabase();
return(sbt==null)? false:true ;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqb = new SQLiteQueryBuilder();
sqb.setTables(Table.mTableName);
switch (uriMatch.match(uri))
{
case mFirstLastName :
sqb.appendWhere( table0coloum.mUid+ "=" + uri.getPathSegments().get(1));
break;
case mAllData :
sqb.setProjectionMap(mContactProjectionMap);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == ""){
sortOrder = table0coloum.mFirstName;
}
Cursor c = sqb.query(sbt,projection,selection,selectionArgs,null,null,sortOrder);
c.setNotificationUri(getContext().getContentResolver(),uri);
return c;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatch.match(uri))
{
case mFirstLastName :
return "vnd.android.cursor.item/vdn.com.nexus.nsnik.contentsave";
case mAllData :
return "vnd.android.cursor.dir/vdn.com.nexus.nsnik.contentsave";
}
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
long insertId = sbt.insert(Table.mTableName,null,values);
if (insertId>0)
{
Uri _uri = ContentUris.withAppendedId(mContentUri, insertId);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatch.match(uri))
{
case mFirstLastName :
String id = uri.getPathSegments().get(1);
count = sbt.delete( Table.mTableName, table0coloum.mFirstName + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
case mAllData :
count = sbt.delete(Table.mTableName, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatch.match(uri))
{
case mFirstLastName :
String id = uri.getPathSegments().get(1);
count = sbt.update( Table.mTableName, values,table0coloum.mFirstName + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
case mAllData :
count = sbt.update(Table.mTableName, values,selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nexus.nsnik.contentsave">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<provider
android:authorities="com.nexus.nsnik.contentsave"
android:exported="true"
android:name=".DataProvider">
</provider>
</application>
在代码启动时出现此错误:
Java.lang.RuntimeException:无法启动活动ComponentInfo {com.nexus.nsnik.contentsave / com.nexus.nsnik.contentsave.StartActivity}:java.lang.IllegalArgumentException:未知的URI内容://com.nexus。 nsnik.contentsave /接触
这是我第一次尝试内容提供商,请原谅我提出这个问题,但是我无法理解这个错误,比如为什么它可以解析uri
答案 0 :(得分:0)
我认为您的来源包含StartActivity.this
,请尝试从中删除类名,以便左侧以this
作为参数。