package com.project3.
app;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Databasehelper mydb;
EditText ed1, ed2, ed3;
Button btn,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb= new Databasehelper(this);
ed1=(EditText)findViewById(R.id.editText3);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText);
btn=(Button) findViewById(R.id.button);
btn2=(Button) findViewById(R.id.button2);
adddata();
viewdata();
}
public void adddata(){
btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean insertdata = mydb.insertdata(ed1.getText().toString(),
ed2.getText().toString(), ed3.getText().toString());
if (insertdata = true)
Toast.makeText(MainActivity.this, "data inserted",
Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "data not inserted",
Toast.LENGTH_LONG).show();
}
}
);
}
public void viewdata(){
btn2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor cursor= mydb.getdata();
if(cursor.getColumnCount()==0){
//message: there is nothing to show
showmessage("Error","Nothing found");
}
else {
StringBuffer buffer = new StringBuffer();
// there is something to show
while(cursor.moveToNext()) {
buffer.append("id:"+cursor.getString(0)+"\n");
buffer.append("name:"+cursor.getString(1)+"\n");
buffer.append("surname:"+cursor.getString(2)+"\n");
buffer.append("marks:"+cursor.getString(3)+"\n");
showmessage("Data found", buffer.toString() );
}
}
}
}
);
public void showmessage (String tittle, String message){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(tittle);
builder.setMessage(message);
builder.show();