1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
package com.example.hanoun.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
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 java.sql.SQLException;
/**
* Created by Hanoun on 06/07/2015.
*/
public class client extends Fragment {
private Context context ;
private EditText tf1,tf2,tf3,tf4,tf5,tf6;
private String str1,str2,str3,str4,str5,str6;
private View v ;
private Button button;
private SQLiteDatabase mDataBase;
private DataBaseHelper MyDB ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v= inflater.inflate(R.layout.client_layout,container,false);
tf1 = (EditText) v.findViewById(R.id.tfName);
tf2 = (EditText) v.findViewById(R.id.tfAdr);
tf3 = (EditText) v.findViewById(R.id.tfCAP);
tf4 = (EditText) v.findViewById(R.id.tfVille);
tf5 = (EditText) v.findViewById(R.id.tfProv);
tf6 = (EditText) v.findViewById(R.id.tfCountry);
str1= tf1.getText().toString() ;
str2=tf2.getText().toString() ;
str3= tf3.getText().toString() ;
str4=tf4.getText().toString() ;
str5= tf5.getText().toString() ;
str6=tf6.getText().toString();
button= (Button) v.findViewById(R.id.addBtn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
AddData();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
return v ;
}
public String getStr1() {
return str1;
}
public String getStr2() {
return str2;
}
public String getStr3() {
return str3;
}
public String getStr4() {
return str4;
}
public String getStr5() {
return str5;
}
public String getStr6() {
return str6;
}
public void AddData () throws SQLException
{
MyDB = new DataBaseHelper(v.getContext());
try {
MyDB.createDataBase();
}
catch (Exception e){ System.out.println(e);}
try {
MyDB.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase db = MyDB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Name", str1 );
values.put("Address", str2 );
values.put("CAP", str3 );
values.put("City", str4 );
values.put("Province",str5 );
values.put("Country", str6 );
// Inserting Row
db.insert("Clients", null, values);
db.close(); // Closing database connection
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
} |
Partager