package com.example.patrice.content_provider02;

import android.os.Bundle;
import android.net.Uri;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

//https://www.tutorialspoint.com/android/android_content_providers.htm
// http://www.techotopia.com/index.php/An_Android_Studio_Content_Provider_Example

// http://www.vogella.com/tutorials/AndroidSQLite/article.html

// https://www.sitepoint.com/create-your-own-content-provider-in-android/

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClickAddName(View view)
    {
        // Add a new student record
        ContentValues values = new ContentValues();
        values.put(StudentsProvider.NAME, ((EditText)findViewById(R.id.editText2)).getText().toString());
        values.put(StudentsProvider.GRADE,((EditText)findViewById(R.id.editText3)).getText().toString());

        Uri uri = getContentResolver().insert( StudentsProvider.CONTENT_URI, values);

        Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show();
    }
    public void onClickRetrieveStudents(View view)
    {
        // Retrieve student records
        String URL = "content://com.example.patrice.content_provider02.StudentsProvider";

        Uri students = Uri.parse(URL);
        Cursor c = managedQuery(students, null, null, null, "name");

        if (c.moveToFirst())
        {
            do{
                Toast.makeText(this,  c.getString(c.getColumnIndex(StudentsProvider._ID)) +
                                    ", " +  c.getString(c.getColumnIndex( StudentsProvider.NAME)) +
                                    ", " + c.getString(c.getColumnIndex( StudentsProvider.GRADE)),  Toast.LENGTH_SHORT).show();
            } while (c.moveToNext());
        }
    }
}