Coding sbbu mengunci siapa yang sedang login dan ataukah aplikasi sedang dalam kondisi login
package com.mustofin.expandablenavigationdrawer; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import com.mustofin.expandablenavigationdrawer.fragment.Loginas; import java.util.HashMap; public class SessionManager { SharedPreferences pref; Editor editor; Context _context; int PRIVATE_MODE = 0; private static final String PREF_NAME = "LP2MARAY"; private static final String IS_LOGIN = "IsLoggedIn"; public static String kode_admin = ""; public static String username = ""; public static final String KEY_KODE = "kode"; public SessionManager(Context context){ this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } public void createLoginSession(String skode){ editor.putBoolean(IS_LOGIN, true); editor.putString(KEY_KODE, skode); editor.commit(); } public void checkLogin(){ if(!this.isLoggedIn()){ Intent i = new Intent(_context, Loginas.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); _context.startActivity(i); } } public HashMap<String, String> getUserDetails(){ HashMap<String, String> user = new HashMap<String, String>(); user.put(KEY_KODE, pref.getString(KEY_KODE, null)); return user; } public void logoutUser(){ editor.clear(); editor.commit(); Intent i = new Intent(_context, Loginas.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); _context.startActivity(i); } public boolean isLoggedIn(){ return pref.getBoolean(IS_LOGIN, false); } }
=========================================
Sebagai berikut Coding pada Constant : Menu draw:
package com.mustofin.expandablenavigationdrawer; import com.mustofin.expandablenavigationdrawer.fragment.About_fragment; import com.mustofin.expandablenavigationdrawer.fragment.BerandaFragment; import com.mustofin.expandablenavigationdrawer.fragment.ListMahasiswa_fragment; import com.mustofin.expandablenavigationdrawer.navigationdrawer.NavMenuModel; import java.util.ArrayList; public class Constant { public static ArrayList<NavMenuModel> getMenuNavigasi(){ ArrayList<NavMenuModel> menu = new ArrayList<>(); menu.add(new NavMenuModel("Beranda", R.drawable.home, BerandaFragment.newInstance(""))); menu.add(new NavMenuModel("Mahasiswa", R.drawable.ic_order, ListMahasiswa_fragment.newInstance(""))); menu.add(new NavMenuModel("Pilih Semester", R.drawable.ic_produk, new ArrayList<NavMenuModel.SubMenuModel>() {{ add(new NavMenuModel.SubMenuModel("Semester 1", R.drawable.telkomsellogo, ListMahasiswa_fragment.newInstance("Semester 1"))); add(new NavMenuModel.SubMenuModel("Semester 2", R.drawable.indosatlogo, ListMahasiswa_fragment.newInstance("Semester 2"))); add(new NavMenuModel.SubMenuModel("Semester 3", R.drawable.xl, ListMahasiswa_fragment.newInstance("Semester 3"))); }})); menu.add(new NavMenuModel("Bantuan", R.drawable.ic_order, About_fragment.newInstance(""))); menu.add(new NavMenuModel("Logout", R.drawable.exit, BerandaFragment.newInstance(""))); menu.add(new NavMenuModel("Keluar", R.drawable.exit, BerandaFragment.newInstance(""))); return menu; } }
=========================
Sebagai berikut coding LOGIN dari database dan direkam ke session:
package com.mustofin.expandablenavigationdrawer.fragment; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.mustofin.expandablenavigationdrawer.MainActivity; import com.mustofin.expandablenavigationdrawer.R; import com.mustofin.expandablenavigationdrawer.SessionManager; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class Loginas extends Activity { SessionManager session; EditText txtusername,txtpassword; String ip=""; int sukses; private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); private static final String TAG_SUKSES = "sukses"; private static final String TAG_record = "record"; String kode_admin="",nama_admin,email; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); ip=jsonParser.getIP(); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); session = new SessionManager(getApplicationContext()); txtusername=(EditText)findViewById(R.id.edUsername); txtpassword=(EditText)findViewById(R.id.edPassword); Button btnLogin= (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user=txtusername.getText().toString(); String pass=txtpassword.getText().toString(); if(user.length()<1){lengkapi("Username");} else if(pass.length()<1){lengkapi("Password");} else{ new ceklogin().execute(); } } }); } public void gagal(){ new AlertDialog.Builder(this) .setTitle("Gagal Login") .setMessage("Silakan Cek Account Anda Kembali") .setNeutralButton("Tutup", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { }}) .show(); } class ceklogin extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Loginas.this); pDialog.setMessage("Proses Login..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected String doInBackground(String... params) { try { String username=txtusername.getText().toString().trim(); String password=txtpassword.getText().toString().trim(); List<NameValuePair> myparams = new ArrayList<NameValuePair>(); myparams.add(new BasicNameValuePair("username", username)); myparams.add(new BasicNameValuePair("password", password)); String url=ip+"admin/admin_login.php"; Log.v("detail",url); JSONObject json = jsonParser.makeHttpRequest(url, "GET", myparams); Log.d("detail", json.toString()); sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array final JSONObject myJSON = myObj.getJSONObject(0); runOnUiThread(new Runnable() { public void run() { try { kode_admin=myJSON.getString("kode_admin"); nama_admin=myJSON.getString("nama_admin"); email=myJSON.getString("email"); } catch (JSONException e) {e.printStackTrace();} }}); } } catch (JSONException e) { e.printStackTrace(); } return null; } @SuppressLint("NewApi") protected void onPostExecute(String file_url) { pDialog.dismiss(); Log.v("SUKSES",kode_admin); if(sukses==1){ session.createLoginSession(kode_admin); Intent i = new Intent(getApplicationContext(),MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); finish(); } else{ gagal("Login"); } } } public void lengkapi(String item){ new AlertDialog.Builder(this) .setTitle("Lengkapi Data") .setMessage("Silakan lengkapi data "+item) .setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { }}) .show(); } public void gagal(String item){ new AlertDialog.Builder(this) .setTitle("Gagal Login") .setMessage("Login "+item+" ,, Gagal") .setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { }}) .show(); } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); return true; } return super.onKeyDown(keyCode, event); } }
MainActivity Code:
package com.mustofin.expandablenavigationdrawer; import android.content.DialogInterface; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.MenuItem; import android.widget.Toast; import com.mustofin.expandablenavigationdrawer.fragment.BerandaFragment; import com.mustofin.expandablenavigationdrawer.navigationdrawer.NavMenuAdapter; import com.mustofin.expandablenavigationdrawer.navigationdrawer.NavMenuModel; import com.mustofin.expandablenavigationdrawer.navigationdrawer.SubTitle; import com.mustofin.expandablenavigationdrawer.navigationdrawer.TitleMenu; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements NavMenuAdapter.MenuItemClickListener{ SessionManager session; Toolbar toolbar; DrawerLayout drawer; ArrayList<NavMenuModel> menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); session = new SessionManager(getApplicationContext()); session.checkLogin(); setToolbar(); drawer = (DrawerLayout) findViewById(R.id.main_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); setNavigationDrawerMenu(); } private void setToolbar() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } private void setNavigationDrawerMenu() { NavMenuAdapter adapter = new NavMenuAdapter(this, getMenuList(), this); RecyclerView navMenuDrawer = (RecyclerView) findViewById(R.id.main_nav_menu_recyclerview); navMenuDrawer.setAdapter(adapter); navMenuDrawer.setLayoutManager(new LinearLayoutManager(this)); navMenuDrawer.setAdapter(adapter); adapter.selectedItemParent = menu.get(0).menuTitle; onMenuItemClick(adapter.selectedItemParent); adapter.notifyDataSetChanged(); } private List<TitleMenu> getMenuList() { List<TitleMenu> list = new ArrayList<>(); menu = Constant.getMenuNavigasi(); for (int i = 0; i < menu.size(); i++) { ArrayList<SubTitle> subMenu = new ArrayList<>(); if (menu.get(i).subMenu.size() > 0){ for (int j = 0; j < menu.get(i).subMenu.size(); j++) { subMenu.add(new SubTitle(menu.get(i).subMenu.get(j).subMenuTitle)); } } list.add(new TitleMenu(menu.get(i).menuTitle, subMenu, menu.get(i).menuIconDrawable)); } return list; } @Override public void onMenuItemClick(String itemString) { for (int i = 0; i < menu.size(); i++) { if (itemString.equals(menu.get(i).menuTitle)){ getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, menu.get(i).fragment) .commit(); break; } else if (itemString.equals("Logout")) { session.logoutUser(); } else if (itemString.equals("Keluar")) { keluarYN(); } else{ for (int j = 0; j < menu.get(i).subMenu.size(); j++) { if (itemString.equals(menu.get(i).subMenu.get(j).subMenuTitle)){ getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, menu.get(i).subMenu.get(j).fragment) .commit(); break; } } } } if (drawer != null){ drawer.closeDrawer(GravityCompat.START); } } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { drawer.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { BerandaFragment beranda= new BerandaFragment(); this.getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, beranda.newInstance("")) .addToBackStack(null) .commit(); return true; } return super.onKeyDown(keyCode, event); } public void keluar(){ new AlertDialog.Builder(this) .setTitle("Menutup Aplikasi") .setMessage("Terimakasih... Anda Telah Menggunakan Aplikasi Ini") .setNeutralButton("Tutup", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { finish(); }}) .show(); } public void keluarYN(){ AlertDialog.Builder ad=new AlertDialog.Builder(MainActivity.this); ad.setTitle("Konfirmasi"); ad.setMessage("Apakah benar ingin keluar?"); ad.setPositiveButton("OK",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { keluar(); }}); ad.setNegativeButton("No",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface arg0, int arg1) { }}); ad.show(); } }
===============================================================
LiatMahasiswa / DAFTAR MHS
package com.mustofin.expandablenavigationdrawer.fragment; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import com.mustofin.expandablenavigationdrawer.R; import com.mustofin.expandablenavigationdrawer.SessionManager; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ListMahasiswa_fragment extends Fragment { private String kode_user; SessionManager session; private static final String ARG_PARAM = ""; String paramText; String ip=""; ListView lv; private ProgressDialog pDialog; JSONParser jParser = new JSONParser(); JSONArray myJSON = null; ArrayList<HashMap<String, String>> arrayList; private static final String TAG_SUKSES = "sukses"; private static final String TAG_record = "record"; private static final String TAG_kode_mahasiswa = "kode_mahasiswa"; private static final String TAG_email = "email"; private static final String TAG_nama_mahasiswa = "nama"; public static ListMahasiswa_fragment newInstance(String param) { ListMahasiswa_fragment fragment = new ListMahasiswa_fragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM, param); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.mahasiswa_list, container, false);; lv = (ListView) view.findViewById(R.id.listjson); paramText = getArguments().getString(ARG_PARAM);//paramText dari halaman sebelumnya lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { String pk = ((TextView) view.findViewById(R.id.kode_k)).getText().toString(); Detail_mahasiswafragment detailmahasiswafragment= new Detail_mahasiswafragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, detailmahasiswafragment.newInstance(pk)) .addToBackStack(null) .commit(); }}); arrayList = new ArrayList<HashMap<String, String>>(); ip=jParser.getIP(); new ListMahasiswa_fragment.load().execute(); return view; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 100) {// jika result code 100 Intent intent = getActivity().getIntent(); getActivity().finish(); startActivity(intent); } } class load extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Load data. Silahkan Tunggu..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } protected String doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); JSONObject json = jParser.makeHttpRequest(ip+"mahasiswa/mahasiswa_show.php", "GET", params); Log.d("show: ", json.toString()); try { int sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { myJSON = json.getJSONArray(TAG_record); for (int i = 0; i < myJSON.length(); i++) { JSONObject c = myJSON.getJSONObject(i); String kode_mahasiswa= c.getString(TAG_kode_mahasiswa); String email= c.getString(TAG_email); String nama_mahasiswa = c.getString(TAG_nama_mahasiswa); HashMap<String, String> map = new HashMap<String, String>(); map.put(TAG_kode_mahasiswa, kode_mahasiswa); map.put(TAG_nama_mahasiswa, nama_mahasiswa); map.put(TAG_email, email); arrayList.add(map); } } else { Detail_mahasiswafragment detailmahasiswafragment= new Detail_mahasiswafragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, detailmahasiswafragment.newInstance("")) .addToBackStack(null) .commit(); } } catch (JSONException e) {e.printStackTrace();} return null; } protected void onPostExecute(String file_url) { pDialog.dismiss(); getActivity().runOnUiThread(new Runnable() { public void run() { ListAdapter adapter = new SimpleAdapter(getActivity(), arrayList,R.layout.desain_list, new String[] { TAG_kode_mahasiswa,TAG_nama_mahasiswa,TAG_email},new int[] { R.id.kode_k, R.id.txtNamalkp, R.id.txtDeskripsilkp}); lv.setAdapter(adapter); } });} } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 0, "Add New").setIcon(R.drawable.add); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Detail_mahasiswafragment detailmahasiswafragment= new Detail_mahasiswafragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, detailmahasiswafragment.newInstance("")) .addToBackStack(null) .commit(); return true; } return false; } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { getActivity().onBackPressed(); return true; } return super.getActivity().onKeyDown(keyCode, event); } }Detail Mahasiswa:
package com.mustofin.expandablenavigationdrawer.fragment; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import com.mustofin.expandablenavigationdrawer.R; import com.mustofin.expandablenavigationdrawer.SessionManager; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Set; public class Detail_mahasiswafragment extends Fragment { private String paramText; SessionManager session; private static final String ARG_PARAM1 = ""; String ip=""; String kode_mahasiswa; EditText txtKode_mahasiswa; EditText txtnama_mahasiswa; EditText txttelepon; EditText txtemail; EditText txtketerangan; EditText txtstatus; Button btnProses; Button btnHapus; private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); private static final String TAG_SUKSES = "sukses"; private static final String TAG_record = "record"; private static final String TAG_nama_mahasiswa = "nama"; private static final String TAG_telepon = "telepon"; private static final String TAG_email = "email"; private static final String TAG_keterangan = "keterangan"; private static final String TAG_status = "status"; public Detail_mahasiswafragment newInstance(String param1) { Detail_mahasiswafragment fragment = new Detail_mahasiswafragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { paramText = getArguments().getString(ARG_PARAM1); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.mhs_detail, container, false);; session = new SessionManager(getActivity()); session.getUserDetails(); HashMap map=session.getUserDetails(); Set keys = map.keySet(); Iterator itr = keys.iterator(); String KODE=""; while(itr.hasNext()) { String key = (String)itr.next(); KODE= (String)map.get(key); } Log.v("BACA",KODE+"#=paramText="+paramText+"#"); ip=jsonParser.getIP(); txtKode_mahasiswa = (EditText) view.findViewById(R.id.txtkode_mahasiswa); txtnama_mahasiswa= (EditText) view.findViewById(R.id.txtnama_mahasiswa); txtemail= (EditText) view.findViewById(R.id.txtemail); txttelepon= (EditText) view.findViewById(R.id.txttelepon); txtketerangan = (EditText) view.findViewById(R.id.txtketerangan); txtstatus= (EditText) view.findViewById(R.id.txtstatus); btnProses= (Button) view.findViewById(R.id.btnproses); btnHapus = (Button) view.findViewById(R.id.btnhapus); kode_mahasiswa = paramText; if(kode_mahasiswa.length()>0){ new get().execute(); btnProses.setText("Update Data"); btnHapus.setVisibility(View.VISIBLE); } else{ btnProses.setText("Add New Data"); btnHapus.setVisibility(View.GONE); } btnProses.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { kode_mahasiswa= txtKode_mahasiswa.getText().toString(); String lnama_mahasiswa= txtnama_mahasiswa.getText().toString(); String lemail= txtemail.getText().toString(); String ltelepon= txttelepon.getText().toString(); String lketerangan= txtketerangan.getText().toString(); String lstatus= txtstatus.getText().toString(); if(kode_mahasiswa.length()<1){lengkapi("kode_mahasiswa");} else if(lnama_mahasiswa.length()<1){lengkapi("nama_mahasiswa");} else if(lemail.length()<1){lengkapi("email");} else if(ltelepon.length()<1){lengkapi("telpon");} else if(lketerangan.length()<1){lengkapi("keterangan");} else if(lstatus.length()<1){lengkapi("status");} else{ if(kode_mahasiswa.length()>0){ new update().execute(); } else{ new save().execute(); } }//else }}); btnHapus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { new del().execute(); // getActivity().onBackPressed(); }}); return view; } class get extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Load data detail. Silahkan tunggu..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected String doInBackground(String... params) { int sukses; try { List<NameValuePair> params1 = new ArrayList<NameValuePair>(); params1.add(new BasicNameValuePair("kode_mahasiswa", kode_mahasiswa)); String url=ip+"mahasiswa/mahasiswa_detail.php"; Log.v("detail",url); JSONObject json = jsonParser.makeHttpRequest(url, "GET", params1); Log.d("detail", json.toString()); sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array final JSONObject myJSON = myObj.getJSONObject(0); getActivity().runOnUiThread(new Runnable() { public void run() { try { txtKode_mahasiswa.setText(kode_mahasiswa); txtnama_mahasiswa.setText(myJSON.getString(TAG_nama_mahasiswa)); txttelepon.setText(myJSON.getString(TAG_telepon)); txtemail.setText(myJSON.getString(TAG_email)); txtketerangan.setText(myJSON.getString(TAG_keterangan)); txtstatus.setText(myJSON.getString(TAG_status)); } catch (JSONException e) {e.printStackTrace();} }}); } else{ // jika id tidak ditemukan } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) {pDialog.dismiss();} } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class save extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Menyimpan data ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected String doInBackground(String... args) { String lnama_mahasiswa= txtnama_mahasiswa.getText().toString(); String lemail= txtemail.getText().toString(); String ltelepon= txttelepon.getText().toString(); String lketerangan= txtketerangan.getText().toString(); String lstatus= txtstatus.getText().toString(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("kode_mahasiswa0", kode_mahasiswa)); params.add(new BasicNameValuePair("kode_mahasiswa", kode_mahasiswa)); params.add(new BasicNameValuePair("nama_mahasiswa", lnama_mahasiswa)); params.add(new BasicNameValuePair("email", lemail)); params.add(new BasicNameValuePair("telepon", ltelepon)); params.add(new BasicNameValuePair("keterangan", lketerangan)); params.add(new BasicNameValuePair("status", lstatus)); String url=ip+"mahasiswa/mahasiswa_add.php"; Log.v("add",url); JSONObject json = jsonParser.makeHttpRequest(url,"POST", params); Log.d("add", json.toString()); try { int sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { Intent i = getActivity().getIntent(); getActivity().setResult(100, i); // getActivity().finish(); ListMahasiswa_fragment List_mahasiswa= new ListMahasiswa_fragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, List_mahasiswa.newInstance("")) .addToBackStack(null) .commit(); } else { // gagal update data } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) {pDialog.dismiss();} } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class update extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Mengubah data ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected String doInBackground(String... args) { String lnama_mahasiswa= txtnama_mahasiswa.getText().toString(); String lemail= txtemail.getText().toString(); String ltelepon= txttelepon.getText().toString(); String lketerangan= txtketerangan.getText().toString(); String lstatus= txtstatus.getText().toString(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("kode_mahasiswa0", kode_mahasiswa)); params.add(new BasicNameValuePair("kode_mahasiswa", kode_mahasiswa)); params.add(new BasicNameValuePair("nama_mahasiswa", lnama_mahasiswa)); params.add(new BasicNameValuePair("email", lemail)); params.add(new BasicNameValuePair("telepon", ltelepon)); params.add(new BasicNameValuePair("keterangan", lketerangan)); params.add(new BasicNameValuePair("status", lstatus)); String url=ip+"mahasiswa/mahasiswa_update.php"; Log.v("update",url); JSONObject json = jsonParser.makeHttpRequest(url,"POST", params); Log.d("add", json.toString()); try { int sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { Intent i = getActivity().getIntent(); getActivity().setResult(100, i); // getActivity().finish();//pnggntiintent ListMahasiswa_fragment List_mahasiswa= new ListMahasiswa_fragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, List_mahasiswa.newInstance("")) .addToBackStack(null) .commit(); // } else { // gagal update data } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) {pDialog.dismiss();} } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ class del extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Menghapus data..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected String doInBackground(String... args) { int sukses; try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("kode_mahasiswa", kode_mahasiswa)); String url=ip+"mahasiswa/mahasiswa_del.php"; Log.v("delete",url); JSONObject json = jsonParser.makeHttpRequest(url, "GET", params); Log.d("delete", json.toString()); sukses = json.getInt(TAG_SUKSES); if (sukses == 1) { Intent i = getActivity().getIntent(); getActivity().setResult(100, i); // getActivity().finish(); ListMahasiswa_fragment List_mahasiswa= new ListMahasiswa_fragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.main_content, List_mahasiswa.newInstance("")) .addToBackStack(null) .commit(); } } catch (JSONException e) {e.printStackTrace();} return null; } protected void onPostExecute(String file_url) {pDialog.dismiss();} } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public void lengkapi(String item){ new AlertDialog.Builder(getActivity()) .setTitle("Lengkapi Data") .setMessage("Silakan lengkapi data "+item +" !") .setNeutralButton("Tutup", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { getActivity().finish(); }}).show(); } void callMarquee(){ Calendar cal = Calendar.getInstance(); int jam = cal.get(Calendar.HOUR); int menit= cal.get(Calendar.MINUTE); int detik= cal.get(Calendar.SECOND); int tgl= cal.get(Calendar.DATE); int bln= cal.get(Calendar.MONTH); int thn= cal.get(Calendar.YEAR); String stgl=String.valueOf(tgl)+"-"+String.valueOf(bln)+"-"+String.valueOf(thn); String sjam=String.valueOf(jam)+":"+String.valueOf(menit)+":"+String.valueOf(detik); txtMarquee.setSelected(true); String kata="Selamat Datang @lp2maray.com Aplikasi Android "+stgl+"/"+sjam+" #"; String kalimat=String.format("%1$s", TextUtils.htmlEncode(kata)); txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat)); } }
OK MUDAH YAAAAAAAAA....
Tidak ada komentar:
Posting Komentar