我现在正在开发具有登录功能的 android 应用程序,我想让它让人们可以删除自己的帐户。我尝试了一些代码,但无法删除。这是我的删除方法的代码......我在 LoginDataBaseAdapter 类上声明了这个方法。
public boolean deleteUser(String Username)
{String user=String.valueOf(Username);
return db.delete(TABLE_LOGIN, KEY_USER +"=" + user, null) > 0;}
然后我需要在activity中调用,不知道我的activity哪一部分出错了...由于代码很长,所以我尝试这样缩短...
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);
}
}
从上面的代码中,有一个名为getUserDetail的方法,我也在LoginDataBaseAdapter类中声明了这个方法,我有点想用这个方法,我现在可以从谁是活动用户中获取用户名。这是代码..
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 && !cursor.isClosed())
{
cursor.close();
}
return user;
}
用户类本身只是具有 getUser、getPassword、setUser 和 setPassword 方法的类
请帮忙.....