Practical No 19
Practical No 19
19
#activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/phoneEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone" />
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Contact" />
<Button
android:id="@+id/queryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>
#MainActivity.java
package com.example.contentproviderdemo;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
phoneEditText = findViewById(R.id.phoneEditText);
resultTextView = findViewById(R.id.resultTextView);
insertButton.setOnClickListener(v -> {
values.put(ContactsDatabaseHelper.COLUMN_NAME, name);
values.put(ContactsDatabaseHelper.COLUMN_PHONE, phone);
});
queryButton.setOnClickListener(v -> {
do {
String name =
cursor.getString(cursor.getColumnIndex(ContactsDatabaseHelper.COLUMN_NAME));
String phone =
cursor.getString(cursor.getColumnIndex(ContactsDatabaseHelper.COLUMN_PHONE));
} while (cursor.moveToNext());
cursor.close();
resultTextView.setText(result.toString());
});
}
#output