gpt4 book ai didi

java - Android 从数据库中删除一行

转载 作者:行者123 更新时间:2023-12-01 12:20:23 25 4
gpt4 key购买 nike

我正在尝试从数据库中删除一行,我想读取用户输入并删除该行。我遇到的问题是获取用户信息并使用它来删除行。

这是我读取行信息的代码。我不知道在 onClick 方法或天气中写什么,甚至是正确的地方

package com.example.rory.dbtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.pinchtapzoom.R;


public class delete extends Activity {

Button delete;
Public EditText edit1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);

delete = (Button) findViewById(R.id.button);
editi = (EditText) findViewById(R.id.edit1);
delete.setOnClickListener((android.view.View.OnClickListener) this);

public void onClick(View v) {
//what code do i enter here to read in and send rowid to the db class
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.delete, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

这也是我正在使用的数据库代码

//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

最佳答案

首先,您需要获取用户输入并验证它是否是与您的 KEY_ROWID 数据类型相对应的有效值(例如,如果它是整数或长整型)。

假设您的 KEY_ROWID 是一个长数字,您将执行以下操作:

public void onClick(View v) {
//what code do i enter here to read in and send rowid to the db class
//..this code:
String userInput = edit1.getText().toString();

//if the ROW ID its a number (long)
try{
long rowID = Long.parseLong(userInput);
//call the delete method
deleteContact(rowID);
}catch(NumberFormatException e){
Log.e("INPUT ERROR","Input is not a number!",e);
//notify the user that the input is invalid.
Toast.makeText(this,"Invalid Value!",Toast.LENGTH_LONG).show();
}
}

关于java - Android 从数据库中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com