Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SQLite delete user failed
    primarykey
    data
    text
    <p>I'm now currently working on android application with login function, I want to make it so people can delete their own account. I tried some code but it failed to delete. Here's code for my delete method... I declared this method on LoginDataBaseAdapater Class. </p> <pre><code> public boolean deleteUser(String Username) {String user=String.valueOf(Username); return db.delete(TABLE_LOGIN, KEY_USER +"=" + user, null) &gt; 0;} </code></pre> <p>Then I need to call it in activity, I don't know which part of my activity is wrong... Since the code is very long, I try to shortened it like this...</p> <pre><code>package shiro.yuki; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import android.widget.Toast; public class menulayanan extends ListActivity { LoginDataBaseAdapter loginDataBaseAdapter; LoginDataBaseAdapter db; User user; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); loginDataBaseAdapter=new LoginDataBaseAdapter(this); loginDataBaseAdapter=loginDataBaseAdapter.open(); final LoginDataBaseAdapter db = new LoginDataBaseAdapter(this); Intent intent = getIntent(); String uname = (String) intent.getSerializableExtra("username"); final User user = db.getUserDetail(uname); String[] menulayanan = new String[] {"A","B"}; setListAdapter(new array(this, menulayanan)); } public void detail() { db.deleteUser2(user.getUser()); // from logcat seems that this code is wrong Toast.makeText(menulayanan.this, "Hapus Sukses !", Toast.LENGTH_LONG).show(); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Object o = this.getListAdapter().getItem(position); String pilihan = o.toString(); tampilkanPilihan(pilihan); } public void tampilkanPilihan(String pilihan) { try { //Intent digunakan untuk sebagai pengenal suatu activity Intent i = null; if (pilihan.equals("A")) { i = new Intent(this, A.class); } else if (pilihan.equals("B")) { i = new Intent(this, B.class); } else { Toast.makeText(this,"Anda Memilih: " + pilihan + " , Actionnya belum dibuat", Toast.LENGTH_LONG).show(); } startActivity(i); } catch (Exception e) { e.printStackTrace(); } } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.layout.opt_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.delete: AlertDialog.Builder builder3 = new AlertDialog.Builder(this); builder3.setMessage("Ingin menghapus account ?") .setCancelable(false).setPositiveButton("Ya",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { detail(); // from logcat seems that this code is wrong too keluar(); } }) .setNegativeButton("Tidak", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }).show(); return true; default: return super.onOptionsItemSelected(item); } } public void keluar() { Intent awal = new Intent (this, Doa.class); startActivity(awal); finish(); } public void belajar(){ Intent Pembelajaran = new Intent (this, Pembelajaran.class); startActivity(A); } } </code></pre> <p>And from code above, there's a method called getUserDetail, I declared this method in LoginDataBaseAdapter Class too, I kinda think that with this method, I can catch username from who's the active user now. Here's the code..</p> <pre><code> public User getUserDetail(String Username) { User user = null; Cursor cursor = db.query("login", new String[]{"Username", "Password"}, "Username='" +Username+ "'", null, null, null, null); if(cursor.moveToFirst()) { user = new User(); user.setUser(cursor.getString(0)); user.setPass(cursor.getString(1)); } if(cursor != null &amp;&amp; !cursor.isClosed()) { cursor.close(); } return user; } </code></pre> <p>User class itself is just class with some method of getUser,getPassword,setUser and setPassword</p> <p>Please help.....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload