Practical 19
Practical 19
android:allowBackup="true"
Activity_main.xml
android:dataExtractionRules="@xml/data_extraction_ru
<?xml version="1.0" encoding="utf-8"?> les"
<LinearLayout android:fullBackupContent="@xml/backup_rules"
xmlns:android="http://schemas.android.com/apk/res/an android:icon="@mipmap/ic_launcher"
droid" android:label="@string/app_name"
android:layout_width="match_parent" android:roundIcon="@mipmap/ic_launcher_round"
android:layout_height="match_parent" android:supportsRtl="true"
android:orientation="vertical" android:theme="@style/Theme.Pr19_e1"
android:padding="16dp"> tools:targetApi="31">
<TextView <activity
android:id="@+id/textView" android:name=".MainActivity"
android:layout_width="match_parent" android:exported="true">
android:layout_height="wrap_content" <intent-filter>
android:text="Data will appear here" <action
android:textSize="18sp" android:name="android.intent.action.MAIN" />
android:textStyle="bold" <category
android:padding="8dp" android:name="android.intent.category.LAUNCHER" />
android:background="#E0E0E0" </intent-filter>
android:gravity="center"/> </activity>
</LinearLayout>
DataBaseHelper.java
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> package com.example.pr19_e1;
<manifest
xmlns:android="http://schemas.android.com/apk/res/an import android.content.Context;
droid" import android.database.sqlite.SQLiteDatabase;
xmlns:tools="http://schemas.android.com/tools"> import android.database.sqlite.SQLiteOpenHelper;
@Override @Override
public int delete(Uri uri, String selection, String[] public String getType(Uri uri) {
selectionArgs) { return null;
int count; }
switch (uriMatcher.match(uri)) { }
case USERS:
count =
database.delete(DatabaseHelper.TABLE_NAME,
selection, selectionArgs); MainAcitivity.java
break;
package com.example.pr19_e1;
case USER_ID:
selection = DatabaseHelper.COLUMN_ID + "=?";
import android.content.ContentValues;
selectionArgs = new
import android.database.Cursor;
String[]{uri.getLastPathSegment()};
import android.net.Uri;
count =
import android.os.Bundle;
database.delete(DatabaseHelper.TABLE_NAME,
import android.view.View;
selection, selectionArgs);
import android.widget.Button;
break;
import android.widget.TextView;
default:
import androidx.appcompat.app.AppCompatActivity;
throw new
IllegalArgumentException("Unknown URI: " + uri);
public class MainActivity extends AppCompatActivity {
}
TextView textView;
getContext().getContentResolver().notifyChange(uri,
Button btnInsert, btnFetch;
null);
return count;
@Override
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
setContentView(R.layout.activity_main);
public int update(Uri uri, ContentValues values, String
selection, String[] selectionArgs) {
textView = findViewById(R.id.textView);
int count;
btnInsert = findViewById(R.id.btnInsert);
switch (uriMatcher.match(uri)) {
btnFetch = findViewById(R.id.btnFetch);
case USERS:
count =
// Insert Data on Button Click
database.update(DatabaseHelper.TABLE_NAME, values,
btnInsert.setOnClickListener(new
selection, selectionArgs);
View.OnClickListener() {
break;
@Override
case USER_ID:
public void onClick(View v) {
selection = DatabaseHelper.COLUMN_ID + "=?";
insertData("Alice", 25);
}
});
getContentResolver().insert(MyContentProvider.CONTEN
T_URI, values);
}
if (cursor != null) {
while (cursor.moveToNext()) {
int id =
cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseH
elper.COLUMN_ID));
String name =
cursor.getString(cursor.getColumnIndexOrThrow(Databa
seHelper.COLUMN_NAME));
int age =
cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseH
elper.COLUMN_AGE));
result.append("ID: ").append(id).append(",
Name: ").append(name).append(", Age:
").append(age).append("\n");
}
cursor.close();
}
textView.setText(result.toString());
}
}