can't take string data from dialogfragment to sql database due to unresolved string
I want to have a dialogfragment take the contact name and number from the user and save it in an sqLite database that I can use to show as a cardview in the main activity. I tried following this tutorial on how to have multiple column entries and add them to your database.
This is the code for the addcontact dialogfragment
package com.example.rakshakmk1;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class Dialog_AddContact extends DialogFragment {
DatabaseHelper myDB;
@Override
public void onStart() {
super.onStart();
// safety check
if (getDialog() == null) {
return;
}
// set the animations to use on showing and hiding the dialog
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
// alternative way of doing it
//getDialog().getWindow().getAttributes().
// windowAnimations = R.style.dialog_animation_fade;
// ... other stuff you want to do in your onStart() method
}
private static final String TAG = "Dialog_AddContact";
public Dialog_AddContact() {
}
public void AddData(String newName,String newNumber){
boolean insertData = myDB.addData(newName,newNumber);
if(insertData==true){
Toast.makeText(getContext(),"Contact Added Successfully",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getContext(),"Contact not Added",Toast.LENGTH_SHORT).show();
}
}
public Dialog_AddContact(View source) {
this.source = source;
}
//widgets
private EditText etName,etPhonenumber ;
private Button btnEnterContact, btnCancel1;
private View source;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_addcontact,container,false);
Window window = getDialog().getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 50;
params.y = 1000;
window.setAttributes(params);
Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y));
//for rounded corners
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
btnCancel1 = view.findViewById(R.id.btnCancel1);
btnEnterContact = view.findViewById(R.id.btnEnterContact);
etName = view.findViewById(R.id.etName);
etPhonenumber = view.findViewById(R.id.etNumber);
btnCancel1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Closing Dialog");
getDialog().dismiss();
}
});
btnEnterContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Entering Contact");
if(etName.length() == 0)
{
getDialog().dismiss();
Toast.makeText(getContext(),"Invalid Name",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getContext(),"Contact Added",Toast.LENGTH_SHORT).show();
String name = etName.getText().toString();
getDialog().dismiss();
}
if(etPhonenumber.length() == 0)
{
getDialog().dismiss();
Toast.makeText(getContext(),"Invalid Number",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getContext(),"Contact Added",Toast.LENGTH_SHORT).show();
String number = etPhonenumber.getText().toString();
getDialog().dismiss();
}
if(etName.length()!=0 && etPhonenumber.length()!=0){
AddData(newName,newNumber);
}
else {
Toast.makeText(getContext(),"Invalid Entries",Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
and this is the code for the sqLite databasehelper
package com.example.rakshakmk1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import static android.content.ContentValues.TAG;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "contactlist.db";
public static final String TABLE_NAME = "contactlist_data";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "NUMBER";
public DatabaseHelper(@Nullable Context context){super(context,DATABASE_NAME,null,1);}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"NAME TEXT,NUMBER TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,"Upgrading database from version"+oldVersion+"to"+ newVersion + ",which will destroy all old data");
//db.execSQL("DROP_IF_TABLE_EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean addData(String newName,String newNumber) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, newName);
contentValues.put(COL3,newNumber);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
}
Currently I get this error even though I have defined it in the databasehelper
I can't seem to figure out the reason why I can't get those string values from the fragment while it seems to work perfectly in the tutorial video.
Any and all help will be really appreciated
Variables newName and newNumber do not exist.
I believe that you want:-
if(etName.length()!=0 && etPhonenumber.length()!=0){
AddData(etName.getText().toString(),etPhonenumber.getText().toString());
}