From so much research I realized that the fragment
do not work the relation with the database SQLite
as do the activity
directly, that this could help me to work or what is the correct code line for to be able to work with the Database from fragment
SQLite.class (where I have the database only 2 tables)
package company.viral.organizadorjec;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by erny on 27/10/2016.
*/
public class SQLite extends SQLiteOpenHelper {
//constructor.......
public SQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//aqui se crea la tabla...
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table usuarios (id_usuario integer primary key autoincrement, " +
"usuario text, clave text)");
db.execSQL("create table profesores (id_profesor integer primary key autoincrement," +
"nombre_profesor text, comentario_profesor text");
db.execSQL("insert into usuarios values('0','admin','admin')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("create table usuarios (id_usuario integer primary key autoincrement, " +
"usuario text, clave text)");
db.execSQL("create table profesores (id_profesor integer primary key autoincrement," +
"nombre_profesor text, comentario_profesor text");
db.execSQL("insert into usuarios values('0','admin','admin')");
}
}
MenuCentral (is the activity where the fragment
works which is a Navigation Drawer
)
package company.viral.organizadorjec;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import company.viral.organizadorjec.Fragment.AjustesF;
import company.viral.organizadorjec.Fragment.CalendarioF;
import company.viral.organizadorjec.Fragment.ConfiguracionActividadF;
import company.viral.organizadorjec.Fragment.ConfiguracionMateriaF;
import company.viral.organizadorjec.Fragment.ConfiguracionPeriodoF;
import company.viral.organizadorjec.Fragment.ConfiguracionProfesorF;
import company.viral.organizadorjec.Fragment.InicioF;
import company.viral.organizadorjec.Fragment.PerfilF;
import company.viral.organizadorjec.Fragment.ProfesoresF;
public class MenuCentral extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private PopupWindow popupadicion;
private DrawerLayout posicionpopup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_central);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
posicionpopup = (DrawerLayout) findViewById(R.id.drawer_layout);
//colocamos el fragment con que inicia el menu
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();
//este es el apartado para el botonsito flotante
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
//metodo de escucha para el popup
@Override
public void onClick(View view) {
//implementamos el popup
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View vistaadicion = inflater.inflate(R.layout.activity_pop_adicion,null);
popupadicion = new PopupWindow(
vistaadicion, RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
//luego de clicear y abrir el popup le decimos...
//si das al profe ve a profe
LinearLayout btnprofe = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarprofesor);
btnprofe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionProfesorF()).commit();
popupadicion.dismiss();
}
});
//si le das actividad ve actividad
LinearLayout btnactividad = (LinearLayout) vistaadicion.findViewById(R.id.btnagregaractividad);
btnactividad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionActividadF()).commit();
popupadicion.dismiss();
}
});
//si le das a materias ve a materias
LinearLayout btnmaterias = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarmateria);
btnmaterias.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionMateriaF()).commit();
popupadicion.dismiss();
}
});
//si le das a periodo ve a periodo
LinearLayout btnperiodo = (LinearLayout) vistaadicion.findViewById(R.id.btnagregarperiodo);
btnperiodo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionPeriodoF()).commit();
popupadicion.dismiss();
}
});
//luego le decimos que cierre el popup con el boton
Button cerrarboton = (Button) vistaadicion.findViewById(R.id.btnpopupcerrar);
cerrarboton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupadicion.dismiss();
}
});
//hubicamos donde queremos el popup
popupadicion.showAtLocation(posicionpopup, Gravity.CENTER,0,0 );
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_central, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
if (id == R.id.nav_camera) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new InicioF()).commit();
} else if (id == R.id.nav_gallery) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new ProfesoresF()).commit();
} else if (id == R.id.nav_slideshow) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new CalendarioF()).commit();
} else if (id == R.id.nav_manage) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new AjustesF()).commit();
} else if (id == R.id.nav_share) {
fragmentManager.beginTransaction().replace(R.id.contenedor,new PerfilF()).commit();
} else if (id == R.id.nav_send) {
} else if (id == R.id.nav_materia){
fragmentManager.beginTransaction().replace(R.id.contenedor,new ConfiguracionMateriaF()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
ProffesorF configuration ( fragment
involved in the problem)
package company.viral.organizadorjec.Fragment;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import company.viral.organizadorjec.R;
import company.viral.organizadorjec.SQLite;
public class ConfiguracionProfesorF extends Fragment {
private EditText etnombreprofesorf;
private EditText etcomentarioprofesorf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_configuracion_profesor, container, false);
etnombreprofesorf = (EditText)view.findViewById(R.id.etnombreprofesor);
etcomentarioprofesorf = (EditText)view.findViewById(R.id.etcomentarioprofesor);
Button btnagrerarpf = (Button)view.findViewById(R.id.btnagregarprofesor);
btnagrerarpf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String auxnp = etnombreprofesorf.getText().toString();
String auxcp = etcomentarioprofesorf.getText().toString();
SQLite admin = new SQLite(getActivity(),"administracion",null,1);
SQLiteDatabase bd = admin.getWritableDatabase();
ContentValues registroprofe = new ContentValues();
registroprofe.put("nombre_profesor",auxnp);
registroprofe.put("comentario_profesor",auxcp);
bd.insert("profesores",null,registroprofe);
bd.close();
etnombreprofesorf.setText("");
etcomentarioprofesorf.setText("");
}
});
return view;
}
}
**fragment_configuracion_profesor** (correspondiente 'layout' de dicho 'fragment')
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="company.viral.organizadorjec.Fragment.ConfiguracionProfesorF">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Registro de Profesores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
tools:textColor="@color/colorPrimary"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<EditText
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etnombreprofesor"
android:hint="Nombre del profesor"
android:layout_width="match_parent" />
<TextView
android:text="Elija primera caracteristica:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView8" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:text="Elija segunda caracteristica:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView11" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:hint="Si lo desea ingrese comentarios del profesor"
android:id="@+id/etcomentarioprofesor" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Agregar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_weight="1"
style="@android:style/Widget.Button" />
<Button
android:text="Editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_weight="1"
style="@android:style/Widget.Button" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Thank you very much for helping me, sorry for the code so noob, but keep in mind that I'm starting and is "self-taught" if they have exercises to learn how to reduce code of truth I would really appreciate it ... finally many Thanks for your time !!!